buildersbox.corp-sansan.com
⇒ BSDっぽい。
https://www.reportlab.com/documentation/faq/#1.3.1
C:\python>python -m venv pdf C:\python>pdf\Scripts\activate (pdf) C:\python>cd pdf (pdf) C:\python\pdf>mkdir src (pdf) C:\python\pdf>cd src (pdf) C:\python\pdf\src>pip install PyPDF2 --proxy http://proxy01.xxx (pdf) C:\python\pdf\src>pip install reportlab --proxy http://proxy01.xxx
フォントはここでダウンロードした。
IPAexフォントのダウンロード
こんな感じで動くっぽい。
import io from PyPDF2 import PdfFileWriter from PyPDF2 import PdfFileReader from reportlab.lib.colors import Color from reportlab.lib.pagesizes import letter from reportlab.pdfbase import pdfmetrics from reportlab.pdfbase.cidfonts import UnicodeCIDFont from reportlab.pdfbase.ttfonts import TTFont from reportlab.pdfgen import canvas pdf = PdfFileReader("./test1.pdf") pdfmetrics.registerFont( TTFont( 'IPAexGothic', './ipaexg.ttf' ) ) page = pdf.getPage(0) packet = io.BytesIO() #can = canvas.Canvas(packet, pagesize=letter) can = canvas.Canvas(packet) font_size = 40 can.setFont('IPAexGothic', font_size) can.setFillColor(Color(1, 0, 0, alpha=1)) can.drawString( 000, 100, "検収 2020/06/30 山田 太郎" ) can.save() packet.seek(0) new_pdf = PdfFileReader(packet) page.mergePage(new_pdf.getPage(0)) tmp = io.BytesIO() output = PdfFileWriter() output.addPage(page) with open("test2.pdf", "wb") as fout: output.write(fout) output.write(tmp) print(type(fout)) print(tmp.getvalue())