0

使用另一个stackoverflow问题和答案,我能够找到部分解决我正在尝试做的代码合并PDF文件

但是,此修改后的代码会导致两个 PDF 的内容相互重叠。我正在尝试堆叠它们或垂直连接结果:

例子:

  • PDF1 目录 -> “Hello World”
  • PDF2 目录 -> “我是比尔”

下面的代码导致以下重叠图像:

  • 结果无效

所需的结果如下所示:

  • 期望的结果

使用的代码导致图像重叠:

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')

提前致谢!

4

2 回答 2

-1

这是一个使用PyPDF2库的示例:

  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)

这是另一个使用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)

导入可能看起来像:

import time
import pdfkit
import os
from PyPDF2 import PdfFileMerger, PdfFileReader
from werkzeug.utils import secure_filename
from pikepdf import Pdf
于 2021-12-01T16:27:46.813 回答
-1

你有没有尝试过


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)

于 2021-12-01T16:13:42.560 回答