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}\n");
    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}\n");
    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}\n");
    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}\n");
    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}\n");
    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}\n");
    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}\n");
    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}\n");
    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}\n");
    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 of the debug log
    echo "Debug log url: " . $client->getDebugLogUrl() . "\n";
    
    // print the number of conversion credits remaining in your account
    echo "Remaining credit count: " . $client->getRemainingCreditCount() . "\n";
    
    // print the number of credits used for the conversion
    echo "Consumed credit count: " . $client->getConsumedCreditCount() . "\n";
    
    // print the unique identifier for the conversion
    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}\n");
    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}\n");
    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}\n");
    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}\n");
    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}\n");
    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}\n");
    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}\n");
    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}\n");
    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}\n");
    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}\n");
    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}\n");
    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}\n");
    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}\n");
    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}\n");
    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}\n");
    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}\n");
    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}\n");
    throw $why;
}

?>

PHP website examples

Webpage to PDF in PHP website

<?php
require 'pdfcrowd.php';

// The recommended method is POST.
if($_SERVER['REQUEST_METHOD'] == 'POST') {
    try {
        // Create an API client instance.
        $client = new \Pdfcrowd\HtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

        // Run the conversion.
        $pdf = $client->convertUrl("http://www.example.com");

        // Set HTTP response headers.
        header("Content-Type: application/pdf");
        header("Cache-Control: no-cache");
        header("Accept-Ranges: none");
        header("Content-Disposition: attachment; filename*=UTF-8''" . rawurlencode("example.pdf"));

        echo $pdf;
    }
    catch(\Pdfcrowd\Error $why) {
        // Report the error.
        header("Content-Type: text/plain");
        http_response_code($why->getCode());
        echo "Pdfcrowd Error: {$why}";
    }
}

?>

HTML file to PDF in PHP website

<?php
require 'pdfcrowd.php';

// The recommended method is POST.
if($_SERVER['REQUEST_METHOD'] == 'POST') {
    try {
        // Create an API client instance.
        $client = new \Pdfcrowd\HtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

        // Run the conversion.
        $pdf = $client->convertFile("/path/to/MyLayout.html");

        // Set HTTP response headers.
        header("Content-Type: application/pdf");
        header("Cache-Control: no-cache");
        header("Accept-Ranges: none");
        header("Content-Disposition: attachment; filename*=UTF-8''" . rawurlencode("MyLayout.pdf"));

        echo $pdf;
    }
    catch(\Pdfcrowd\Error $why) {
        // Report the error.
        header("Content-Type: text/plain");
        http_response_code($why->getCode());
        echo "Pdfcrowd Error: {$why}";
    }
}

?>

HTML string to PDF in PHP website

<?php
require 'pdfcrowd.php';

// The recommended method is POST.
if($_SERVER['REQUEST_METHOD'] == 'POST') {
    try {
        // Create an API client instance.
        $client = new \Pdfcrowd\HtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

        // Run the conversion.
        $pdf = $client->convertString("<html><body><h1>Hello World!</h1></body></html>");

        // Set HTTP response headers.
        header("Content-Type: application/pdf");
        header("Cache-Control: no-cache");
        header("Accept-Ranges: none");
        header("Content-Disposition: attachment; filename*=UTF-8''" . rawurlencode("HelloWorld.pdf"));

        echo $pdf;
    }
    catch(\Pdfcrowd\Error $why) {
        // Report the error.
        header("Content-Type: text/plain");
        http_response_code($why->getCode());
        echo "Pdfcrowd Error: {$why}";
    }
}

?>

Laravel examples

Webpage to PDF in Laravel

<?php

// The recommended method is POST.
Route::post('/', function () {
    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");

        // Send the result and set HTTP response headers.
        return response($pdf)
            ->header("Content-Type", "application/pdf")
            ->header("Cache-Control", "no-cache")
            ->header("Accept-Ranges", "none")
            ->header("Content-Disposition",
                     "attachment; filename*=UTF-8''" . rawurlencode("example.pdf"));
    }
    catch(\Pdfcrowd\Error $why) {
        // Send the error in the HTTP response.
        return response($why->getMessage(), $why->getCode())
            ->header("Content-Type", "text/plain");
    }
});

?>

HTML file to PDF in Laravel

<?php

// The recommended method is POST.
Route::post('/', function () {
    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");

        // Send the result and set HTTP response headers.
        return response($pdf)
            ->header("Content-Type", "application/pdf")
            ->header("Cache-Control", "no-cache")
            ->header("Accept-Ranges", "none")
            ->header("Content-Disposition",
                     "attachment; filename*=UTF-8''" . rawurlencode("MyLayout.pdf"));
    }
    catch(\Pdfcrowd\Error $why) {
        // Send the error in the HTTP response.
        return response($why->getMessage(), $why->getCode())
            ->header("Content-Type", "text/plain");
    }
});

?>

HTML string to PDF in Laravel

<?php

// The recommended method is POST.
Route::post('/', function () {
    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>");

        // Send the result and set HTTP response headers.
        return response($pdf)
            ->header("Content-Type", "application/pdf")
            ->header("Cache-Control", "no-cache")
            ->header("Accept-Ranges", "none")
            ->header("Content-Disposition",
                     "attachment; filename*=UTF-8''" . rawurlencode("HelloWorld.pdf"));
    }
    catch(\Pdfcrowd\Error $why) {
        // Send the error in the HTTP response.
        return response($why->getMessage(), $why->getCode())
            ->header("Content-Type", "text/plain");
    }
});

?>

Symfony examples

Webpage to PDF in Symfony

<?php
namespace App\Controller;

use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;

class DemoController
{
    /**
     * @Route("/", methods={"POST"})
     * The recommended method is POST.
     */
    public function convert()
    {
        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");

            // Send the result and set HTTP response headers.
            return new Response(
                $pdf,
                Response::HTTP_OK,
                ["Content-Type" => "application/pdf",
                 "Cache-Control" => "no-cache",
                 "Accept-Ranges" => "none",
                 "Content-Disposition" => "attachment; filename*=UTF-8''" . rawurlencode("example.pdf")]);
        }
        catch(\Pdfcrowd\Error $why) {
            // Send the error in the HTTP response.
            return new Response($why->getMessage(),
                                $why->getCode(),
                                ["Content-Type" => "text/plain"]);
        }
    }
}

HTML file to PDF in Symfony

<?php
namespace App\Controller;

use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;

class DemoController
{
    /**
     * @Route("/", methods={"POST"})
     * The recommended method is POST.
     */
    public function convert()
    {
        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");

            // Send the result and set HTTP response headers.
            return new Response(
                $pdf,
                Response::HTTP_OK,
                ["Content-Type" => "application/pdf",
                 "Cache-Control" => "no-cache",
                 "Accept-Ranges" => "none",
                 "Content-Disposition" => "attachment; filename*=UTF-8''" . rawurlencode("MyLayout.pdf")]);
        }
        catch(\Pdfcrowd\Error $why) {
            // Send the error in the HTTP response.
            return new Response($why->getMessage(),
                                $why->getCode(),
                                ["Content-Type" => "text/plain"]);
        }
    }
}

HTML string to PDF in Symfony

<?php
namespace App\Controller;

use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;

class DemoController
{
    /**
     * @Route("/", methods={"POST"})
     * The recommended method is POST.
     */
    public function convert()
    {
        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>");

            // Send the result and set HTTP response headers.
            return new Response(
                $pdf,
                Response::HTTP_OK,
                ["Content-Type" => "application/pdf",
                 "Cache-Control" => "no-cache",
                 "Accept-Ranges" => "none",
                 "Content-Disposition" => "attachment; filename*=UTF-8''" . rawurlencode("HelloWorld.pdf")]);
        }
        catch(\Pdfcrowd\Error $why) {
            // Send the error in the HTTP response.
            return new Response($why->getMessage(),
                                $why->getCode(),
                                ["Content-Type" => "text/plain"]);
        }
    }
}