Web/HTML To PDF REST API for Python Samples

python-programming-languagePython is a widely used general-purpose, high-level programming language. Its design philosophy emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than would be possible in languages such as C++ or Java. The language provides constructs intended to enable clear programs on both a small and large scale.

Python supports multiple programming paradigms, including object-oriented, imperative and functional programming or procedural styles. It features a dynamic type system and automatic memory management and has a large and comprehensive standard library.

In this post, SelectPdf team has put together a few Python samples for our Html To Pdf API to make your life a little easier if you need to convert from web pages to pdf in your Python applications.

Later Edit: The recommended approach to access SelectPdf Online REST API from Python is to use the dedicated Python Client Library for SelectPdf API. Alternatively, you can write your own code to access the API. Below are some examples for this approach.

Example #1 – How to use Python to convert a webpage to PDF and save it on the disk

This code converts an url to pdf in Python using SelectPdf HTML To PDF API through a GET request. The content is saved into a file on the disk.

Example #2 – How to use Python to convert a webpage to PDF with a POST API request and save it on the disk

This code converts an url to pdf in Python using SelectPdf HTML To PDF REST API through a POST request. The parameters are url encoded (default). The content is saved into a file on the disk.

Example #3 – How to use Python to convert a web page to PDF with a POST API request and save it on the disk

This code converts an url to pdf in Python using SelectPdf HTML To PDF REST API through a POST request. The parameters are JSON encoded. The content is saved into a file on the disk.

The above samples use only the 2 mandatory parameters (key and url or html) for the html to pdf conversion. For the full list of optional parameters, take a look at SelectPdf HTML To PDF API page. Using the API optional parameters, you can control the page size, orientation and margins in the pdf document, the document information and security, the headers and footers and a lot more.

Hopefully these examples will help you to be on your way using the API for URL to PDF conversion in Python.

The above Python samples can also be found in the following GitHub repository:
https://github.com/selectpdf/selectpdf-api-python-samples