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?

martineau
  • 119,623
  • 25
  • 170
  • 301
0leg
  • 13,464
  • 16
  • 70
  • 94
  • 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. – martineau Mar 06 '19 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](https://stackoverflow.com/questions/45953770/creating-and-writing-to-a-pdf-file-in-python) @martineau – Edeki Okoh Mar 06 '19 at 19:25

1 Answers1

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.

Edeki Okoh
  • 1,786
  • 15
  • 27
  • What's the point of using `reportlab.pdfgen` (since your code is completely ignoring the `pdf_file` variable)? – martineau Mar 06 '19 at 19:00
  • 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. – Edeki Okoh Mar 06 '19 at 19:01
  • The way you're writing the pdf file completely overwrites anything that may have already been in it. – martineau Mar 06 '19 at 19:02
  • 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. – Edeki Okoh Mar 06 '19 at 19:04
  • Sorry, I don't think that could be true—or at least it doesn't make any sense. What sort of "errors"? – martineau Mar 06 '19 at 19:07
  • The canvas object not being created correctly. I used the [Documentation](https://www.reportlab.com/docs/reportlab-userguide.pdf) when following the guide and sometimes I would have problems with my pdf not being created correctly when not assigned to a variable. However this method, assign the canvas object to a variable, even without using the object, still allows the empty pdf to be created which I can then use to write to. I would only need to use the pdf object if I was doing other operations on it i.e showing a specific page, cropping, etc. @martineau – Edeki Okoh Mar 06 '19 at 19:16
  • @martineau I think the confusion here is with the write method. I don't think you can make a pdf directly with the write method. You need a different package to make sure the file created has the properties of a pdf before writing to it. – Edeki Okoh Mar 06 '19 at 19:26
  • The way you're doing completely replaces whatever may have previously been in the file (if it even exists at that point). Perhaps you're talking about some OS-specific file metadata. Guess we'll have to wait and let the OP decide... – martineau Mar 06 '19 at 19:28
  • It does not replace anything since the file does not get created until you call the canvas.Canvas method. Please read the documentation. – Edeki Okoh Mar 06 '19 at 23:44