PyPdf2 与扫描的 PDF 合并会产生"An error exists on this page..."



我想使用PyPDF2获取扫描的PDF文档的每一页

  • 将页面缩放到原始大小的85%
  • 并将页面居中放置在8.5乘11的空白页面上
  • 具有相同页数

创建打印/添加条形码所需的页边距。

我已经尝试了几种mergeScaledTranslatedPage方法,但当我在Adobe Acrobat DC中打开文件时,总是会收到一条错误消息。

即使输出看起来是成功的,我在打开文件时也会收到以下错误:

此页面上存在错误。Acrobat可能无法正确显示页面。请与创建PDF文档的人员联系以更正此问题。

如何使其工作?

我是pypdfPyPDF2的维护者。请使用pypdf

from pypdf import PdfReader, PdfWriter, Transformation
from pypdf.generic import RectangleObject
reader = PdfReader("GeoTopo.pdf")
writer = PdfWriter()

desired_width = 100
desired_height = 100
r = RectangleObject([0, 0, desired_width, desired_height])
for page in reader.pages[:10]:
old_width = page.mediabox.width
old_height = page.mediabox.height
a1 = desired_width / old_width
a2 = desired_height / old_height
factor = min(a1, a2)
new_width = float(old_width * factor)
new_height = float(old_height * factor)
dx = (desired_width - new_width) / 2
dy = (desired_height - new_height) / 2
op = Transformation().translate(tx=dx, ty=dy)
page.scale_to(width=new_width, height=new_height)
page.add_transformation(op)
page.mediabox = r
page.artbox = r
page.cropbox = r
page.bleedbox = r
page.trimbox = r
writer.add_page(page)
with open("foo.pdf", "wb") as fp:
writer.write(fp)
``

相关内容

最新更新