3
3

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

Last updated at Posted at 2018-10-02
pdf_gen.py
#! /usr/bin/python
#
#	pdf_gen.py
#
#						Feb/16/2019
#
# -------------------------------------------------------------------------
import sys
from reportlab.pdfgen import canvas
from reportlab.pdfbase.cidfonts import UnicodeCIDFont
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.lib.pagesizes import A4
# -------------------------------------------------------------------------
def draw01_proc(cc):
	cc.drawString(100,100,"0 こんにちは")
	cc.drawString(100,200,"1 本日は晴天なり")
	cc.drawString(100,300,"2 Good Afternoon")
	cc.drawString(100,400,"3 Hello World")
#
# -------------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
file_pdf = sys.argv[1]
sys.stderr.write(file_pdf + "\n")
cc = canvas.Canvas(file_pdf,pagesize=A4,bottomup=False)
# cc = canvas.Canvas(file_pdf,pagesize=A4)
width, height = A4
print(width,height)
fontname_g = "HeiseiKakuGo-W5"
pdfmetrics.registerFont(UnicodeCIDFont(fontname_g))
cc.setFont(fontname_g,16)

#pdfmetrics.registerFont(TTFont('TakaoExGothic','TakaoExGothic.ttf'))
#cc.setFont("TakaoExGothic", 16)


draw01_proc(cc)
cc.showPage()
cc.save()
#
sys.stderr.write("*** 終了 ***\n")
# -------------------------------------------------------------------------

実行結果
image.png

次のバージョンで確認しました。

$ python --version
Python 3.10.4

参考ページ
Python3: reportlab で A4横 PDF ファイルを作成
reportlab で 複数ページの PDF を作成

3
3
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
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?