Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formwrapper cannot fill Checkbox #558

Closed
73VW opened this issue Apr 8, 2024 · 8 comments · Fixed by #564
Closed

Formwrapper cannot fill Checkbox #558

73VW opened this issue Apr 8, 2024 · 8 comments · Fixed by #564

Comments

@73VW
Copy link

73VW commented Apr 8, 2024

Hello there,

It seems like the FormWrapper cannot fill checkbox.

Sample code to test with the sample pdf:

from PyPDFForm import FormWrapper

filled = FormWrapper("sample_template.pdf").fill(
    {
        "check": True,
    },
    flatten=False
)

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

First checkbox stays unchecked.

73VW added a commit to 73VW/PyPDFForm that referenced this issue Apr 8, 2024
@chinapandaman
Copy link
Owner

Hello, first of all thanks for contributing!

Before we get into the actual coding part, I'm not sure if this issue is reproducible:

  1. On my Ubuntu 22.04 LTS machine, I spun up a new virtual environment, deleted all pip caches, ran pip install -U PyPDFForm, downloaded the template you linked, and ran the snippet your posted in the thread. And this is the generated output. I opened it in both Chrome and Document Viewer and in both cases the first checkbox are checked.
  2. There are test cases for FormWrapper, for example this one. And there are also expected PDFs that are used to help asserting these test cases. For the test case I just linked this is its expected PDF and you can see the checkboxes that are meant to be checked are both checked. So if FormWrapper fails to check appropriate checkboxes these tests will fail, which they don't as of now.
  3. I wonder if you are using a PDF browser I have never seen? Different PDF browsers display PDF forms differently. For example the expected PDF I linked in 2), GitHub won't display texts that are put into the text fields. But if you download the PDF and open it in Chrome Chrome will display the texts correctly. If you speculate this might be the issue I suggest you flatten the PDF when you fill it and usually flattening will generate consistent results across different PDF browsers.

@73VW
Copy link
Author

73VW commented Apr 10, 2024

Hello @chinapandaman,

I am using Adobe Acrobat reader to display the pdf. This seems weird that it would work on Chrome but not with Acrobat. Maybe that Acrobat is more severe regarding pdf structure than Chrome?
I will make a fresh install and try to reproduce the issue and attach a screenshot.

@chinapandaman
Copy link
Owner

Hmm interesting OK. I mean I don't have Acrobat on my current machine but I believe you. Plus the changes you are making are minor.

So here are some next steps:

  1. I created a new branch and would love you to first close your PR and re-open it so that it targets this new branch instead of master.
  2. I will then have to do some chores on the new branch to cleanup your changes a bit, as well as adding test cases for these new changes.
  3. Once I'm done with that, I will create a new PR from the new branch to master and tag you as a reviewer. I would also love you to verify couple newly generated test case PDFs for me on Acrobat that with the new changes their checkboxes are indeed checked.
  4. Once confirmed, I will merge the PR and bump a new release out.

Let me know if you have any question regarding the above.

@chinapandaman
Copy link
Owner

Actually nvm about what I said above. I will just put up a PR in that branch.

@chinapandaman
Copy link
Owner

Hey, so I migrated your changes into #564 with some cleanups. Feel free to go give it a review before I move further with it.

I also updated old tests with the changes. Could you verify that for example this PDF now has checkbox checked when opened in Adobe Acrobat?

@73VW
Copy link
Author

73VW commented Apr 12, 2024

Hey buddy!

So I've a made a few tests, starting back with a fresh install in a new venv. I am using python3.11. Your package has been installed through pip and its version is 1.4.17.
I've also re-downloaded the sample pdf and the prescription pdf.

Using this code:

from PyPDFForm import FormWrapper

filled = FormWrapper("sample_template.pdf").fill(
    {
        "test": "Test 1",
        "check": True,
    },
)

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

filled = FormWrapper("verordnungsformular_4f.pdf").fill(
    {
        "LiMA": True,
        "NomPrénom 1": "73VW"
    },
)

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

In Acrobat reader, here's the sample pdf without modifications:

image

After filling it with the non-patched version of your package:

image

It seems that now the checkbox is working but not the text field. I don't understand what's going on here.

Opening the same document in a chromium based browser seem to work:

image

Testing the same with the prescription form:

No modifications, Acrobat:

image

After filling it, Acrobat:

image

After filling it, Chromium:

image

I can't reproduce the bug with the checkboxes but now it happens with the text boxes. Could this be a problem in acrobat? I don't really know. I can't remember updating it this week so this might not be related to it.

Feel free to close this issue as this might not be a problem anymore. If you wish, I can open a new one regarding the text fields.

Sorry for the inconvenience.
Best

@73VW
Copy link
Author

73VW commented Apr 12, 2024

I've downloaded your sample filled and here's the result:

image

However, it seems that the value is present, just not shown. If I click in the field, I get this:

image

I've crossed checked the two pdfs in my last comment and it looks the same. If I click in the field, value appears!

@chinapandaman
Copy link
Owner

So a bit more about the text not showing up unless clicked issue. This is actually less surprising than checkbox not showing up. If you take a look at how GitHub displays say this PDF you will see that texts don't show up there either.

To add a bit more context, when I initially started writing this library, it worked similar to how FormWrapper works nowadays. See here: https://github.com/chinapandaman/PyPDFForm/blob/v0.0.1/PyPDFForm/pdf.py#L81

And the reason why what is now PdfWrapper eventually took precedence was because the same behavior on the text field, where the text that got filled won't show up unless clicked, existed back then. Except it affected many other PDF viewers, including Chrome and Document Viewer (the default PDF app on Ubuntu). Ultimately this behavior across all PDF viewers was what made me decided to deprecate what is now FormWrapper and moved forward with PdfWrapper.

So you may ask, why is FormWrapper back now? Well recently I was enlightened by a Reddit user. His code snippet for filling PDF forms in place will result in PDFs without the weird text behavior, at least when opened through Chrome and Document Viewer. In the end we found out the magic was this line. The /AP key, when set with the text that's set to the /V key, will render the text fields correctly. I thought I found the silver bullet for this issue, and only recently brought back FormWrapper.

So enough history lessons. Here are some next steps:

  1. I will still be merging PPF-558: fix checkbox not display checked on Adobe Acrobat #564. I know based on your most recent tests the checkboxes aren't broken even without this patch. But since you did initially observe this, adding on top of that the changes don't break any existing test cases, I think it won't hurt to merge it.
  2. I will close this thread as well as your PR. After reading what I said above if you are still interested in discussing the text field issue let's open another thread.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants