【问题标题】:Automating PDF generation自动生成 PDF
【发布时间】:2012-01-13 17:25:57
【问题描述】:

生成 PDF 报告的可靠工具是什么?特别是,我们有兴趣创建包含视频的交互式 PDF,例如 here 中的示例。

目前我们正在使用 Python 和 reportlab 生成 PDF,但还没有完全探索该库(主要是因为许可证定价有点令人望而却步)

我们一直在研究 Adob​​e 的 SDK 和 iText 库,但很难说它们的功能是什么。

能够从模板 PDF 生成文档将是一个加分项。

任何指针或 cmets 将不胜感激。

谢谢,

【问题讨论】:

    标签: python pdf adobe report acrobat


    【解决方案1】:

    最近,我需要为 Django 应用程序创建 PDF 报告; ReportLab 许可证可用,但我最终选择了 LaTeX。这种方法的好处是我们可以使用Django templates 来生成 LaTeX 源代码,而不必为我们需要创建的许多报告编写大量代码而烦恼。另外,我们可以利用相对更简洁的 LaTeX 语法(它确实有很多怪癖,并不适合所有用途)。

    This snippet 提供了该方法的一般概述。我发现有必要进行一些更改,我在这个问题的末尾提供了这些更改。主要的补充是检测Rerun LaTeX 消息,这表明需要额外的通过。用法很简单:

    def my_view(request):
        pdf_stream = process_latex(
            'latex_template.tex',
            context=RequestContext(request, {'context_obj': context_obj})
        )
        return HttpResponse(pdf_stream, content_type='application/pdf')
    

    可以在 LaTeX 生成的 PDF 中嵌入视频,但我对此没有任何经验。 Here 是顶级Google result。

    此解决方案确实需要生成一个新进程 (pdflatex),因此如果您想要纯 Python 解决方案,请继续寻找。

    import os
    from subprocess import Popen, PIPE
    from tempfile import NamedTemporaryFile
    
    from django.template import loader, Context
    
    
    class LaTeXException(Exception):
        pass
    
    
    def process_latex(template, context={}, type='pdf', outfile=None):
        """
        Processes a template as a LaTeX source file.
        Output is either being returned or stored in outfile.
        At the moment only pdf output is supported.
        """
        t = loader.get_template(template)
        c = Context(context)
        r = t.render(c)
    
        tex = NamedTemporaryFile()
        tex.write(r)
        tex.flush()
        base = tex.name
        names = dict((x, '%s.%s' % (base, x)) for x in (
            'log', 'aux', 'pdf', 'dvi', 'png'))
        output = names[type]
    
        stdout = None
        if type == 'pdf' or type == 'dvi':
            stdout = pdflatex(base, type)
        elif type == 'png':
            stdout = pdflatex(base, 'dvi')
            out, err = Popen(
                ['dvipng', '-bg', '-transparent', names['dvi'], '-o', names['png']],
                cwd=os.path.dirname(base), stdout=PIPE, stderr=PIPE
            ).communicate()
    
        os.remove(names['log'])
        os.remove(names['aux'])
    
        # pdflatex appears to ALWAYS return 1, never returning 0 on success, at
        # least on the version installed from the Ubuntu apt repository.
        # so instead of relying on the return code to determine if it failed,
        # check if it successfully created the pdf on disk.
        if not os.path.exists(output):
            details = '*** pdflatex output: ***\n%s\n*** LaTeX source: ***\n%s' % (
                stdout, r)
            raise LaTeXException(details)
    
        if not outfile:
            o = file(output).read()
            os.remove(output)
            return o
        else:
            os.rename(output, outfile)
    
    
    def pdflatex(file, type='pdf'):
        out, err = Popen(
            ['pdflatex', '-interaction=nonstopmode', '-output-format', type, file],
            cwd=os.path.dirname(file), stdout=PIPE, stderr=PIPE
        ).communicate()
    
        # If the output tells us to rerun, do it by recursing over ourself.
        if 'Rerun LaTeX.' in out:
            return pdflatex(file, type)
        else:
            return out
    

    【讨论】:

    • 出于好奇:您为什么选择 LaTeX 而不是 ReportLab 的 RML?
    • @Goro:直到现在我才知道 RML 的存在。谢谢。 :)
    【解决方案2】:

    我建议使用https://github.com/mreiferson/py-wkhtmltox 将 HTML 渲染为 PDF。

    并使用您选择的任何工具将报告呈现为 HTML。我喜欢http://www.makotemplates.org/

    【讨论】:

    • 不确定它是否可以将视频生成为 pdf。可能带有 html5 视频标签。但可能不会。
    • 我使用 HTML 到 PDF 渲染器(尤其是 wkhtmltopdf)的经验是,与使用“核心”PDF 库(如 reportlab)生成的 PDF 文件相比,生成的 PDF 文件质量不是很好。
    猜你喜欢
    • 2015-06-03
    • 1970-01-01
    • 1970-01-01
    • 2010-10-25
    • 1970-01-01
    • 2020-06-10
    • 2017-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多