pypdf icon indicating copy to clipboard operation
pypdf copied to clipboard

Pdf form overlap issue

Open Rob1080 opened this issue 12 years ago • 12 comments

When merging 2 pdfs, where one has form elements and the other does not; the "check box" form element overwrites any text that is present.

Is there a workaround for this?

The example below shows the text "blah" overlapped by check box element. check box form element overlap text

Rob1080 avatar Mar 28 '14 14:03 Rob1080

Any feedback on this?

Rob1080 avatar Apr 02 '14 13:04 Rob1080

Thanks for the example image - I was able to reproduce this issue.

If you interchange the calling pageObject and the argument pageObject, you will get a better result with text visible over form fields

For example, given a formPDF and textPDF as PdfFileReader objects,

formPage = formPDF.getPage(0)
textPage = textPDF.getPage(0)

textPage.mergePage(formPage)

This output won't look pretty (will look like your above example). Instead, swap pageObjects:

formPage.mergePage(textPage)

This should place text on top of form elements.

mstamy2 avatar Apr 02 '14 22:04 mstamy2

Thanks Matthew. Strangely enough, in this instance, I had to switch to this: textPage.mergePage(formPage) in order for it work correctly.

Is there a concept of "layering" in pdf markup? After making the above change, images in the "formPdf" now overlap the text. Here's an example: image

This isn't a train smash, ideally I'd just like to set the "z-index" (if there were such a thing for pdfs) of text higher than anything else.

Rob1080 avatar Apr 03 '14 07:04 Rob1080

@Rob1080 I know it's been a long time. Do you happen to have an example (code + PDF) that shows the issue with the latest PyPDF2 version? Can you create one?

MartinThoma avatar Jul 10 '22 04:07 MartinThoma

I'm uncertain if this really is an issue. Without example files, I don't think we can work on it.

MartinThoma avatar Aug 06 '22 06:08 MartinThoma

@Rob1080 @mstamy2 @mstamy2 @rrthomas @avm // Here is the code of two PDFs merged on one page (overlapping) In Node js

const pdftk = require("node-pdftk"); const fs = require("fs"); const path = require("path");

const PdfController = { pdf: async (req, res) => { try { const pdfFile1Path = "/controllers/blank_GS_P5-PED08.pdf"; const pdfFile2Path = "/controllers/content_GS_P5-PED08.pdf"; const outputFilePath = "/controllers/merged_GS_P5-PED08.pdf";

  // Set a temporary directory where your Node.js process has write 
  // permissions using the tmpDir option
  const tmpDir = "/tmp"; // Change '/tmp' to an appropriate directory

  // Read the input PDF files
  const pdfFile1Buffer = fs.readFileSync(pdfFile1Path);
  const pdfFile2Buffer = fs.readFileSync(pdfFile2Path);

  // Use the stamp operation to overlay pdfFile2 onto pdfFile1
  await pdftk
    .input(pdfFile1Buffer)
    .stamp(pdfFile2Buffer)
    .output(outputFilePath);

  // Send a success response with the output PDF path
  res
    .status(200)a
    .json({
      message: "PDFs merged successfully with overlap",
      outputPath: outputFilePath,
    });
} catch (error) {
  console.error("Error merging PDFs:", error);
  res.status(500).json({ error: "An error occurred while merging PDFs" });
}

}, };

module.exports = PdfController;

amitkprimo avatar Sep 13 '23 07:09 amitkprimo

How is this relevant for pypdf? If ever, one probably wants to use a pdftk wrapper for Python, but as mentioned by @MartinThoma, this might be fixable in pypdf as well if we have a corresponding pypdf code snippet as well as the failing PDF.

stefan6419846 avatar Sep 13 '23 07:09 stefan6419846

How is this relevant for pypdf? If ever, one probably wants to use a pdftk wrapper for Python, but as mentioned by @MartinThoma, this might be fixable in pypdf as well if we have a corresponding pypdf code snippet as well as the failing PDF.

You can use PyPDF2 I have Solutions also in Python here is code import PyPDF2 def merge_pdfs_with_data_filling(primary_pdf_path, data_pdf_path, output_pdf_path): primary_pdf = PyPDF2.PdfReader(open(primary_pdf_path, "rb")) data_pdf = PyPDF2.PdfReader(open(data_pdf_path, "rb")) output_pdf = PyPDF2.PdfWriter()

num_pages = max(len(primary_pdf.pages), len(data_pdf.pages))

for page_num in range(num_pages):
    primary_page = primary_pdf.pages[page_num] if page_num < len(primary_pdf.pages) else None
    data_page = data_pdf.pages[page_num] if page_num < len(data_pdf.pages) else None

    if primary_page and data_page:
        output_page = primary_page
        output_page.merge_page(data_page)  # Merge content from data_page into output_page
    elif primary_page:
        output_page = primary_page
    else:
        output_page = data_page

    output_pdf.add_page(output_page)

with open(output_pdf_path, "wb") as output_file:
    output_pdf.write(output_file)

Paths to the primary PDF, data PDF, and output PDF

primary_pdf_path = "blank_GS_P5-PED08.pdf" data_pdf_path = "content_GS_P5-PED08.pdf" output_pdf_path = "merged_GS_P5-PED08.pdf"

Merge PDFs with data filling

merge_pdfs_with_data_filling(primary_pdf_path, data_pdf_path, output_pdf_path) print(f"PDFs merged and filled with data saved as '{output_pdf_path}'.")

amitkprimo avatar Sep 13 '23 10:09 amitkprimo

@amitkprimo please don't tag a bunch of people on an issue like this, especially people like me who have nothing to do with the issue and aren't even project maintainers!

rrthomas avatar Sep 13 '23 11:09 rrthomas

@amitkprimo I don't understand what you're trying to tell us. Is there a problem? Why do you paste JavaScript code in here? How is it related to this issue?

MartinThoma avatar Sep 13 '23 12:09 MartinThoma

@amitkprimo I don't understand what you're trying to tell us. Is there a problem? Why do you paste JavaScript code in here? How is it related to this issue?

I just explained with code how to merge two PDFs into one page in Python or Node. I'm trying to find a solution, and I found it, so I thought it might help other people. If you don't like it, please ignore it.

amitkprimo avatar Sep 15 '23 06:09 amitkprimo

@amitkprimo, what you wrote here could be a good Stack Exchange question and answer; if you post it there, it's much more likely to be found by people looking to solve the same problem.

rrthomas avatar Sep 15 '23 09:09 rrthomas