Convert fillable python pdf to readonly like browser save_as_pdf functionality

120 views Asked by At

I'm filling my pdf form fields using python pypdf library and sending it to DocuSign for signing.Docusign converts the pdf fields to read-only but shrinks the formatting for it which is not what I want. Since my pdf is fillable form, if I fill it in chrome and use the chrome save_as_pdf feature. It makes a read-only pdf of my form, which DocuSign reads correctly without ruining the formatting of the pdf. Even when I try to change my pdf to readonly using pypdf writer flags, the pdf I get back has incorrect formatting.

This is how the pdf looks if I use chrome save_as and then upload to docusign

When I make it readonly using pypdf, this is how it looks

I've tried all the possible pdf libraries to convert my pdf to a readonly pdf like the browser save_as_pdf functionality but all has been in vain. If I can convert my local file the same way as the browser and then push the bytes to DocuSign I'm hopeful it would work fine.

#Code to fill pdf and write to file
reader = PdfReader("wphg_form.pdf")
# reader = PdfReader("form.pdf")
writer = PdfWriter()
writer.append(reader)
writer.update_page_form_field_values(writer.pages[0],  fields={
                "DEPOT_NR": "1111111111",
            })
with open("filled-out.pdf", "wb") as output_stream:
    writer.write(output_stream)
2

There are 2 answers

0
Inbar Gazit On

Don't use PDF form fields, it won't work for this situation.

For something like that you will need to create 10 text fields, and not just one.

Basically, you can do this using a loop that adds them one after the other and pushes each field about 40 pixels to the right (you will need to figure out the exact number with trial and error).

You then fill the values and make it read only if you want that

Here is rough code to get you the idea:

for x in range(10):
  text_box = Text(
  anchor_string="Depot", anchor_units="pixels",
  anchor_y_offset="-10", anchor_x_offset=300+x*35,
  font="helvetica", font_size="size11",
  bold="true", value=args["signer_name"],
  locked="true", tab_id="Textbox" + x,
  tab_label="Textbox" + x)
  signer.tabs.append(text_box) 
3
Larry K On

Since you don't want the DocuSign signer to be able to change the fields, best is to "flatten" the PDF.

There appear to be several options for doing so: search results

~ ~ ~

A prior idea: setting transformPdfFields to false

...but then the contents of the PDF form fields aren't shown.