How to convert HTML to PDF in Python (with Composer)

PDFShift provides a PHP package available for free on https://github.com/pdfshift/pdfshift-php.

Documentation

See the full documentation on PDFShift's documentation.

Requirements

You need PHP 5.4 or later.

Installing with Composer

You can install the bindings via Composer. Run the following command:

composer require pdfshift/pdfshift-php

To use the bindings, use Composer's autoload:

require_once('vendor/autoload.php');

Manual Installation

If you do not wish to use Composer, you can download the latest release. Then, to use the bindings, include the init.php file.

require_once('/path/to/pdfshift-php/init.php');

Usage

This library needs to be configured with your api_key received when creating an account.
Setting it is easy as:

require_once('vendor/autoload.php');
use \PDFShift\PDFShift;

PDFShift::setApiKey('your_api_key');

The sandbox parameter allows you to do unlimited conversion, but will add a watermark on top of the generated document.
No credits are deduced from your account when the sandbox mode is on.

You can set it like this:

require_once('vendor/autoload.php');
use \PDFShift\PDFShift;

PDFShift::setApiKey('your_api_key');

// We use an instance of PDFShift instead of the ::convertTo to easily handle advanced configuration
$pdfshift = new PDFShift();
$pdfshift->sandbox = true;
$pdfshift->convert('https://www.example.com');
$pdfshift->save('result.pdf');

With an URL

Converting an URL with PDFShift is really easy. All you have to do is send a POST request with the source parameter set to the URL, like the following:

require_once('vendor/autoload.php');
use \PDFShift\PDFShift;

PDFShift::setApiKey('your_api_key');

// This will convert "https://www.example.com" and save the result in "result.pdf"
PDFShift::convertTo('https://www.example.com', null, 'result.pdf');

With inline HTML data:

To convert a raw HTML data with PDFShift, simply send the raw string in the source parameter:

require_once('vendor/autoload.php');
use \PDFShift\PDFShift;

PDFShift::setApiKey('your_api_key');

$data = file_get_contents('invoice.html');

// This will convert the content of "invoice.html" and save the result in "result.pdf"
PDFShift::convertTo(data, null, 'result.pdf');

Custom HTTP Headers

You can pass custom HTTP headers, allowing you to adapt to the server handling your source. This can be a custom identification header, changing the language, or anything else.

require_once('vendor/autoload.php');
use \PDFShift\PDFShift;

PDFShift::setApiKey('your_api_key');

// We use an instance of PDFShift instead of the ::convertTo to easily handle advanced configuration
$pdfshift = new PDFShift();
$pdfshift->setHTTPHeaders(['X-Original-Header' => 'Awesome value']);
$pdfshift->addHTTPHeader('user-agent', 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0'); // Also works like this
$pdfshift->convert('https://httpbin.org/headers');
$pdfshift->save('result.pdf');

Accessing secured pages

If your source requires a BASIC AUTH mechanism, you can either use the custom headers part, or use the auth parameter from the API that behaves the same.

require_once('vendor/autoload.php');
use \PDFShift\PDFShift;

PDFShift::setApiKey('your_api_key');

// We use an instance of PDFShift instead of the ::convertTo to easily handle advanced configuration
$pdfshift = new PDFShift();
$pdfshift->auth('user', 'passwd');
$pdfshift->convert('https://httpbin.org/basic-auth/user/passwd');
$pdfshift->save('result.pdf');

Using cookies

Cookies might help you access unauthorized areas that aren't restricted by a simple Basic Auth mechanism. You can define as many cookies as you want.

require_once('vendor/autoload.php');
use \PDFShift\PDFShift;

PDFShift::setApiKey('your_api_key');

// We use an instance of PDFShift instead of the ::convertTo to easily handle advanced configuration
$pdfshift = new PDFShift();
$pdfshift->addCookie('session', '4cb496a8-a3eb-4a7e-a704-f993cb6a4dac');
$pdfshift->convert('https://httpbin.org/cookies');
$pdfshift->save('result.pdf');

Loading CSS from an URL:

By passing a css parameter, you will be able to modify the page with your CSS.
This allows you to customize the rendering of the page.

You can also call multiple CSS by calling a root css (like "print.css" in that case) that will call @import in it for each CSS files.

require_once('vendor/autoload.php');
use \PDFShift\PDFShift;

PDFShift::setApiKey('your_api_key');

$data = file_get_content('invoice.html');
PDFShift::convertTo(data, ['css' => 'https://www.example.com/public/css/print.css'], 'result.pdf');

Loading CSS from a string:

Like for the source parameter, you can pass a raw set of CSS rules to the css parameter and they will be injected to the loaded document.

require_once('vendor/autoload.php');
use \PDFShift\PDFShift;

PDFShift::setApiKey('your_api_key');

$data = file_get_content('invoice.html');
PDFShift::convertTo(data, ['css' => 'a {text-decoration: underline; color: blue}'], 'result.pdf');

Adding Watermark

Some documents that you share need a watermark to clearly identify your brand. That's easy with PDFShift:

require_once('vendor/autoload.php');
use \PDFShift\PDFShift;

PDFShift::setApiKey('your_api_key');

// We use an instance of PDFShift instead of the ::convertTo to easily handle advanced configuration
$pdfshift = new PDFShift();
$pdfshift->watermark([
    'image' => 'https://pdfshift.io/static/img/logo.png',
    'offsetX' => 50,
    'offsetY' => '100px',
    'rotate' => 45
])
$pdfshift->convert('https://www.example.com');
$pdfshift->save('result.pdf');

You can add some custom header or footer to your generated document. These are often used to indicate the current page, or show the logo of your company on every page.

Note that the header and footer are not related to the body. For this reason, the CSS in your body doesn't apply to your header/footer.
By default, the font-size will be really small. You will have to set it manually, like in the following example:

require_once('vendor/autoload.php');
use \PDFShift\PDFShift;

PDFShift::setApiKey('your_api_key');

// We use an instance of PDFShift instead of the ::convertTo to easily handle advanced configuration
$pdfshift = new PDFShift();
$pdfshift->setFooter('<div style="font-size: 12px">Page {{page}} of {{total}}</div>', '50px');
$pdfshift->convert('https://www.example.com');
$pdfshift->save('result.pdf');

Protecting the generated PDF

Protecting your document is easy with PDFShift. You can specify a password for the user and for the owner.
(The owner will have full rights access while the user will have limited access based on your choice).

Please keep in mind that some PDF reader doesn't respect the rights as long as the user is authenticated.
This means that if you set an empty password for the user, with no rights to print or copy, some PDF reader will ignore this and still allow printing and copying.

This is outside of our capabilities here at PDFShift as we can't enforce a reader to respect PDF's standard.

require_once('vendor/autoload.php');
use \PDFShift\PDFShift;

PDFShift::setApiKey('your_api_key');

// We use an instance of PDFShift instead of the ::convertTo to easily handle advanced configuration
$pdfshift = new PDFShift();
$pdfshift->protect([
    'userPassword' => 'user',
    'ownerPassword' => 'owner',
    'noPrint' => true
]);
$pdfshift->convert('https://www.example.com');
$pdfshift->save('result.pdf');

(Read our API documentation for more, in depth, details.)