19

JinjaがHTMLに対して行うのと同じように機能する、PythonでのPDF用の最も正確なツールを探しています。

あなたの提案は何ですか?

4

7 に答える 7

13

jbochiが回答したように、ReportLabはPDFを生成するほとんどすべてのPythonプロジェクトの基盤です。

ただし、ニーズに応じて、Pisa/xhtml2pdfを確認することをお勧めします。Jinjaテンプレートを使用してHTMLを生成し、Pisaを使用してHTMLをPDFに変換します。PisaはReportLabの上に構築されています。

編集:私が忘れていたもう1つのオプションはwkhtmltopdfです

于 2010-01-14T17:16:59.973 に答える
4

Have a look at ReportLab Toolkit.

You can use templates only with the commercial version, though.

于 2010-01-14T14:59:41.893 に答える
3

現在、ブロックにはWeasyPrintという新しい子供がいます。

于 2012-02-10T19:55:41.240 に答える
3

OPとまったく同じ要件がありました。残念ながら、WeasyPrintは実行可能なソリューションではありませんでした。これは、非常に正確なポジショニングとバーコードのサポートが必要だったためです。数日間の作業の後、Jinja2をサポートするreportlabXMLラッパーを完成させました。

コードはGitHubにあり、次のPDFを生成するXMLの例が含まれています。

于 2012-07-18T15:22:51.193 に答える
2

rst2pdfまたはpandocを使用して、python/jinjaからrst/htmlおよびhtml/rstからpdfについてはどうでしょうか。

これらは両方とも私にとってはうまくいきましたが。プレーのように、私は将来、 Weasyprintを試すかもしれません。

于 2012-05-18T18:15:10.633 に答える
1

Jinja自体よりもJinjaのように機能するPythonのPDF用のより正確なツールは何ですか?

Jinjaブロック、変数、およびコメントの識別文字列がコマンドと競合しないことを確認する必要がありLaTeXます。環境を変更してJinja環境を模倣すると、LaTeX準備が整います。

箱から出してすぐに機能するスニペットを次に示します。

Pythonソース: ./create_pdf.py

import os, jinja2
from jinja2 import Template

latex_jinja_env = jinja2.Environment(
    block_start_string    = '\BLOCK{',
    block_end_string      = '}',
    variable_start_string = '\VAR{',
    variable_end_string   = '}',
    comment_start_string  = '\#{',
    comment_end_string    = '}',
    line_statement_prefix = '%%',
    line_comment_prefix   = '%#',
    trim_blocks           = True,
    autoescape            = False,
    loader                = jinja2.FileSystemLoader(os.path.abspath('./latex/'))
)
template = latex_jinja_env.get_template('latex_template.tex')

# populate a dictionary with the variables of interest
template_vars  = {}
template_vars['section_1'] = 'The Section 1 Title'
template_vars['section_2'] = 'The Section 2 Title'

# create a file and save the latex
output_file = open('./generated_latex.tex', 'w')
# pass the dictionary with variable names to the renderer
output_file.write( template.render( template_vars ) )
output_file.close()

ラテックステンプレート: ./latex/latex_template.tex

\documentclass{article}
\begin{document}
\section{Example}
An example document using \LaTeX, Python, and Jinja.

% This is a regular LaTeX comment
\section{\VAR{section_1}}
\begin{itemize}
\BLOCK{ for x in range(0,3) }
  \item Counting: \VAR{x}
\BLOCK{ endfor }
\end{itemize}

\#{This is a long-form Jinja comment}
\BLOCK{ if subsection_1_1 }
\subsection{ The subsection }
This appears only if subsection_1_1 variable is passed to renderer.
\BLOCK{ endif }

%# This is a short-form Jinja comment
\section{\VAR{section_2}}
\begin{itemize}
%% for x in range(0,3)
  \item Counting: \VAR{x}
%% endfor
\end{itemize}

\end{document}

今すぐ呼び出す:$> python ./create_pdf.py

結果として生じるラテックスソース: ./generated_latex.tex

\documentclass{article}
\begin{document}
\section{Example}
An example document using \LaTeX, Python, and Jinja.

% This is a regular LaTeX comment
\section{The Section 1 Title}
\begin{itemize}
  \item Counting: 0
  \item Counting: 1
  \item Counting: 2
\end{itemize}

\section{The Section 2 Title}
\begin{itemize}
  \item Counting: 0
  \item Counting: 1
  \item Counting: 2
\end{itemize}

\end{document}

生成されたPDF:

ここに画像の説明を入力してください

参照:

于 2017-06-19T23:55:58.087 に答える
0

元のドキュメントを変更せずに既存のPDFをテンプレートとして使用する場合は、Dhekテンプレートエディタを使用できます。これにより、別のテンプレートファイルで領域(境界、名前、タイプ)を定義できます。

テンプレートはJSON形式で保存されるため、Pythonで解析して、PDF上の領域を埋め、最終的なドキュメントを生成できます(たとえば、Webフォームの値を使用)。

https://github.com/applicius/dhekのドキュメントを参照してください。

于 2014-07-17T11:37:40.067 に答える