|
#! /usr/bin/python |
|
|
|
import os |
|
import pdfrw |
|
|
|
|
|
INVOICE_TEMPLATE_PATH = 'delay.pdf' |
|
INVOICE_OUTPUT_PATH = 'delay_pdfrw2.pdf' |
|
|
|
|
|
ANNOT_KEY = '/Annots' |
|
ANNOT_FIELD_KEY = '/T' |
|
ANNOT_VAL_KEY = '/V' |
|
ANNOT_RECT_KEY = '/Rect' |
|
SUBTYPE_KEY = '/Subtype' |
|
WIDGET_SUBTYPE_KEY = '/Widget' |
|
|
|
|
|
def write_fillable_pdf(input_pdf_path, output_pdf_path, data_dict): |
|
template_pdf = pdfrw.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]: |
|
annotation.update(pdfrw.PdfDict(AP='')) |
|
key = annotation[ANNOT_FIELD_KEY][1:-1] |
|
if key in data_dict.keys(): |
|
annotation.update( |
|
pdfrw.PdfDict(V='{}'.format(data_dict[key])) |
|
) |
|
watermark = 'sign.pdf' |
|
wmark = pdfrw.PageMerge().add(pdfrw.PdfReader(watermark).pages[0])[0] |
|
page = template_pdf.pages[0] |
|
pdfrw.PageMerge(page).add(wmark, prepend=True).render() |
|
pdfrw.PdfWriter().write(output_pdf_path, template_pdf) |
|
|
|
data_dict = {'Student Name': 'asdf', |
|
'Advisor Name': 'Prof. asdf ', |
|
'Email Address': 'asdf', |
|
'Date Submitted': '03 Jan 2019', |
|
'Research Topic': 'asdf', |
|
'Advisor': 'Prof. asdf', |
|
"Advisor's Nominee": 'Prof. asdf', |
|
"Student's Nominee": 'Prof. sadf', |
|
"Dean's Nominee": None, |
|
'Month and Year': '09/2019', 'Rationale': "asdf", |
|
"Advisor's Comments": None} |
|
|
|
|
|
if __name__ == '__main__': |
|
write_fillable_pdf(INVOICE_TEMPLATE_PATH, INVOICE_OUTPUT_PATH, data_dict) |
|
~ |
|
~ |