Convert HTML To PDF in PHP The Easy Way

Mon 18 April 2011

In this tutorial you will learn how to easily convert web pages and raw HTML code to PDF in your PHP applications. We will use the Pdfcrowd API for PDF generation. The API offers these benefits:

  • The API is easy to use and fully supports HTML/CSS2/JavaScript.
  • The integration takes only a few minutes.
  • No third-party libraries are needed, just a single tiny PHP file.
  • It does not consume CPU/memory on your computer, PDFs are created in the Pdfcrowd servers.

Introduction

Let's start with an example:

<?php
require 'pdfcrowd.php';

$client = new Pdfcrowd("username", "apikey");
$pdf = $client->convertURI('http://bbc.co.uk/');
?>

This code converts bbc.co.uk and stores the generated PDF to a string. You can save the result to a file or you can stream it to the browser, which we will discuss in detail shortly. You can click the thumbnail to open the generated PDF file:

BBC

Besides web pages, you can also convert a local HTML file or an HTML string:

<?php
$pdf_from_file = $client->convertFile('/path/to/your/file.html');
$pdf_from_html = $client->convertHtml('<b>bold</b> and <i>italic</i>');
?>

It is also possible to save the PDF directly to a file by passing a file pointer as the second argument:

<?php
$client->convertURI('http://example.com/', fopen('example.pdf', 'wb'));
?>

Basic Customization

Now that you know the basics, you may want to customize the generated PDF. Let's change the page format to Legal with half-inch margins:

<?php
$client->setPageWidth("8.5in");
$client->setPageHeight("14in");
$client->setHorizontalMargin("0.5in");
$client->setVerticalMargin("0.5in");
?>

You can use metric units as well, for instance the following code sets A4:

<?php
$client->setPageWidth("210mm");
$client->setPageHeight("297mm");
?>

You can also specify the appearance of the PDF when it is opened in a viewer:

<?php
$client->setInitialPdfZoomType(Pdfcrowd::FIT_PAGE);
$client->setPageLayout(Pdfcrowd::CONTINUOUS);
?>

The API provides many other options including password protection and fully customizable page headers and footers. You can find the complete PHP API reference here.

Error Handling

The API should not never return a corrupted PDF. If the service can't for some reason generate a valid PDF then an instance of PdfcrowdException is thrown. Therefore it is highly recommended to enclose your code within a try-catch block:

<?php
try {
    // ...
}
catch(PdfcrowdException $why) {
    echo "FAILED: " . $why;
}
?>

Server Side PDF Generation

In this section we will show two common PDF generation scenarios.

Generate PDF and send it to the browser

The following code converts mydomain.com to PDF and sends it as a response:

<?php

require 'pdfcrowd.php';

try {

    $client = new Pdfcrowd("username", "apikey");
    $pdf = $client->convertURI("http://mydomain.com/");

    header("Content-Type: application/pdf");
    header("Cache-Control: no-cache");
    header("Accept-Ranges: none");
    header("Content-Disposition: inline; filename=\"mydomain.pdf\"");

    echo $pdf;

}
catch(PdfcrowdException $why) {
    echo "Can't create PDF: ".$why."\n";
}

?>

To convert an HTML string you can use convertHtml() instead of convertURI():

<?php
$pdf = $client->convertHtml("<html><head>..</head><body>..</body></html>");
?>

Since Content-Disposition is set to inline the generated PDF is opened in the browser. If you change it to attachment the browser will popup the file download dialog:

<?php
header("Content-Disposition: attachment; filename=\"mydomain.pdf\"");
?>

Provide a PDF version of your web pages

This example shows how to enhance your PHP code so it can return a PDF version of your web pages. Let's look at the following helper function:

<?php
require 'pdfcrowd.php';

function generatePDF()
{
    if (!$_GET["pdf"])
        return False;

    try {
        // build the url and remove the pdf field from the query string
        $url = "http://" . $_SERVER["SERVER_NAME"] . $_SERVER["PHP_SELF"];
        if (count($_GET) > 1) {
            unset($_GET["pdf"]);
            $url = $url . "?" . http_build_query($_GET, '', '&');
        }

        // call the API
        $client = new Pdfcrowd("username", "apikey");
        $pdf = $client->convertURI($url);

        // send the generated pdf to the browser
        header("Content-Type: application/pdf");
        header("Cache-Control: no-cache");
        header("Accept-Ranges: none");
        header("Content-Disposition: attachment; filename=\"created.pdf\"");

        echo $pdf;
    }
    catch(PdfcrowdException $why) {
        echo "PDF creation failed: ".$why."\n";
    }

    return True;
}
?>

The generatePDF() function first checks if there is a pdf field in the query string. If yes, then the field is removed from the url. The function then passes the modified url to the API and finally sends the generated PDF to the browser.

You can use the function in your code like this:

<?php
if (generatePDF())
    return;

// your HTML rendering code
// ...
?>

Now when you enter

http://mydomain.com/page.php?pdf=1

the browser returns a PDF version of the page.


Try Out The API

You can sign up for a free trial account, download the API client library for your favorite language and start creating PDF from web pages or raw HTML in just a few minutes. Pdfcrowd supports PHP, Java, Ruby, Python, .NET, node.js, Bash, and HTTP POST.

links

social