1

I'm using pdfrw python library to fill pdf fields from a web form. Then I have to pass it to an e-signature webservice, but they want the pdf to be non editable (flatten).

I've tried to flatten it using Adobe, it works but then I can't fill it through my python code..

import pdfrw

template_pdf = pdfrw.PdfReader(input_pdf_path)
    annotations = template_pdf.pages[0]['/Annots']
    for annotation in annotations:
        if annotation['/Subtype'] == '/Widget':
            if annotation['/T']:
                key = annotation['/T'][1:-1]
                if key in data_dict.keys():
                    annotation.update(
                        pdfrw.PdfDict(V='{}'.format(data_dict[key]))
                    )
template_pdf.Root.AcroForm.update(pdfrw.PdfDict(NeedAppearances=pdfrw.PdfObject('true')))
pdfrw.PdfFileWriter().write(output_pdf_path, template_pdf)

My idea is to flatten the pdf after fill it, using pdfrw, but I really can't figure how. If anyone has encountered this problem and has a solution, it would help me a lot.

paul.ruelle
  • 132
  • 2
  • 11

1 Answers1

2
if key in data_dict.keys():
    annotation.update(pdfrw.PdfDict(V='{}'.format(data_dict[key])))
    annotation.update(pdfrw.PdfDict(Ff=1)) # This set the annotation as as not editable
  • 1
    Please add more info about how this code helps the OP. – Mehraban Dec 04 '19 at 15:18
  • 1
    I'm having a similar problem. Using the Ff=1 flag does set the annotation as read only but not for all viewers. For example on Foxit Reader this works but on MacOS Preview the fields can still be edited. Is there a workaround for this? – Sebastian M Feb 22 '21 at 11:13