简体   繁体   中英

Python - PyPdf2 merge does not keep PDF size

I have a size issue when I merge a PDF using PyPDF2. I have the following code to merge pdfs files :

merger = PyPDF2.PdfFileMerger()
    for pdf in fileSorted:
        merger.append(pdf[1])
        os.remove(pdf[1])
    merger.write(tmpPath + '/result.pdf')

The problem is, the PDF size is too high than the originals ones. How can I specify a pdf size ?

The input file has a size of 210*297mm (A4) and the ouptut has a size of 900x1273mm

Thanks a lot

I finally found the solution. My problem was here because before merge I convert pdf to jpg and then convert jpg to pdf and then merge it. So I change the merge to this :

writer = PyPDF2.PdfFileWriter()
for pdf in fileSorted:
   reader = PyPDF2.PdfFileReader(pdf[1])
   page   = PageObject.createBlankPage(reader)
   page.mergePage(reader.getPage(0))
   page.scaleTo(width=595,height=842)
   writer.addPage(page)


outputStream = open(tmpPath + '/result.pdf', 'wb')
writer.write(outputStream)
outputStream.close()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM