Return PDF generated with FPDF in Flask

2024/9/21 20:31:36

I can generate a PDF with an image using the code below. How can I return the generated PDF from a Flask route?

from fpdf import FPDF
pdf = FPDF()
img = input('enter file name')
g = img + '.jpg'
pdf.add_page()
pdf.image(g, 50, 50)
pdf.output(img + '.pdf', 'F')
Answer

Use make_response to create a response with the PDF data output as a string (dest='S'). Encode the output as latin-1, otherwise Flask will encode it as UTF-8 and it may not be valid. Set the Content-Disposition and Content-Type headers to tell the browser to download/handle a PDF file. Return the constructed response.

from flask import make_response@app.route('/jpg_to_pdf/<name>')
def jpg_to_pdf(name):pdf = FPDF()pdf.add_page()pdf.image(os.path.join(app.instance_path, name + '.jpg'), 50, 50)response = make_response(pdf.output(dest='S').encode('latin-1'))response.headers.set('Content-Disposition', 'attachment', filename=name + '.pdf')response.headers.set('Content-Type', 'application/pdf')return response

This example assumes the images are in the instance folder, modify as necessary to point to where the images actually are.

https://en.xdnf.cn/q/72016.html

Related Q&A

Tensorflow not found on pip install inside Docker Container using Mac M1

Im trying to run some projects using the new Mac M1. Those projects already work on Intel processor and are used by other developers that use Intel. I am not able to build this simple Dockerfile: FROM …

Fast fuse of close points in a numpy-2d (vectorized)

I have a question similar to the question asked here: simple way of fusing a few close points. I want to replace points that are located close to each other with the average of their coordinates. The c…

I use to_gbq on pandas for updating Google BigQuery and get GenericGBQException

While trying to use to_gbq for updating Google BigQuery table, I get a response of:GenericGBQException: Reason: 400 Error while reading data, error message: JSON table encountered too many errors, givi…

Something wrong with Keras code Q-learning OpenAI gym FrozenLake

Maybe my question will seem stupid.Im studying the Q-learning algorithm. In order to better understand it, Im trying to remake the Tenzorflow code of this FrozenLake example into the Keras code.My code…

How to generate month names as list in Python? [duplicate]

This question already has answers here:Get month name from number(18 answers)Closed 2 years ago.I have tried using this but the output is not as desired m = [] import calendar for i in range(1, 13):m.a…

Getting ERROR: Double requirement given: setuptools error in zappa

I tried to deploy my Flask app with zappa==0.52.0, but I get an error as below;ERROR: Double requirement given: setuptools (already in setuptools==52.0.0.post20210125, name=setuptools) WARNING: You are…

PySpark - Create DataFrame from Numpy Matrix

I have a numpy matrix:arr = np.array([[2,3], [2,8], [2,3],[4,5]])I need to create a PySpark Dataframe from arr. I can not manually input the values because the length/values of arr will be changing dyn…

RunTimeError during one hot encoding

I have a dataset where class values go from -2 to 2 by 1 step (i.e., -2,-1,0,1,2) and where 9 identifies the unlabelled data. Using one hot encode self._one_hot_encode(labels)I get the following error:…

Is there a Mercurial or Git version control plugin for PyScripter? [closed]

Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines. It is not currently accepting answers.We don’t allow questi…

How to make a color map with many unique colors in seaborn

I want to make a colormap with many (in the order of hundreds) unique colors. This code: custom_palette = sns.color_palette("Paired", 12) sns.palplot(custom_palette)returns a palplot with 12 …