chinapandaman / PyPDFForm

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

PPF-706: PyPDFForm does not handle fields in forms rotated 90 degrees #706

Open smhegde-1979 opened 2 months ago

smhegde-1979 commented 2 months ago

Version

PyPDFForm=1.4.31

Issue Description

Background - We use fillable pdfs that have been created by rotating a default form clockwise by 90 degree. This has been done because the "landscape" mode can accommodate all the columns we need on a single printed page. So far we have been using fdggen and then flatten using pdftk and this is able to handle the form, and fields, that are thus rotated. PyPDFForm, does fill the form however it does not handle the rotation of the text. Also, please notice the \n character in the data. In the output generated by PyPDFForm, the newline character is replaced with a visual representation. I don't know if this would be converted to newlines in the pdf automatically if this issue is fixed.

Code Snippet

from PyPDFForm import PdfWrapper

filled = PdfWrapper("sample_form.pdf").fill(
    {"order_nbr" : "Order001\nOrder002",
     "cntr_nbr" : "Container001\nContainer002"
     }
    )

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

PDF Form Template

sample_form.pdf

Screenshots (if applicable)

output.pdf

chinapandaman commented 2 months ago

Hey, sorry for a rather late response. As you can see from my commit history I have been quite busy recently with other stuffs and haven't spent much time developing this project.

Anyway, this is not a scenario I have thought about when I initially designed PdfWrapper, so I will need sometime to look at how I can implement this. In the meantime, I tried your snippet with FormWrapper (link to docs) and it filled the form fine. So I'd say for a temp measure try using that instead. It's a trim down version of the PdfWrapper but it still supports the two basic filling and flattening functionalities.

smhegde-1979 commented 2 months ago

Thank you! I will look into FormWrapper. Also, no apologies necessary. Thank you for all that you do!

On Mon, Aug 12, 2024, 9:30 AM Jinge Li @.***> wrote:

Hey, sorry for a rather late response. As you can see from my commit history I have been quite busy recently with other stuffs and haven't spent much time developing this project.

Anyway, this is not a scenario I have thought about when I initially designed PdfWrapper, so I will need sometime to look at how I can implement this. In the meantime, I tried your snippet with FormWrapper (link to docs https://chinapandaman.github.io/PyPDFForm/simple_fill/) and it filled the form fine. So I'd say for a temp measure try using that instead. It's a trim down version of the PdfWrapper but it still supports the two basic filling and flattening functionalities.

— Reply to this email directly, view it on GitHub https://github.com/chinapandaman/PyPDFForm/issues/706#issuecomment-2284001280, or unsubscribe https://github.com/notifications/unsubscribe-auth/BKLSJMUGOU6ER3XGBD7MTLDZRC2GRAVCNFSM6AAAAABMDEZP6OVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEOBUGAYDCMRYGA . You are receiving this because you authored the thread.Message ID: @.***>

smhegde-1979 commented 2 months ago

Thank you for the feedback! FormatWrapper works to a certain degree. As long as I do not flatten the result, the form fills correctly. The rendering of newlines was an issue with the sample_form, i did not make the textbox allow multi-lines. I have corrected and update the attachment, "sample_form.pdf"

There is still an issue with this solution. It leaves the pdf editable this is not ok for us in certain reports where the information generated using our product needs to remain as is. However, using flatten=True in FormWrapper renders the text not visible in the final pdf. I am digging around the flatten code to see why this maybe happening. At the moment, I do not know the PDF specification so I need to start from there. I will dig around some more and provide an update, hopefully soon.

chinapandaman commented 2 months ago

So I finally spent some time looking at this yesterday. Unfortunately it is not an easy change to support landscape mode for the PdfWrapper. It will require major overhauls of both the coordinate calculation and draw logic.

For now I suggest stick with FormWrapper. As far as what you said about when flatten=True, what PDF viewing app are you using? If it's Adobe Acrobat have you tried adobe mode?

smhegde-1979 commented 2 months ago

I view the final output in a browser. I only use Adobe Acrobat for preparing the initial editable form. I can definitely confirm its not related to the viewing software or viewing modes, because my previous approach which uses fdfgen and then pdftk-java produces the right results when viewed using the same browser (or other PDF viewers). FYI, the main reason we are evaluating PyPDFForm and underlying pypdf as an alternative is to get closer to a python implementation and remove the Java footprint from our tech-stack.

However, I do understand the complications you mention. I was digging around myself and did not find any quick solutions either. I will try to continue with what we have for the moment but I will keep looking at this as time permits.

chinapandaman commented 2 months ago

Umm not sure what browser you are using. Chrome seems to work for me.

Snippet:

from PyPDFForm import FormWrapper

filled = FormWrapper("sample_form.pdf").fill(
    {"order_nbr" : "Order001\nOrder002",
     "cntr_nbr" : "Container001\nContainer002"
     },
    flatten=True
    )

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

output.pdf

Screenshot: Screenshot from 2024-08-20 21-19-53

smhegde-1979 commented 2 months ago

Ok Yes, I can confirm Chrome on Linux and Windows renders this as you have shown. Firefox on Linux and Windows does not render the Orders or Containers. More surprisingly, even Adobe Acrobat Does not render the fields.

image image

I can definitely advise people to use Chrome, but the choice of end user's Browser, is unfortunately not in my control. However, at least, I still have a recommendation for them. Thank you!