如何使用 pyPdf 合并两个横向 pdf 页面

发布于 2024-11-07 18:39:09 字数 699 浏览 4 评论 0原文

我在使用 pyPdf 合并两个 PDF 文件时遇到问题。当我运行以下代码时,水印(第 1 页)看起来不错,但第 2 页已顺时针旋转 90 度。

有什么想法吗?

问题示例

from pyPdf import PdfFileWriter, PdfFileReader

# PDF1: A4 Landscape page created in photoshop using PdfCreator, 
input1 = PdfFileReader(file("base.pdf", "rb"))
page1 = input1.getPage(0)

# PDF2: A4 Landscape page, text only, created using Pisa (www.xhtml2pdf.com)
input2 = PdfFileReader(file("text.pdf", "rb"))
page2 = input2.getPage(0)

# Merge
page1.mergePage(page2)

# Output
output = PdfFileWriter()
output.addPage(page1)
outputStream = file("output.pdf", "wb")
output.write(outputStream)
outputStream.close()

I'm having trouble merging two PDF files with pyPdf. When I run the following code the the watermark (page1) looks fine, but the page2 has been rotated 90 degrees clockwise.

Any ideas what's going on?

Example of what's going wrong

from pyPdf import PdfFileWriter, PdfFileReader

# PDF1: A4 Landscape page created in photoshop using PdfCreator, 
input1 = PdfFileReader(file("base.pdf", "rb"))
page1 = input1.getPage(0)

# PDF2: A4 Landscape page, text only, created using Pisa (www.xhtml2pdf.com)
input2 = PdfFileReader(file("text.pdf", "rb"))
page2 = input2.getPage(0)

# Merge
page1.mergePage(page2)

# Output
output = PdfFileWriter()
output.addPage(page1)
outputStream = file("output.pdf", "wb")
output.write(outputStream)
outputStream.close()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

半边脸i 2024-11-14 18:39:09

您可以在将页面合并到另一个页面时转换该页面。我定义了这个函数来在合并时围绕一个点旋转页面:

def mergeRotateAroundPointPage(page, page2, rotation, tx, ty):
    translation = [[1, 0, 0],
                   [0, 1, 0],
                   [-tx,-ty,1]]
    rotation = math.radians(rotation)
    rotating = [[math.cos(rotation), math.sin(rotation),0],
                [-math.sin(rotation),math.cos(rotation), 0],
                [0,                  0,                  1]]
    rtranslation = [[1, 0, 0],
                   [0, 1, 0],
                   [tx,ty,1]]
    ctm = utils.matrixMultiply(translation, rotating)
    ctm = utils.matrixMultiply(ctm, rtranslation)

    return page.mergeTransformedPage(page2, [ctm[0][0], ctm[0][1],
                                             ctm[1][0], ctm[1][1],
                                             ctm[2][0], ctm[2][1]])

然后你可以这样调用它:

mergeRotateAroundPointPage(page1, page2, 
                page1.get('/Rotate') or 0, 
                page2.mediaBox.getWidth()/2, page2.mediaBox.getWidth()/2)

You can transform the page while you're merging it into another page. I defined this function to rotate the page around a point while being merged:

def mergeRotateAroundPointPage(page, page2, rotation, tx, ty):
    translation = [[1, 0, 0],
                   [0, 1, 0],
                   [-tx,-ty,1]]
    rotation = math.radians(rotation)
    rotating = [[math.cos(rotation), math.sin(rotation),0],
                [-math.sin(rotation),math.cos(rotation), 0],
                [0,                  0,                  1]]
    rtranslation = [[1, 0, 0],
                   [0, 1, 0],
                   [tx,ty,1]]
    ctm = utils.matrixMultiply(translation, rotating)
    ctm = utils.matrixMultiply(ctm, rtranslation)

    return page.mergeTransformedPage(page2, [ctm[0][0], ctm[0][1],
                                             ctm[1][0], ctm[1][1],
                                             ctm[2][0], ctm[2][1]])

Then you call it like this:

mergeRotateAroundPointPage(page1, page2, 
                page1.get('/Rotate') or 0, 
                page2.mediaBox.getWidth()/2, page2.mediaBox.getWidth()/2)
三生一梦 2024-11-14 18:39:09

我找到了解决方案。我的代码很好 - 我只需要更改生成原始 PDF 文件的方式。

而不是使用 PdfCreator & 创建 PDF Photoshop,我将 Photoshop 图像复制并粘贴到 MS Word 2007 中,然后使用它的导出功能为第 1 页创建 PDF 文件。现在效果很好!

因此,PdfCreator 必须生成与 pyPdf 不兼容的 PDF 文件。

I found a solution. My code was fine - I just had to change how I generated the original PDF files.

Instead of creating the PDF using PdfCreator & Photoshop, I copy and pasted my photoshop image into MS Word 2007, and then used it's export feature to create the PDF file for page1. It now works great!

So, PdfCreator must producing PDF files that are not compatible with pyPdf.

悲欢浪云 2024-11-14 18:39:09

由于您使用的是 pyPdf,因此这应该可以解决旋转页面的问题:

output.addPage(input1.getPage(1).rotateClockwise(90))

Since you're using pyPdf, this should do the trick for rotating pages:

output.addPage(input1.getPage(1).rotateClockwise(90))
泛泛之交 2024-11-14 18:39:09

我想补充一点,我使用 Photoshop 保存 PDF,但与 1.4 版本兼容。这生成了一个巨大的 PDF 文件,但它有效。

所以这是 pyPDF 没有正确读取它。

I would like to add that I used Photoshop to save the PDF but as version 1.4 compatible. This made a huge PDF file but it worked.

So it is pyPDF not reading it right.

耀眼的星火 2024-11-14 18:39:09

您可以在页面对象中使用rotateClockwise 或rotataeCounterClockwise 函数。

page2 = input2.getPage(0).rotateCounterClockwise(90)

You can make use of the rotateClockwise or rotataeCounterClockwise function in the page object.

page2 = input2.getPage(0).rotateCounterClockwise(90)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文