2

There is a request has been made to the server using Python's requests module:

requests.get('myserver/pdf', headers)

It returned a status-200 response, which all contains PDF binary data in response.content

Question

How does one create a PDF file from the response.content?

2
  • I suggest trying just writing the data to a file opened in binary mode—as currently shown near at the very end @Edeki Okoh's answer. Commented Mar 6, 2019 at 19:11
  • I disagree. In order to make a true pdf file you cannot just use the write method. You need to create a pdf first to make sure that the file being created actually has the properties of a pdf @martineau Commented Mar 6, 2019 at 19:25

1 Answer 1

3

You can create an empty pdf then save write to that pdf in binary like this:

from reportlab.pdfgen import canvas
import requests

# Example of path. This file has not been created yet but we 
# will use this as the location and name of the pdf in question

path_to_create_pdf_with_name_of_pdf = r'C:/User/Oleg/MyDownloadablePdf.pdf'

# Anything you used before making the request. Since you did not
# provide code I did not know what you used
.....
request = requests.get('myserver/pdf', headers)

#Actually creates the empty pdf that we will use to write the binary data to
pdf_file = canvas.Canvas(path_to_create_pdf_with_name_of_pdf)

#Open the empty pdf that we created above and write the binary data to. 
with open(path_to_create_pdf_with_name_of_pdf, 'wb') as f:
     f.write(request.content)
     f.close()

The reportlab.pdfgen allows you to make a new pdf by specifying the path you want to save the pdf in along with the name of the pdf using the canvas.Canvas method. As stated in my answer you need to provide the path to do this.

Once you have an empty pdf, you can open the pdf file as wb (write binary) and write the content of the pdf from the request to the file and close the file.

When using the path - ensure that the name is not the name of any existing files to ensure that you do not overwrite any existing files. As the comments show, if this name is the name of any other file then you risk overwriting the data. If you are doing this in a loop for example, you will need to specify the path with a new name at each iteration to ensure that you have a new pdf each time. But if it is a one-off thing then you do not run that risk so as long as it is not the name of another file.

Sign up to request clarification or add additional context in comments.

9 Comments

What's the point of using reportlab.pdfgen (since your code is completely ignoring the pdf_file variable)?
The method will still create the empty pdf even if I do not call the pdf_file but in the past I have had issues making the empty pdf if it was not assigned to a variable. This is the solution I used to automate it. Essentially that part is just to make sure that we have a pdf to open and write the binary data to. canvas.Canvas will use the name of the pdf in the path so we can assign the name before and just call it again when we want to write to it.
The way you're writing the pdf file completely overwrites anything that may have already been in it.
There's nothing in it to overwrite which is the point. Its an empty pdf that we use later to then write the binary data to. However without assigning it to a variable you can get errors.
Sorry, I don't think that could be true—or at least it doesn't make any sense. What sort of "errors"?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.