PHPIntermediate

Convert HTML to PDF with DocRaptor

PB Pb28 Master Team July 15th, 2022 Intermediate

📦 Get the complete source code for this tutorial

Generating PDFs is a common programming task. PDFs are used to make invoices, receipts, eBooks, publishing documents and more. Unfortunately, programmatic generation of PDF can be difficult, especially if you need to generate many PDFs rapidly.

DocRaptor is an HTML-to-PDF API that greatly simplifies the problem, and they have a PHP library for easy integration. Let’s walk through how to convert HTML into a PDF with DocRaptor:

Step 1: Installation

Download the latest release and unzip into your project. If you use Composer, run this from your command line:

code
composer require docraptor/docraptor

If you’re not using Composer, just download the latest release and unzip it into your project. Make a new file called “docraptor.php” and include autoload.php:

php
// docraptor.php

require_once('/path/to/docraptor-php/autoload.php');

Step 2: Authentication

You can use the key “YOUR_API_KEY_HERE” without signing up for an account, but it can only be used for test (watermarked) documents. To make non-watermarked documents, you’ll have to signup for a paid account.

DocRaptor

php
$configuration = DocRaptor\Configuration::getDefaultConfiguration();

$configuration->setUsername("YOUR_API_KEY_HERE");

Step 3: Add HTML or a URL

You can use any HTML you want to make the PDF:

php
$docraptor = new DocRaptor\DocApi();

$doc = new DocRaptor\Doc();

$doc->setDocumentContent("<html><body>Hello World</body></html>");

You can also use a website URL:

php
$docraptor = new DocRaptor\DocApi();

$doc = new DocRaptor\Doc();

$doc->setDocumentUrl("http://docraptor.com/examples/invoice.html");

Whether you use HTML or a URL, all the external assets referenced (images, stylesheets, etc) must be accessible over the internet because DocRaptor will need to access them.

Step 4: API Options

First, let’s make sure we make a PDF from HTML (DocRaptor also converts HTML into Excel files):

php
$doc->setDocumentType("pdf");

And we need to make a test document, since we’re using the free API key:

php
$doc->setTest(true);

JavaScript is off by default to speed up document creation, but you can turn it on with this line:

php
$doc->setJavascript(true);

There’s bunch of other options available if you read DocRaptor’s API documentation.

Step 5: Get the document!

Once all your API options are set, just run this code to generate the PDF document. It includes error handling if anything goes wrong.

php
try {

   $create_response = $docraptor->createDoc($doc);

} catch (DocRaptor\ApiException $error) {

   echo $error . "\n";

   echo $error->getMessage() . "\n";

   echo $error->getCode() . "\n";

   echo $error->getResponseBody() . "\n";

}

You can save it to your server with this code:

php
$file = fopen("/tmp/docraptor-php.pdf", "wb");

fwrite($file, $create_response);

fclose($file);

Or let the user download it with this code:

php
header('Content-Description: File Transfer');

header('Content-Type: application/pdf');

header('Content-Disposition: attachment; filename=example.pdf');

header('Content-Transfer-Encoding: binary');

header('Expires: 0');

header('Cache-Control: must-revalidate, post-check=0, pre-check=0');

header('Pragma: public');

header('Content-Length: ' . strlen($create_response));

ob_clean();

flush();

echo($create_response);

exit;

That’s it.

The complete code

php
<?php

// docraptor.php

require_once('docraptor-php/autoload.php');



$configuration = DocRaptor\Configuration::getDefaultConfiguration();

$configuration->setUsername("YOUR_API_KEY_HERE");



$docraptor = new DocRaptor\DocApi();

$doc = new DocRaptor\Doc();

$doc->setDocumentContent("<html><body>Hello World</body></html>");

//$doc->setDocumentUrl("http://docraptor.com/examples/invoice.html");



$doc->setDocumentType("pdf"); // DocRaptor also makes Excel files

$doc->setTest(true);

//$doc->setJavascript(true);



try {

  $create_response = $docraptor->createDoc($doc);



  $file = fopen("/tmp/docraptor-php.pdf", "wb");

  fwrite($file, $create_response);

  fclose($file);



  //header('Content-Description: File Transfer');

  //header('Content-Type: application/pdf');

  //header('Content-Disposition: attachment; filename=example.pdf');

  //header('Content-Transfer-Encoding: binary');

  //header('Expires: 0');

  //header('Cache-Control: must-revalidate, post-check=0, pre-check=0');

  //header('Pragma: public');

  //header('Content-Length: ' . strlen($create_response));

  //ob_clean();

  //flush();

  //echo($create_response);

  //exit;



} catch (DocRaptor\ApiException $error) {

   echo $error . "\n";

   echo $error->getMessage() . "\n";

   echo $error->getCode() . "\n";

   echo $error->getResponseBody() . "\n";

}

📦 Download the full project files and try it locally