gen_landscape.py
#! /usr/bin/python
#
# gen_landscape.py
#
# Aug/08/2022
#
# ------------------------------------------------------------------
import sys
#
from reportlab.pdfgen import canvas
from reportlab.pdfbase.pdfmetrics import registerFont
from reportlab.pdfbase.cidfonts import UnicodeCIDFont
from reportlab.lib.pagesizes import A4, landscape, portrait
from reportlab.lib.pagesizes import A3
from reportlab.pdfbase.ttfonts import TTFont
# ------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
file_out = sys.argv[1]
sys.stderr.write(file_out + "\n")
#
IPAG_TTF = "/usr/share/fonts/opentype/ipafont-gothic/ipag.ttf"
TakaoMincho_TTF = "/usr/share/fonts/truetype/takao-mincho/TakaoMincho.ttf"
TakaoGothic_TTF = "/usr/share/fonts/truetype/takao-gothic/TakaoGothic.ttf"
#
#IPAG_TTF = "/usr/share/fonts/OTF/ipag.ttf"
#TakaoMincho_TTF = "/usr/share/fonts/OTF/TakaoMincho.ttf"
#TakaoGothic_TTF = "/usr/share/fonts/OTF/TakaoGothic.ttf"
#
registerFont(TTFont('ipag', IPAG_TTF))
registerFont(TTFont('takao_mincho', TakaoMincho_TTF))
registerFont(TTFont('takao_gothic', TakaoGothic_TTF))
#
pdf_canvas = canvas.Canvas(file_out,pagesize=landscape(A4))
#pdf_canvas = canvas.Canvas(file_out,pagesize=landscape(A3))
#
ypos = 500
str_base = "日本語でこんにちは! "
for font in ['ipag','takao_mincho','takao_gothic']:
pdf_canvas.setFont(font, 30)
str_out = str_base + font
pdf_canvas.drawString(100, ypos, str_out)
ypos -= 100
#
ypos = 200
str_base="2022年8月8日 PM 12:48"
for font in ['ipag','takao_mincho','takao_gothic']:
pdf_canvas.setFont(font, 15)
str_out = str_base + " " + font
pdf_canvas.drawString(400, ypos, str_out)
ypos -= 50
#
pdf_canvas.showPage()
pdf_canvas.save()
#
sys.stderr.write("*** 終了 ***\n")
# ------------------------------------------------------------------
実行方法
./gen_landscape.py out01.pdf
次のバージョンで確認しました。
$ python --version
Python 3.10.4
A4縦 PDF ファイルを作成する方法のサンプルはこちら
Python3: reportlab で PDF ファイルを作成