chinapandaman / PyPDFForm

:fire: The Python library for PDF forms.
https://chinapandaman.github.io/PyPDFForm/
MIT License
381 stars 15 forks source link

Boxes disappear on fill #704

Open AaronWGoh opened 1 month ago

AaronWGoh commented 1 month ago

Version

PyPDFForm==1.4.31

Issue Description

The PDF form field boxes disappear and the styling of the entire pdf changes with the basic out of box documentation.

Code Snippet

input_pdf = <file_path>
from PyPDFForm import PdfWrapper

wrapper = PdfWrapper(input_pdf)
data = wrapper.sample_data

keys_to_delete = [key for key in data if "Signature" in key]
for key in keys_to_delete:
    del data[key]

filled = wrapper.fill(
    {
        "VA_File_Number[0]": "123456789",
        "Veteran_Service_Member_First_Name[0]": "Samuel",
    },
)

with open("output.pdf", "wb+") as output:
    output.write(filled.read())

PDF Form Template

file.pdf Above is pdf used to replicate example

Screenshots (if applicable)

Initial pdf looks like

image

After the code is run, it becomes

image
chinapandaman commented 1 month ago

Hey, thanks for posting, and sorry for a rather late response.

This is actually an intended behavior of PdfWrapper. The "box like looks" are I believe part of the widget styles, which as described at the top of this doc, all widgets will be flattened after filling. Now in the case of PdfWrapper flattening widgets means removing them and only leaving the text, which is why there are no more box styles.

If you still want to persist the box styles, I suggest taking a look at the FormWrapper, documented here. Below is a result of me using your snippet except using FormWrapper:

output.pdf

AaronWGoh commented 1 month ago

@chinapandaman I saw in the source code that PdfWrapper inherits from FormWrapper. I'd prefer to use the PdfWrapper as it appears there are other features tied to it such as merging pdfs & signing pdfs with images. Without digging too deep, are there ways around flattening the widgets in PdfWrapper?

chinapandaman commented 1 month ago

Unfortunately no.