Links
Comment on page

Download PDF from URL

Tags: #python #pdf #snippet #url #naas #operations
Author: Florent Ravenel
Last update: 2023-04-12 (Created: 2022-04-29)
Description: This notebook provides a step-by-step guide to downloading a PDF file from a URL using Python.

Input

Import libraries

import urllib

Setup Variables

# Input
pdf_path = "https://s22.q4cdn.com/959853165/files/doc_financials/2021/q4/da27d24b-9358-4b5c-a424-6da061d91836.pdf"
# Output
pdf_output = "2021 Annual Report.pdf"

Model

Download PDF

def download_pdf(url, filepath):
response = urllib.request.urlopen(url)
file = open(filepath, "wb")
file.write(response.read())
file.close()
print("File saved:", filepath)

Output

Display result

download_pdf(pdf_path, pdf_output)