JinjaがHTMLに対して行うのと同じように機能する、PythonでのPDF用の最も正確なツールを探しています。
あなたの提案は何ですか?
JinjaがHTMLに対して行うのと同じように機能する、PythonでのPDF用の最も正確なツールを探しています。
あなたの提案は何ですか?
jbochiが回答したように、ReportLabはPDFを生成するほとんどすべてのPythonプロジェクトの基盤です。
ただし、ニーズに応じて、Pisa/xhtml2pdfを確認することをお勧めします。Jinjaテンプレートを使用してHTMLを生成し、Pisaを使用してHTMLをPDFに変換します。PisaはReportLabの上に構築されています。
編集:私が忘れていたもう1つのオプションはwkhtmltopdfです
Have a look at ReportLab Toolkit.
You can use templates only with the commercial version, though.
現在、ブロックにはWeasyPrintという新しい子供がいます。
rst2pdfまたはpandocを使用して、python/jinjaからrst/htmlおよびhtml/rstからpdfについてはどうでしょうか。
これらは両方とも私にとってはうまくいきましたが。プレーのように、私は将来、 Weasyprintを試すかもしれません。
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:
参照:
元のドキュメントを変更せずに既存のPDFをテンプレートとして使用する場合は、Dhekテンプレートエディタを使用できます。これにより、別のテンプレートファイルで領域(境界、名前、タイプ)を定義できます。
テンプレートはJSON形式で保存されるため、Pythonで解析して、PDF上の領域を埋め、最終的なドキュメントを生成できます(たとえば、Webフォームの値を使用)。
https://github.com/applicius/dhekのドキュメントを参照してください。