How to make fillable pdf to read only pdf using python pdfrw package

1.8k Views Asked by At

I am using pdfrw package to fill pdf file and i am looking for make the pdf to read only but i am not able to create can any one help me with this

def write_fillable_pdf(input_pdf_path, output_pdf_path, data_dict):
template_pdf = PdfReader(input_pdf_path)
annotations = template_pdf.pages[0][ANNOT_KEY]
for annotation in annotations:
    if annotation[SUBTYPE_KEY] == WIDGET_SUBTYPE_KEY:
        if annotation[ANNOT_FIELD_KEY]:
            key = annotation[ANNOT_FIELD_KEY][1:-1]
            if key in data_dict.keys():
                annotation.update(
                    PdfDict(V='{}'.format(data_dict[key]))
                )
PdfWriter().write(output_pdf_path, template_pdf)
1

There are 1 best solutions below

1
On

According to documentation in page 532 set Ffto 1 gives your desired behaviour, then your code will change as follows:

def write_fillable_pdf(input_pdf_path, output_pdf_path, data_dict):
template_pdf = PdfReader(input_pdf_path)
annotations = template_pdf.pages[0][ANNOT_KEY]
for annotation in annotations:
    if annotation[SUBTYPE_KEY] == WIDGET_SUBTYPE_KEY:
        if annotation[ANNOT_FIELD_KEY]:
            key = annotation[ANNOT_FIELD_KEY][1:-1]
            if key in data_dict.keys():
                annotation.update(
                    PdfDict(V='{}'.format(data_dict[key]),Ff=1)
                )
PdfWriter().write(output_pdf_path, template_pdf)