PyPdf合并错误 [英] PyPdf Merge error

查看:92
本文介绍了PyPdf合并错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将使用PyPdf的多个Pdf页面合并为使用mergeTranslatedPage的单个页面时,我得到了一些未知字符,这些未知的正方形是上次合并页面中未包括的字符,经过研究后,我认为方法效果不佳,因为后面的页面可能会覆盖旧页面的资源,因此我在每次合并后都尝试了page1.compressContentStreams(),但是没有结果.

When i merge several Pdf pages using PyPdf into one single page using mergeTranslatedPage, i got some unknown characters, these unknown squares are the characters not included in the last merged page, after some research i think that the method _merge_ressources not working very well , because the later page could overwrite the ressources of the older pages , i tried page1.compressContentStreams() after each merge but without a result.

此链接中,您将看到一个PDF示例合并并生成PDF结果.

in this link you will see an example of the PDF that has been merged and the PDF result.

请帮助

推荐答案

以下解决方案使用 merged.pdf 来验证所有字符的格式是否正确.下面的代码默认情况下使用2x2网格,但是您可以在调用merge时通过设置grid参数来更改它.

The below solutions uses the pdfjam command to merge multiple pdf pages into a single pdf page. It's a very powerful command with many different options and good documentation. I tested the solution on the two files you've provided 4_P7.pdf and 4_P13.pdf. You can view the merged.pdf to verify that all characters are formatted correctly. The code below uses a 2x2 grid by default but you can change that by setting the grid argument when you call merge.

from subprocess import check_output

def merge(inputs, output, grid='2x2'):
    check_output(['pdfjam'] + inputs + ['--nup', grid, '--outfile', output])

merge(['4_P7.pdf', '4_P13.pdf'], 'merged.pdf')

下面的评论中有一个问题,关于是否可以像问题示例文件中那样完成自定义职位.问题中提供的布局与以下相同.它首先构造一个顶部布局,即4x2布局,然后构造底部2x6布局,然后最后将这两个布局合并到 final.pdf .在以下示例中使用的pdf可以在此处找到.

There was a question in the comment below as to whether custom positions can be done as is the case in the questions example file. The same layout that was provided in the question is implemented below. It first constructs the top layout which is a 4x2 layout, then the bottom 2x6 layout, then finally merges these two layouts into final.pdf. The pdfs used in the below example can be found here.

from subprocess import check_output

def merge(inputs, output, grid='2x2'):
    return check_output(['pdfjam'] + inputs + ['--nup', grid, '--outfile', output])

files = ['1.pdf', '2.pdf', '3.pdf', '4.pdf', '1.pdf', '2.pdf', '3.pdf', '4.pdf']
merge(files, 'top.pdf', '4x2')

files = ['1.pdf', '2.pdf', '3.pdf', '4.pdf', '5.pdf', '6.pdf', '1.pdf', '2.pdf',
    '3.pdf', '4.pdf', '5.pdf', '6.pdf']
merge(files, 'bottom.pdf', '2x6')

merge(['top.pdf', 'bottom.pdf'], 'final.pdf', '1x2')

这篇关于PyPdf合并错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆