View all our articles

Convert an HTML document to PDF from a URL in Python with requests

Converting HTML documents to PDFs using PDFShift's API is a straightforward process. With just a few lines of Python code, you can seamlessly convert a URL into a PDF document.

import requests

# You can get an API key at https://pdfshift.io
api_key = 'sk_xxxxxxxxxxxx'

params = {
    'source': 'https://www.example.com',
}

response = requests.post(
    'https://api.pdfshift.io/v3/convert/pdf',
    auth=('api', api_key),
    json=params
)
response.raise_for_status()

with open('result.pdf', 'wb') as f:
    f.write(response.content)

print('The PDF document was generated and saved to result.pdf')

With this Python script, you can effortlessly convert HTML documents from a URL to PDF files using PDFShift's API. Happy coding!

For further details on the source property and its usage, please refer to our dedicated documentation.

We hope this guide was helpful. If you have any questions or noticed any issues on the code above,
feel free to drop us a line.