データーベースなどから読み込んだデータをPDF帳票出力する時に、表組出力~罫線等で囲んで
連続した出力をする場合、
Jasperreports on Java | 詳細で複雑な表でも可能だが、 XMLテンプレート作成がとても面倒で労力が必要 |
ReportLab on Python | テンプレート作成ではなくフラグメント作成→連続出力だが、 機能を習得するのがとてもたいへんで学習コストがかかる |
そんな壁があるなら、一度HTMLで、ul-li タグや、table タグで出力して、
HTMLからPDFに変換すれば良い。
なにも学習コストで時間を費やす必要がなく効率的と考える。
PDF出力で必要なレイアウト決定の為に、
A4サイズ、マージン=0.4 inch で、1ページの縦横サイズを、
以下、Python と HTMLで求めた。
pdfkit · PyPI が必要なので、インストールしておき、
pdfkit.from_file( HTMLファイルPATH , 出力PDFファイルPATH , options=options)
で、HTML→PDF作成である。
A4 向き Portrait HTML→PDF
'orientation': 'Portrait' を指定する。default は、Portrait でよく省略されてる。
import pdfkit options = { 'page-size': 'A4', 'orientation': 'Portrait', 'margin-top': '0.4in', 'margin-right': '0.4in', 'margin-bottom': '0.4in', 'margin-left': '0.4in', 'encoding': "UTF-8", 'no-outline': None } pdfkit.from_file("portrait.html", "out_port.pdf", options=options)
A4 向き Portrait サイズ確認用 HTML
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta http-equiv="content-language" content="ja"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>portrait</title> <style type="text/css"> html{ width:100%; height:100%; } body{ margin: 0; padding: 0; } #content{ border: 1px solid #000099; width: 1432px; height: 2074px; } </style> </head> <body> <div id="content"></div> </body> </html>
A4 横向き Landscape HTML→PDF
'orientation': 'Landscape' を指定する。
import pdfkit options = { 'page-size': 'A4', 'orientation': 'Landscape', 'margin-top': '0.4in', 'margin-right': '0.4in', 'margin-bottom': '0.4in', 'margin-left': '0.4in', 'encoding': "UTF-8", 'no-outline': None } pdfkit.from_file("landscape.html", "out_landscape.pdf", options=options)
A4 横向き Landscape サイズ確認用 HTML
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta http-equiv="content-language" content="ja"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>landscape</title> <style type="text/css"> html{ width:100%; height:100%; } body{ margin: 0; padding: 0; } #content{ border: 1px solid #000099; width: 2092px; height: 1432px; } </style> </head> <body> <div id="content"> </div> </body> </html>
まとめると、
PDF変換元HTMLは、
A4サイズ PDF(左右上下マージン=0.4inch)
を作成するために、
A4 | width | height |
Portrait(縦向き) | 1432px | 2074px |
Landscape(横向き) | 2092px | 1432px |
で、HTMLの CSSレイアウトを組む必要がある。