I'm trying to serve a dynamic PDF to my users through Flask. They fill a form to get some calculations presented, but they would like to auto-fill the values to a PDF-template and then print it on paper.
I got it working locally, just filling the values to a PDF-template, but I have no clue on how I'd work it into my Flask app. What I got locally is I've got a template PDF-file, a dictionary with the values, a function to fill the values to the template and write it out to a new PDF-file. Thou when hosting it with Flask, I want it to dynamically serve them the new PDF created by the function instead of writing it to a file.
I'm using the pdfrw library for the pdf-modification, I realise I might need to use make_reponse or IO or a combination. Just dont know how to get the result from pdfrw NOT to a file. Or where to start.
Here's the function:
import os
import pdfrw
data_dict = #just a plain dict corresponding with the temp.pdf keys
PDF_TEMPLATE_PATH = 'temp.pdf'
PDF_OUT_PATH = 'out.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]:
key = annotation[ANNOT_FIELD_KEY][1:-1]
if key in data_dict.keys():
annotation.update(
pdfrw.PdfDict(V='{}'.format(data_dict[key]))
)
pdfrw.PdfWriter().write(output_pdf_path, template_pdf)
if __name__ == '__main__':
write_fillable_pdf(PDF_TEMPLATE_PATH, PDF_OUT_PATH, data_dict)
So I guess, how do I get this line
pdfrw.PdfWriter().write(output_pdf_path, template_pdf)
to be able to serve with eg. make_reponse or IO or likely? I'll just make a button-href to a /pdf route in flask for this when I get it working.
Sorry I'm rather new to this, trying over my head to meet the wishes of my users.
Thank you for any guidance or direction.