4
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Python3: reportlab で A4横 PDF ファイルを作成

Last updated at Posted at 2019-01-11

次のような A4横ファイルを作成する方法です。
image.png

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 ファイルを作成

4
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?