How to append PDF pages using PyPDF2

后端 未结 5 865
灰色年华
灰色年华 2020-12-16 19:03

Is anybody has experience merging two page of PDF file into one using python lib PyPDF2. When I try page1.mergePage(page2) it results with page2 overlayed page1

5条回答
  •  醉酒成梦
    2020-12-16 20:02

    If the 2 PDFs do not exist on your local machine, and instead are normally accessed/download via a URL (i.e. http://foo/bar.pdf & http://bar/foo.pdf), we can fetch both PDFs from remote locations and merge them together in memory in one-fell-swoop.

    This eliminates the assumed step of downloading the PDF to begin with, and allows us to generalize beyond the simple case of both PDFs existing on disk. Specifically, it generalizes the solution to any HTTP-accessible PDF.

    The example:

        from PyPDF2 import PdfFileMerger, PdfFileReader
    
        pdf_content_1 = requests.get('http://foo/bar.pdf').content
        pdf_content_2 = requests.get('http://bar/foo.pdf').content
    
        # Write to in-memory file-like buffers        
        pdf_buffer_1 = StringIO.StringIO().write(pdf_content_1)
        pdf_buffer_2 = StringIO.StringIO().write(pdf_content_2)
        pdf_merged_buffer = StringIO.StringIO()
    
        merger = PdfFileMerger()
        merger.append(PdfFileReader(pdf_buffer_1))
        merger.append(PdfFileReader(pdf_buffer_2))
        merger.write(pdf_merged_buffer)
    
        # Option 1:
        # Return the content of the buffer in an HTTP response (Flask example below)
        response = make_response(pdf_merged_buffer.getvalue())
        # Set headers so web-browser knows to render results as PDF
        response.headers['Content-Type'] = 'application/pdf'
        response.headers['Content-Disposition'] = \ 
            'attachment; filename=%s.pdf' % 'Merged PDF'
        return response 
        # Option 2: Write to disk
        with open("merged_pdf.pdf", "w") as fp:
            fp.write(pdf_merged_buffer.getvalue())
    

提交回复
热议问题