2011-12-06 31 views
3

PDFレポートを生成するために使用するソリッドツールは何ですか?特に、hereのように、ビデオを含むインタラクティブなPDFを作成することに興味があります。PDF生成の自動化

(ライセンス価格は少し法外である主な理由)

を今、我々はPDFを生成するPythonとreportlabを使用しているが、完全にライブラリを探索していない私たちは、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

好奇心をそらして:なぜあなたはReportLabのRMLでLaTeXを選んだのですか? – Goro

+0

@ゴロ:今までRMLが存在していたことは分かりませんでした。ありがとう。 :) –

0

https://github.com/mreiferson/py-wkhtmltoxを使用してHTMLをPDFにレンダリングすることをお勧めします。

レポートをHTMLとしてレンダリングするために選択したツールを使用します。私は好きですhttp://www.makotemplates.org/

+0

それがpdfへのビデオを生成できるかどうかわからない。 html5ビデオタグを使用している可能性があります。おそらくそうではありません。 – lig

+0

HTMLからPDFへのレンダリング(特にwkhtmltopdf)の経験は、結果のPDFファイルが、reportlabのような "コア" PDFライブラリで生成されたものと比べて非常に良い品質ではないということです。 – Goro