pmaupin / pdfrw

pdfrw is a pure Python library that reads and writes PDFs
Other
1.84k stars 271 forks source link

filling forms with persian or arabic #203

Open shs2010 opened 4 years ago

shs2010 commented 4 years ago

i'm new with python. I have done a project filling pdf form with pdfrw in english but now i want to fill it with arabic or persian character . I cant see the characters in generated pdf before clicking on the form. is there a solution to fix it ? def write_fillable_pdf(input_pdf_path, output_pdf_path, data_dict): ANNOT_KEY = '/Annots' ANNOT_FIELD_KEY = '/T' # name ANNOT_FORM_type = '/FT' # Form type (e.g. text/button) ANNOT_FORM_button = '/Btn' # ID for buttons, i.e. a checkbox ANNOT_FORM_text = '/Tx' # ID for textbox SUBTYPE_KEY = '/Subtype' WIDGET_SUBTYPE_KEY = '/Widget' try: template_pdf = pdfrw.PdfReader(input_pdf_path) i =0 for Page in template_pdf.pages: if Page[ANNOT_KEY]: for annotation in Page[ANNOT_KEY]: if annotation[ANNOT_FIELD_KEY] and annotation[SUBTYPE_KEY] == WIDGET_SUBTYPE_KEY : key = annotation[ANNOT_FIELD_KEY][1:-1] # Remove parentheses if key in data_dict.keys(): i += 1 if annotation[ANNOT_FORM_type] == ANNOT_FORM_button: annotation.update( pdfrw.PdfDict( V=pdfrw.PdfName(data_dict[key]) , AS=pdfrw.PdfName(data_dict[key]) )) elif annotation[ANNOT_FORM_type] == ANNOT_FORM_text: annotation.update( pdfrw.PdfDict( V=data_dict[key] , AP=data_dict[key] ) ) if i>0: template_pdf.Root.AcroForm.update(pdfrw.PdfDict(NeedAppearances=pdfrw.PdfObject('true'))) pdfrw.PdfWriter().write(output_pdf_path, template_pdf) return True except Exception as ex: print(ex) return False

omaralbalolly commented 3 years ago

Hello, I have the same problem did you find a solution?