Generating PDFs in PHP

Most of web applications today have a PDF export requirement: invoices, reports. Generating these PDFs, especially if they’re complex or large, can be really challenging. Fortunately, there’s DocRaptor, which provides an easy-to-use API for converting HTML web pages into PDFs.

This is a walk-through of how to setup and use DocRaptor’s PHP library:

Installation

DocRaptor can be installed via the Composer dependency manager simply run this on your command line:


composer require docraptor/docrapto

Add Your API Key

Unlimited watermarked test documents can made using the “YOUR_API_KEY_HERE” key. If you’re ready to make non-watermarked documents, you can get your own API key by creating an account.


$configuration = DocRaptorConfiguration::getDefaultConfiguration();
$configuration->setUsername("YOUR_API_KEY_HERE");

Add HTML

To make your PDF, simply set the document content to your HTML:


$docraptor = new DocRaptorDocApi();
$doc = new DocRaptorDoc();
$doc->setDocumentContent("Your HTML goes here!");

If you want to use an existing website page, just input the URL like this:


$docraptor = new DocRaptorDocApi();
$doc = new DocRaptorDoc();
$doc->setDocumentUrl("http://docraptor.com/samples/ebook.html");

Either way you decide to input the HTML, any external assets in your HTML (CSS, JS, images) are required to be accessible to the DocRaptor servers. They must also either have absolute URLs or you can include a baseurl in your document or use the baseurl API option.

Set API Options

Since DocRaptor makes PDFs and Excel files, we need to set the document type:


$doc->setDocumentType("pdf");

To create the free watermarked documents, we need to set test to true:


$doc->setTest(true);

JavaScript processing can be enabled with this command:


$doc->setJavascript(true);

See DocRaptor’s API documentation for the full list of API options. There’s a lot of them, but they aren’t necessary for most projects.

Generate the PDF

That’s all the setup that’s required. Creating the document is just one line of code, but we should wrap it error handling:


try {
    $create_response = $docraptor->createDoc($doc);
} catch (DocRaptorApiException $error) {
    echo $error . "n";
    echo $error->getMessage() . "n";
    echo $error->getCode() . "n";
    echo $error->getResponseBody() . "n";
}

Save the PDF

Once generated, the PDF can be saved to the server.


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

Or Send it to the Browser

Or if we’re creating the PDF after a user action and we want them to immediately download it, we can send it to the browser without saving it on the server:


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;

Complete Example

Putting it all together, here’s the full code for making a PDF with DocRaptor. As you can see, it’s short and offloads all the CPU intensive work off our server and puts it onto DocRaptor. It also lets us rapidly make PDFs if multiple users request a PDF at once, which is hard to do if we try to use an open source library.


setUsername("YOUR_API_KEY_HERE");
$docraptor = new DocRaptorDocApi();
$doc = new DocRaptorDoc();
$doc->setDocumentContent("Hello World");
//$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 (DocRaptorApiException $error) {
    echo $error . "n";
    echo $error->getMessage() . "n";
    echo $error->getCode() . "n";
    echo $error->getResponseBody() . "n";
}