from PyPDF2.generic import RectangleObject
from PyPDF2 import PdfReader, PdfWriter, Transformation
# PyPDF2已不更新,库名改为 pypdf
# 1.拼接在右方
def merge_pdf_right():
reader = PdfReader("1.pdf")
page1 = reader.pages[0]
print('page1.cropbox:', page1.cropbox)
reader2 = PdfReader("2.pdf")
page2 = reader2.pages[0]
offset = page1.cropbox.right
print('offset:', offset)
op = Transformation().translate(tx=offset, ty=0) # 拼接在右侧
page2.add_transformation(op)
cb = page2.cropbox
page2.mediabox = RectangleObject((cb.left + offset, cb.bottom, cb.right + offset, cb.top))
page2.cropbox = RectangleObject((cb.left + offset, cb.bottom, cb.right + offset, cb.top))
page2.trimbox = RectangleObject((cb.left + offset, cb.bottom, cb.right + offset, cb.top))
page2.bleedbox = RectangleObject((cb.left + offset, cb.bottom, cb.right + offset, cb.top))
page2.artbox = RectangleObject((cb.left + offset, cb.bottom, cb.right + offset, cb.top))
page1.merge_page(page2, expand=True)
mb = page1.mediabox
print('mb:', mb)
page1.mediabox = RectangleObject((mb.left, mb.bottom, mb.right, mb.top))
page1.cropbox = RectangleObject((mb.left, mb.bottom, mb.right, mb.top))
page1.trimbox = RectangleObject((mb.left, mb.bottom, mb.right, mb.top))
page1.bleedbox = RectangleObject((mb.left, mb.bottom, mb.right, mb.top))
page1.artbox = RectangleObject((mb.left, mb.bottom, mb.right, mb.top))
print('page1.mediabox:', page1.mediabox)
writer = PdfWriter()
writer.add_page(page1)
with open("output.pdf", "wb") as fp:
writer.write(fp)
# 2.拼接在下方
def merge_pdf_bottom():
reader = PdfReader("1.pdf")
page1 = reader.pages[0]
print('page1.cropbox:', page1.cropbox)
reader2 = PdfReader("2.pdf")
page2 = reader2.pages[0]
height = page1.cropbox.height
print('height:', height)
op = Transformation().translate(tx=0, ty=height) # 拼接在下方
page2.add_transformation(op)
cb = page2.cropbox
page2.mediabox = RectangleObject((cb.left, cb.bottom+height, cb.right, cb.top+height))
page2.cropbox = RectangleObject((cb.left, cb.bottom+height, cb.right, cb.top+height))
page2.trimbox = RectangleObject((cb.left, cb.bottom+height, cb.right, cb.top+height))
page2.bleedbox = RectangleObject((cb.left, cb.bottom+height, cb.right, cb.top+height))
page2.artbox = RectangleObject((cb.left, cb.bottom+height, cb.right, cb.top+height))
page1.merge_page(page2, expand=True)
mb = page1.mediabox
print('mb:', mb)
page1.mediabox = RectangleObject((mb.left, mb.bottom, mb.right, mb.top))
page1.cropbox = RectangleObject((mb.left, mb.bottom, mb.right, mb.top))
page1.trimbox = RectangleObject((mb.left, mb.bottom, mb.right, mb.top))
page1.bleedbox = RectangleObject((mb.left, mb.bottom, mb.right, mb.top))
page1.artbox = RectangleObject((mb.left, mb.bottom, mb.right, mb.top))
print('page1.mediabox:', page1.mediabox)
writer = PdfWriter()
writer.add_page(page1)
with open("output.pdf", "wb") as fp:
writer.write(fp)
# 3. 自己将内容写入空白pdf,再进行拼接
import fitz
import qrcode
doc = fitz.open()
page_new = doc.new_page(width=45, height=30)
text = 'product_nameproduct_nameproduct'
page_new.insert_text(fitz.Point(0, 20), text, fontname="helv", fontsize=8)
qrcode_str = 'https://www.bing.com'
pdf_bytes = qrcode.make(qrcode_str)
rect = fitz.Rect(0, 2, 18, 18)
page.insert_image(rect, pixmap=pdf_bytes.get_image().toqpixmap(), overlay=True)
doc.save('lw.pdf')