2011-12-06 119 views
3

什麼是一個可靠的工具用於生成PDF報告?特別是,我們有興趣創建具有視頻的交互式PDF,如發現的示例here自動化PDF生成

現在我們正在使用Python和reportlab生成PDF,但還沒有完全探索庫(主要是因爲許可證的價格是有點令人望而卻步)

我們一直在尋找在Adobe的SDKiText庫,但很難說兩者的功能是什麼。

從模板PDF生成文檔的能力將是一個加號。

任何指針或意見將不勝感激。

謝謝,

回答

5

最近,我需要爲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 
+0

出於好奇:你爲什麼選擇了LaTeX而不是ReportLab的RML? – Goro

+0

@Goro:直到現在我還不知道RML的存在。謝謝。 :) –

0

我建議使用https://github.com/mreiferson/py-wkhtmltox將HTML呈現爲PDF。

並使用您選擇的任何工具將報告呈現爲HTML。我喜歡http://www.makotemplates.org/

+0

不知道它是否可以生成視頻到PDF。可能帶有html5視頻標籤。但可能不是。 – lig

+0

我使用HTML到PDF渲染器(特別是wkhtmltopdf)的經驗是,與使用reportlab等「核心」PDF庫生成的PDF文件相比,生成的PDF文件質量不是很好。 – Goro