admin管理员组

文章数量:1648943

python使用fpdf2包和pdfrw包在已有的PDF页面上添加新的页

目录

python使用fpdf2包和pdfrw包在已有的PDF页面上添加新的页

#包安装

#新内容添加到已有的PDF页面上


#包安装

pip install fpdf2

pip install pdfrw

#新内容添加到已有的PDF页面上

import sys
from fpdf import FPDF
from pdfrw import PdfReader, PdfWriter

IN_FILEPATH = sys.argv[1]
OUT_FILEPATH = sys.argv[2]
NEW_PAGE_INDEX = 1  # set to None to append at the end

def new_page():
    fpdf = FPDF()
    fpdf.add_page()
    fpdf.set_font("helvetica", size=36)
    fpdf.text(50, 50, "Hello!")
    reader = PdfReader(fdata=bytes(fpdf.output()))
    return reader.pages[0]

writer = PdfWriter(trailer=PdfReader(IN_FILEPATH))
writer.addpage(new_page(), at_index=NEW_PAGE_INDEX)
writer.write(OUT_FILEPATH)


fpdf2 is a library for PDF document generation in Python, forked from the unmaintained pyfpdf, itself ported from the PHP FPDF library.

参考:fpdf2

本文标签: 页面PythonPDFpdfrw