'How to reduce size of PDF generated by a Flask server
Good evening everyone! I have a very big problem: I'm building a webapp, which the server-side is a Python server using Flask and the client-side are some simple HTML files.
In the server, I have a static PDF file, which has a fillable form inside. I correctly read this file and fill its form using Python.
The problem is that the template file size is only 144 KByte, while the generated PDF is about 28MB, even if the unique thing I do in the file is to fill the form with some text.
At this point, my question is: is there a library, or a different way, to reduce the size of the file immediatly after the form filling?
I attach some code:
f = PdfFileReader('static/ModelloFattura.pdf')
ff = f.getFields()
page = f.getPage(0)
writer = PdfFileWriter()
writer.appendPagesFromReader(f)
set_need_appearances_writer(writer)
#-----------------------------------
# Doing things to fill the form
#-----------------------------------
fout = open("static/tmp.pdf", "ab")
writer.write(fout)
fout.close()
with open("static/tmp.pdf", 'rb') as file:
binaryData = file.read()
In the end I send as response binaryData to front-end. In the end, the client side correctly retrieve binary data show it in browser as PDF, but you can imagine how long does it takes to do this...
response = make_response(binaryData)
response.headers.set("Content-Type", "application/pdf")
return response
I hope someone can help me!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
