HTML to PDF / PHP Examples

This page contains various examples of using the HTML to PDF API in PHP. The examples are complete and fully functional. Read more about how to convert HTML to PDF in PHP.

Basic examples
Advanced examples
Template rendering examples
PHP website examples
Laravel examples
Symfony examples

Basic examples

Webpage to PDF file

<?php
require "pdfcrowd.php";

try
{
    // Create an API client instance.
    $client = new \Pdfcrowd\HtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // Specify the mapping of HTML content width to the PDF page width.
    // To fine-tune the layout, you can specify an exact viewport width, such as '960px'.
    $client->setContentViewportWidth("balanced");

    // Run the conversion and save the result to a file.
    $client->convertUrlToFile("http://www.example.com", "example.pdf");
}
catch(\Pdfcrowd\Error $why)
{
    error_log("PDFCrowd Error: {$why}");
    throw $why;
}

?>

Webpage to in-memory PDF

<?php
require "pdfcrowd.php";

try
{
    // Create an API client instance.
    $client = new \Pdfcrowd\HtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // Run the conversion and store the result in the `pdf` variable.
    $pdf = $client->convertUrl("http://www.example.com");

    // at this point the "pdf" variable contains PDF raw data and
    // can be sent in an HTTP response, saved to a file, etc.
}
catch(\Pdfcrowd\Error $why)
{
    error_log("PDFCrowd Error: {$why}");
    throw $why;
}

?>

Webpage to PDF stream

<?php
require "pdfcrowd.php";

try
{
    // Create an API client instance.
    $client = new \Pdfcrowd\HtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // Create an output stream for the conversion result
    $output_stream = fopen("example.pdf", "wb");

    // Check for a file creation error.
    if (!$output_stream)
        throw new \Exception(error_get_last()['message']);

    // run the conversion and write the result to the output stream.
    $client->convertUrlToStream("http://www.example.com", $output_stream);

    // Close the output stream.
    fclose($output_stream);
}
catch(\Pdfcrowd\Error $why)
{
    error_log("PDFCrowd Error: {$why}");
    throw $why;
}

?>

HTML file to PDF file

<?php
require "pdfcrowd.php";

try
{
    // Create an API client instance.
    $client = new \Pdfcrowd\HtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // Specify the mapping of HTML content width to the PDF page width.
    // To fine-tune the layout, you can specify an exact viewport width, such as '960px'.
    $client->setContentViewportWidth("balanced");

    // Run the conversion and save the result to a file.
    $client->convertFileToFile("/path/to/MyLayout.html", "MyLayout.pdf");
}
catch(\Pdfcrowd\Error $why)
{
    error_log("PDFCrowd Error: {$why}");
    throw $why;
}

?>

HTML file to in-memory PDF

<?php
require "pdfcrowd.php";

try
{
    // Create an API client instance.
    $client = new \Pdfcrowd\HtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // Run the conversion and store the result in the `pdf` variable.
    $pdf = $client->convertFile("/path/to/MyLayout.html");

    // at this point the "pdf" variable contains PDF raw data and
    // can be sent in an HTTP response, saved to a file, etc.
}
catch(\Pdfcrowd\Error $why)
{
    error_log("PDFCrowd Error: {$why}");
    throw $why;
}

?>

HTML file to PDF stream

<?php
require "pdfcrowd.php";

try
{
    // Create an API client instance.
    $client = new \Pdfcrowd\HtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // Create an output stream for the conversion result
    $output_stream = fopen("MyLayout.pdf", "wb");

    // Check for a file creation error.
    if (!$output_stream)
        throw new \Exception(error_get_last()['message']);

    // run the conversion and write the result to the output stream.
    $client->convertFileToStream("/path/to/MyLayout.html", $output_stream);

    // Close the output stream.
    fclose($output_stream);
}
catch(\Pdfcrowd\Error $why)
{
    error_log("PDFCrowd Error: {$why}");
    throw $why;
}

?>

HTML string to PDF file

<?php
require "pdfcrowd.php";

try
{
    // Create an API client instance.
    $client = new \Pdfcrowd\HtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // Specify the mapping of HTML content width to the PDF page width.
    // To fine-tune the layout, you can specify an exact viewport width, such as '960px'.
    $client->setContentViewportWidth("balanced");

    // Run the conversion and save the result to a file.
    $client->convertStringToFile("<html><body><h1>Hello World!</h1></body></html>", "HelloWorld.pdf");
}
catch(\Pdfcrowd\Error $why)
{
    error_log("PDFCrowd Error: {$why}");
    throw $why;
}

?>

HTML string to in-memory PDF

<?php
require "pdfcrowd.php";

try
{
    // Create an API client instance.
    $client = new \Pdfcrowd\HtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // Run the conversion and store the result in the `pdf` variable.
    $pdf = $client->convertString("<html><body><h1>Hello World!</h1></body></html>");

    // at this point the "pdf" variable contains PDF raw data and
    // can be sent in an HTTP response, saved to a file, etc.
}
catch(\Pdfcrowd\Error $why)
{
    error_log("PDFCrowd Error: {$why}");
    throw $why;
}

?>

HTML string to PDF stream

<?php
require "pdfcrowd.php";

try
{
    // Create an API client instance.
    $client = new \Pdfcrowd\HtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // Create an output stream for the conversion result
    $output_stream = fopen("HelloWorld.pdf", "wb");

    // Check for a file creation error.
    if (!$output_stream)
        throw new \Exception(error_get_last()['message']);

    // run the conversion and write the result to the output stream.
    $client->convertStringToStream("<html><body><h1>Hello World!</h1></body></html>", $output_stream);

    // Close the output stream.
    fclose($output_stream);
}
catch(\Pdfcrowd\Error $why)
{
    error_log("PDFCrowd Error: {$why}");
    throw $why;
}

?>

Get info about the current conversion

<?php
require "pdfcrowd.php";

try
{
    // Create an API client instance.
    $client = new \Pdfcrowd\HtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // Specify the mapping of HTML content width to the PDF page width.
    // To fine-tune the layout, you can specify an exact viewport width, such as '960px'.
    $client->setDebugLog(true);

    // Run the conversion and save the result to a file.
    $client->convertFileToFile("/path/to/MyLayout.html", "MyLayout.pdf");
    
    // print URL pointing to the debug log for this request.
    echo "Debug log url: " . $client->getDebugLogUrl() . "\n";
    
    // print Number of conversion credits remaining in your account.
    echo "Remaining credit count: " . $client->getRemainingCreditCount() . "\n";
    
    // print Number of credits consumed for this conversion.
    echo "Consumed credit count: " . $client->getConsumedCreditCount() . "\n";
    
    // print Unique identifier assigned to this conversion job.
    echo "Job id: " . $client->getJobId() . "\n";
    
    // print Total number of pages in the output document.
    echo "Page count: " . $client->getPageCount() . "\n";
    
    // print Size of the output data in bytes.
    echo "Output size: " . $client->getOutputSize() . "\n";
}
catch(\Pdfcrowd\Error $why)
{
    error_log("PDFCrowd Error: {$why}");
    throw $why;
}

?>

Advanced examples

Customize the page size and the orientation

<?php
require "pdfcrowd.php";

try
{
    // Create an API client instance.
    $client = new \Pdfcrowd\HtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // Configure the conversion.
    $client->setPageSize("Letter");
    $client->setOrientation("landscape");
    $client->setNoMargins(true);

    // Run the conversion and save the result to a file.
    $client->convertUrlToFile("http://www.example.com", "letter_landscape.pdf");
}
catch(\Pdfcrowd\Error $why)
{
    error_log("PDFCrowd Error: {$why}");
    throw $why;
}

?>

Put the source URL in the header and the page number in the footer

<?php
require "pdfcrowd.php";

try
{
    // Create an API client instance.
    $client = new \Pdfcrowd\HtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // Configure the conversion.
    $client->setHeaderHeight("15mm");
    $client->setFooterHeight("10mm");
    $client->setHeaderHtml("<a class='pdfcrowd-source-url' data-pdfcrowd-placement='href-and-content'></a>");
    $client->setFooterHtml("<center><span class='pdfcrowd-page-number'></span></center>");
    $client->setMarginTop("0");
    $client->setMarginBottom("0");

    // Run the conversion and save the result to a file.
    $client->convertUrlToFile("http://www.example.com", "header_footer.pdf");
}
catch(\Pdfcrowd\Error $why)
{
    error_log("PDFCrowd Error: {$why}");
    throw $why;
}

?>

Create fillable PDF form

<?php
require "pdfcrowd.php";

try
{
    // Create an API client instance.
    $client = new \Pdfcrowd\HtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // Configure the conversion.
    $client->setEnablePdfForms(true);

    // Run the conversion and save the result to a file.
    $client->convertStringToFile("<html><body>Enter name:<input type=text></body></html>", "form.pdf");
}
catch(\Pdfcrowd\Error $why)
{
    error_log("PDFCrowd Error: {$why}");
    throw $why;
}

?>

Zoom the HTML document

<?php
require "pdfcrowd.php";

try
{
    // Create an API client instance.
    $client = new \Pdfcrowd\HtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // Configure the conversion.
    $client->setScaleFactor(300);

    // Run the conversion and save the result to a file.
    $client->convertUrlToFile("http://www.example.com", "zoom_300.pdf");
}
catch(\Pdfcrowd\Error $why)
{
    error_log("PDFCrowd Error: {$why}");
    throw $why;
}

?>

Set PDF metadata

<?php
require "pdfcrowd.php";

try
{
    // Create an API client instance.
    $client = new \Pdfcrowd\HtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // Configure the conversion.
    $client->setAuthor("Pdfcrowd");
    $client->setTitle("Hello World");
    $client->setSubject("Demo");
    $client->setKeywords("Pdfcrowd,demo");

    // Run the conversion and save the result to a file.
    $client->convertUrlToFile("http://www.example.com", "with_metadata.pdf");
}
catch(\Pdfcrowd\Error $why)
{
    error_log("PDFCrowd Error: {$why}");
    throw $why;
}

?>

Create a Powerpoint like presentation from an HTML document

<?php
require "pdfcrowd.php";

try
{
    // Create an API client instance.
    $client = new \Pdfcrowd\HtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // Configure the conversion.
    $client->setPageLayout("single-page");
    $client->setPageMode("full-screen");
    $client->setInitialZoomType("fit-page");
    $client->setOrientation("landscape");
    $client->setNoMargins(true);

    // Run the conversion and save the result to a file.
    $client->convertUrlToFile("https://pdfcrowd.com/api/", "slide_show.pdf");
}
catch(\Pdfcrowd\Error $why)
{
    error_log("PDFCrowd Error: {$why}");
    throw $why;
}

?>

Convert an HTML document section

<?php
require "pdfcrowd.php";

try
{
    // Create an API client instance.
    $client = new \Pdfcrowd\HtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // Configure the conversion.
    $client->setElementToConvert("#main");

    // Run the conversion and save the result to a file.
    $client->convertUrlToFile("https://pdfcrowd.com/api/", "html_part.pdf");
}
catch(\Pdfcrowd\Error $why)
{
    error_log("PDFCrowd Error: {$why}");
    throw $why;
}

?>

Inject an HTML code

<?php
require "pdfcrowd.php";

try
{
    // Create an API client instance.
    $client = new \Pdfcrowd\HtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // Configure the conversion.
    $client->setCustomJavascript("el=document.createElement('h2'); el.textContent='Hello from Pdfcrowd API'; el.style.color='red'; el_before=document.getElementsByTagName('h1')[0]; el_before.parentNode.insertBefore(el, el_before.nextSibling)");

    // Run the conversion and save the result to a file.
    $client->convertUrlToFile("http://www.example.com", "html_inject.pdf");
}
catch(\Pdfcrowd\Error $why)
{
    error_log("PDFCrowd Error: {$why}");
    throw $why;
}

?>

Convert a responsive web page as it appears on a large device

<?php
require "pdfcrowd.php";

try
{
    // Create an API client instance.
    $client = new \Pdfcrowd\HtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // Configure the conversion.
    $client->setContentViewportWidth("large");
    $client->setNoMargins(true);

    // Run the conversion and save the result to a file.
    $client->convertUrlToFile("https://getbootstrap.com/", "bootstrap.pdf");
}
catch(\Pdfcrowd\Error $why)
{
    error_log("PDFCrowd Error: {$why}");
    throw $why;
}

?>

Create an in-memory archive (ZIP) and convert it

<?php
require "pdfcrowd.php";
require "vendor/autoload.php";

try
{
    // Create an API client instance.
    $client = new \Pdfcrowd\HtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // Create a ZIP archive.
    $options = new ZipStream\Option\Archive();
    $in_stream = fopen("php://memory", "rw");
    $options->setOutputStream($in_stream);
    $zip = new ZipStream\ZipStream("stream.zip", $options);

    // Add HTML content to the archive.
    $zip->addFile("index.html", "<html>
        <head>
            <style>
             @font-face
             {
                 font-family: 'OpenSans';
                 src: url(fonts/OpenSans.ttf) format('truetype');
             }
    
             h1
             {
                 font-family: OpenSans;
             }
            </style>
        </head>
        <body>
            <h1>Hello World</h1>
            <img src='images/logo.png'>
        </body>
    </html>");

    # Add required local files to the archive.
    $zip->addFileFromPath("fonts/OpenSans.ttf", "/your-path-to/fonts/OpenSans.ttf");
    $zip->addFileFromPath("images/logo.png", "/your-path-to/images/logo.png");

    $zip->finish();
    fseek($in_stream, 0);

    // Create an output stream for the conversion result
    $output_stream = fopen("HelloFromZip.pdf", "wb");

    // Check for a file creation error.
    if (!$output_stream)
        throw new \Exception(error_get_last()['message']);

    // run the conversion and write the result to the output stream.
    $client->convertStreamToStream($in_stream, $output_stream);

    // Close the output stream.
    fclose($output_stream);
}
catch(\Pdfcrowd\Error $why)
{
    error_log("PDFCrowd Error: {$why}");
    throw $why;
}

?>

Renderer debugging - highlight HTML elements

<?php
require "pdfcrowd.php";

try
{
    // Create an API client instance.
    $client = new \Pdfcrowd\HtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // Configure the conversion.
    $client->setCustomJavascript("libPdfcrowd.highlightHtmlElements({backgroundColor: 'rgba(255, 191, 0, 0.1)', borderColor:null})");

    // Run the conversion and save the result to a file.
    $client->convertUrlToFile("http://www.example.com", "highlight_background.pdf");
}
catch(\Pdfcrowd\Error $why)
{
    error_log("PDFCrowd Error: {$why}");
    throw $why;
}

?>

Renderer debugging - borders with spacing around HTML elements

<?php
require "pdfcrowd.php";

try
{
    // Create an API client instance.
    $client = new \Pdfcrowd\HtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // Configure the conversion.
    $client->setCustomJavascript("libPdfcrowd.highlightHtmlElements({borderColor: 'orange', backgroundColor: null, padding: '4px', margin: '4px'})");

    // Run the conversion and save the result to a file.
    $client->convertUrlToFile("http://www.example.com", "highlight_borders.pdf");
}
catch(\Pdfcrowd\Error $why)
{
    error_log("PDFCrowd Error: {$why}");
    throw $why;
}

?>

Template rendering examples

Create PDF from JSON data

<?php
require "pdfcrowd.php";

try
{
    // Create an API client instance.
    $client = new \Pdfcrowd\HtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // Configure the conversion.
    $client->setDataString('{
            "name": "World",
            "product": "Pdfcrowd API"
        }');

    // Run the conversion and save the result to a file.
    $client->convertStringToFile("Hello {{ name }} from {{ product }}", "output.pdf");
}
catch(\Pdfcrowd\Error $why)
{
    error_log("PDFCrowd Error: {$why}");
    throw $why;
}

?>

Create PDF from XML data

<?php
require "pdfcrowd.php";

try
{
    // Create an API client instance.
    $client = new \Pdfcrowd\HtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // Configure the conversion.
    $client->setDataString('<?xml version="1.0" encoding="UTF-8"?>
        <data>
          <name>World</name>
          <product>Pdfcrowd API</product>
        </data>');

    // Run the conversion and save the result to a file.
    $client->convertStringToFile("Hello {{ data.name }} from {{ data.product }}", "output.pdf");
}
catch(\Pdfcrowd\Error $why)
{
    error_log("PDFCrowd Error: {$why}");
    throw $why;
}

?>

Create PDF from YAML data

<?php
require "pdfcrowd.php";

try
{
    // Create an API client instance.
    $client = new \Pdfcrowd\HtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // Configure the conversion.
    $client->setDataString("name: World
product: Pdfcrowd API");

    // Run the conversion and save the result to a file.
    $client->convertStringToFile("Hello {{ name }} from {{ product }}", "output.pdf");
}
catch(\Pdfcrowd\Error $why)
{
    error_log("PDFCrowd Error: {$why}");
    throw $why;
}

?>

Create PDF from CSV data

<?php
require "pdfcrowd.php";

try
{
    // Create an API client instance.
    $client = new \Pdfcrowd\HtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // Configure the conversion.
    $client->setDataString("name,product
World,Pdfcrowd API");

    // Run the conversion and save the result to a file.
    $client->convertStringToFile("Hello {{ name }} from {{ product }}", "output.pdf");
}
catch(\Pdfcrowd\Error $why)
{
    error_log("PDFCrowd Error: {$why}");
    throw $why;
}

?>

PHP website examples

Laravel examples

Symfony examples