Home > front end >  How can I merge two PDF files without overlapping content
How can I merge two PDF files without overlapping content

Time:12-02

Using another stackoverflow question & answer, I was able to locate code which partially resolves what I am trying to do Invalid Result

Desired results would look as follows:

  • Desired Result

Code Used resulting in overlapping imge:

import pdfrw
dirPATH = r'c:\users\<username>\projects\concat_pdfs'
pdf1 = os.path.join(dirPATH, 'PDF1.pdf')
pdf2 = os.path.join(dirPATH, 'PDF2.pdf')

def concat_pdfs(pdf1, pdf2, output):
    form = pdfrw.PdfReader(pdf1)
    olay = pdfrw.PdfReader(pdf2)
    
    for form_page, overlay_page in zip(form.pages, olay.pages):
        merge_obj = pdfrw.PageMerge()
        overlay = merge_obj.add(overlay_page)[0]
        pdfrw.PageMerge(form_page).add(overlay).render()
        
    writer = pdfrw.PdfWriter()
    writer.write(output, form)
    
concat_pdfs(section1, section2, 'result.pdf')

Thanks in advance!

CodePudding user response:

Have you tried


def combine_pdfs(dir_path1, dir_path2, save_path):
    pdf1 = pdfrw.PdfReader(dir_path1)
    pdf2 = pdfrw.PdfReader(dir_path2)
    pdf_writer = pdfrw.PdfWriter()
    for page in pdf1.pages:
        pdf_writer.addpage(page)
    for page in pdf2.pages:
        pdf_writer.addpage(page)
    pdf_writer.write(save_path)

CodePudding user response:

Here's an example using PyPDF2 library:

  merger = PdfFileMerger()
  for filename in files:
    f = files[filename]
    loc = "/tmp/"   secure_filename(filename).replace(".pdf", "")   "_"   str(time.time())   ".pdf"
    f.save(loc)
    f.close()
    reader = PdfFileReader(loc, "rb")
    merger.append(reader)
  dest = "/tmp/merged_"   str(time.time())   ".pdf"
  merger.write(dest)

Here is another using pike pdf:

  pdf = Pdf.new()
  for filename in files:
    f = files[filename]
    loc = "/tmp/"   secure_filename(filename).replace(".pdf", "")   "_"   str(time.time())   ".pdf"
    f.save(loc)
    f.close()
    reader = Pdf.open(loc)
    pdf.pages.extend(reader.pages)
  dest = "/tmp/merged_"   str(time.time())   ".pdf"
  pdf.save(dest)

Imports might look something like:

import time
import pdfkit
import os
from PyPDF2 import PdfFileMerger, PdfFileReader
from werkzeug.utils import secure_filename
from pikepdf import Pdf
  • Related