0

I have a csv file with the url for a list of invoices in Coupa. I need to use python to got to the Coupa and download the image scan PDF file to a specific folder. I have the following code. It runs but when I open the PDF file it comes back corrupted. Any help would be appreciated.

import requests 
file_url = "https://mercuryinsurance.coupahost.com/invoice/15836/image_scan"

r = requests.get(file_url, stream = False) 

with open("python.pdf","wb") as pdf: 
    for chunk in r.iter_content(chunk_size=1024): 

         # writing one chunk at a time to pdf file 
         if chunk: 
             pdf.write(chunk)
Jon
  • 1,820
  • 2
  • 19
  • 43
Glen
  • 1
  • Can you confirm that there are bytes that return from `requests.get()`? – Jon Feb 14 '20 at 18:06
  • Did you attempt to open that PDF only with a PDF viewer, or did you also inspect it with a hex viewer to see if it really contains a PDF? – Jongware Feb 14 '20 at 18:57
  • Yes, I get bytes returned, and when I changed the type on the file from PDF to HTML, I get the login screen. – Glen Feb 17 '20 at 16:15
  • Yes, I attempted to open the PDF file directly, and it works without issue. The only reason I am using python is because I have over 2,300 PDF files to download. – Glen Feb 17 '20 at 16:16

0 Answers0