Shrink and merge PDFs in Python

不想你离开。 提交于 2019-12-07 23:13:37

问题


I'm trying to shrink and merge two A4 PDF pages into one A4 page so that if I had;

 _____        _____        
|     |      |     |
| p1  |      | p2  | 
|     |      |     |
|_____|      |_____|

I would get;

 _____             
| p1  |    
|.....| 
| p2  |  
|_____|      

As a new PDF, with two A5 sized pages on that one page. Similar to how you might print two pages per page on paper.

I've looked into pyPDF (http://pybrary.net/pyPdf/) ReportLab (http://www.reportlab.com) but I can't seem to find how to shrink and merge like this.

Any hints?

Thanks!


回答1:


pdfnup has this functionality (http://pypi.python.org/pypi/pdfnup)#

e.g.

from pyPdf import PdfFileWriter, PdfFileReader
from pdfnup import generateNup

output = PdfFileWriter()
input1 = PdfFileReader(file("in.pdf", "rb"))

page1 = input1.getPage(0)
page2 = input1.getPage(1)

output.addPage(page1)
output.addPage(page2)

outputStream = file("out.pdf", "wb")
output.write(outputStream)
outputStream.close()

generateNup("out.pdf", 2)



回答2:


The way I would do it (not necessarily the best way, but using tools I have available) would be to use Ghostcript's pdf2ps to convert to PostScript, then append an N-up PostScript preamble (PS is a full programming language and you can redefine builtins like "showpage" to add N-up or posterization directly to a document) and then I'd convert back with ps2pdf.

Offhand I'm not finding a published version of a PostScript N-up utility, but you can google several ones written in other languages that manipulate the PostScript document itself, such as those at http://www.tardis.ed.ac.uk/~ajcd/psutils/



来源:https://stackoverflow.com/questions/4140466/shrink-and-merge-pdfs-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!