3
6

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 5 years have passed since last update.

既存の PDF ファイルに加筆する (python3)

Last updated at Posted at 2018-10-02
pdf_overwrite.py
#! /usr/bin/python
#
#	pdf_overwrite.py
#
#						Oct/02/2018
#
# ------------------------------------------------------------------
import sys
from pdfrw import PdfReader
from pdfrw.buildxobj import pagexobj
from pdfrw.toreportlab import makerl
from reportlab.pdfgen import canvas
from reportlab.pdfbase.cidfonts import UnicodeCIDFont
from reportlab.pdfbase import pdfmetrics

# ------------------------------------------------------------------
sys.stderr.write ("*** 開始 ***\n")
file_in = sys.argv[1]
file_out = sys.argv[2]
sys.stderr.write(file_in + "\n")
sys.stderr.write(file_out + "\n")
#

cc = canvas.Canvas(file_out)
fontname_g = "HeiseiKakuGo-W5"
pdfmetrics.registerFont (UnicodeCIDFont (fontname_g))
cc.setFont(fontname_g,16)

page = PdfReader(file_in,decompress=False).pages
pp = pagexobj(page[0])
cc.doForm(makerl(cc, pp))

cc.drawString(100,600,"こんにちは")
cc.drawString(150,500,"本日は晴天なり。")
cc.drawString(100,400,"Hello World")
cc.showPage()
cc.save()
#
sys.stderr.write ("*** 終了 ***\n")
# ------------------------------------------------------------------

実行方法

./pdf_overwrite.py template.pdf out01.pdf

参考ページ
既存の複数ページの PDF ファイルに加筆する (python3)

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?