HTML to Image PHP Examples

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

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

Basic examples

Webpage to PNG file

<?php
require "pdfcrowd.php";

try
{
    // create the API client instance
    $client = new \Pdfcrowd\HtmlToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // configure the conversion
    $client->setOutputFormat("png");

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

?>

Webpage to in-memory PNG

<?php
require "pdfcrowd.php";

try
{
    // create the API client instance
    $client = new \Pdfcrowd\HtmlToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // configure the conversion
    $client->setOutputFormat("png");

    // run the conversion and store the result into the "image" variable
    $image = $client->convertUrl("http://www.example.com");

    // at this point the "image" variable contains PNG 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 PNG stream

<?php
require "pdfcrowd.php";

try
{
    // create the API client instance
    $client = new \Pdfcrowd\HtmlToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // configure the conversion
    $client->setOutputFormat("png");

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

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

    // run the conversion and write the result into 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 PNG file

<?php
require "pdfcrowd.php";

try
{
    // create the API client instance
    $client = new \Pdfcrowd\HtmlToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // configure the conversion
    $client->setOutputFormat("png");

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

?>

HTML file to in-memory PNG

<?php
require "pdfcrowd.php";

try
{
    // create the API client instance
    $client = new \Pdfcrowd\HtmlToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // configure the conversion
    $client->setOutputFormat("png");

    // run the conversion and store the result into the "image" variable
    $image = $client->convertFile("/path/to/MyLayout.html");

    // at this point the "image" variable contains PNG 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 PNG stream

<?php
require "pdfcrowd.php";

try
{
    // create the API client instance
    $client = new \Pdfcrowd\HtmlToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // configure the conversion
    $client->setOutputFormat("png");

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

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

    // run the conversion and write the result into 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 PNG file

<?php
require "pdfcrowd.php";

try
{
    // create the API client instance
    $client = new \Pdfcrowd\HtmlToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // configure the conversion
    $client->setOutputFormat("png");

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

?>

HTML string to in-memory PNG

<?php
require "pdfcrowd.php";

try
{
    // create the API client instance
    $client = new \Pdfcrowd\HtmlToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // configure the conversion
    $client->setOutputFormat("png");

    // run the conversion and store the result into the "image" variable
    $image = $client->convertString("<html><body><h1>Hello World!</h1></body></html>");

    // at this point the "image" variable contains PNG 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 PNG stream

<?php
require "pdfcrowd.php";

try
{
    // create the API client instance
    $client = new \Pdfcrowd\HtmlToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // configure the conversion
    $client->setOutputFormat("png");

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

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

    // run the conversion and write the result into 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 the API client instance
    $client = new \Pdfcrowd\HtmlToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // configure the conversion
    $client->setOutputFormat("png");
    $client->setDebugLog(true);

    // run the conversion and write the result to a file
    $client->convertFileToFile("/path/to/MyLayout.html", "MyLayout.png");
    
    // 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 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;
}

?>

Template rendering examples

Create Image from JSON data

<?php
require "pdfcrowd.php";

try
{
    // create the API client instance
    $client = new \Pdfcrowd\HtmlToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

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

    // run the conversion and write 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 Image from XML data

<?php
require "pdfcrowd.php";

try
{
    // create the API client instance
    $client = new \Pdfcrowd\HtmlToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

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

    // run the conversion and write 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 Image from YAML data

<?php
require "pdfcrowd.php";

try
{
    // create the API client instance
    $client = new \Pdfcrowd\HtmlToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // configure the conversion
    $client->setOutputFormat("png");
    $client->setDataString("name: World
product: Pdfcrowd API");

    // run the conversion and write 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 Image from CSV data

<?php
require "pdfcrowd.php";

try
{
    // create the API client instance
    $client = new \Pdfcrowd\HtmlToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // configure the conversion
    $client->setOutputFormat("png");
    $client->setDataString("name,product
World,Pdfcrowd API");

    // run the conversion and write 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 PNG in PHP website

<?php
require 'pdfcrowd.php';

// the recommended method is POST
if($_SERVER['REQUEST_METHOD'] == 'POST') {
    try {
        // create the API client instance
        $client = new \Pdfcrowd\HtmlToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

        // configure the conversion
        $client->setOutputFormat("png");

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

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

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

?>

HTML file to PNG in PHP website

<?php
require 'pdfcrowd.php';

// the recommended method is POST
if($_SERVER['REQUEST_METHOD'] == 'POST') {
    try {
        // create the API client instance
        $client = new \Pdfcrowd\HtmlToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

        // configure the conversion
        $client->setOutputFormat("png");

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

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

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

?>

HTML string to PNG in PHP website

<?php
require 'pdfcrowd.php';

// the recommended method is POST
if($_SERVER['REQUEST_METHOD'] == 'POST') {
    try {
        // create the API client instance
        $client = new \Pdfcrowd\HtmlToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

        // configure the conversion
        $client->setOutputFormat("png");

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

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

        echo $image;
    }
    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 PNG in Laravel

<?php

// the recommended method is POST
Route::post('/', function () {
    try {
        // create the API client instance
        $client = new \Pdfcrowd\HtmlToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

        // configure the conversion
        $client->setOutputFormat("png");

        // run the conversion and store the result into the "image" variable
        $image = $client->convertUrl("http://www.example.com");

        // send the result and set HTTP response headers
        return response($image)
            ->header("Content-Type", "image/png")
            ->header("Cache-Control", "no-cache")
            ->header("Accept-Ranges", "none")
            ->header("Content-Disposition",
                     "attachment; filename*=UTF-8''" . rawurlencode("example.png"));
    }
    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 PNG in Laravel

<?php

// the recommended method is POST
Route::post('/', function () {
    try {
        // create the API client instance
        $client = new \Pdfcrowd\HtmlToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

        // configure the conversion
        $client->setOutputFormat("png");

        // run the conversion and store the result into the "image" variable
        $image = $client->convertFile("/path/to/MyLayout.html");

        // send the result and set HTTP response headers
        return response($image)
            ->header("Content-Type", "image/png")
            ->header("Cache-Control", "no-cache")
            ->header("Accept-Ranges", "none")
            ->header("Content-Disposition",
                     "attachment; filename*=UTF-8''" . rawurlencode("MyLayout.png"));
    }
    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 PNG in Laravel

<?php

// the recommended method is POST
Route::post('/', function () {
    try {
        // create the API client instance
        $client = new \Pdfcrowd\HtmlToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

        // configure the conversion
        $client->setOutputFormat("png");

        // run the conversion and store the result into the "image" variable
        $image = $client->convertString("<html><body><h1>Hello World!</h1></body></html>");

        // send the result and set HTTP response headers
        return response($image)
            ->header("Content-Type", "image/png")
            ->header("Cache-Control", "no-cache")
            ->header("Accept-Ranges", "none")
            ->header("Content-Disposition",
                     "attachment; filename*=UTF-8''" . rawurlencode("HelloWorld.png"));
    }
    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 PNG 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 the API client instance
            $client = new \Pdfcrowd\HtmlToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

            // configure the conversion
            $client->setOutputFormat("png");

            // run the conversion and store the result into the "image" variable
            $image = $client->convertUrl("http://www.example.com");

            // send the result and set HTTP response headers
            return new Response(
                $image,
                Response::HTTP_OK,
                ["Content-Type" => "image/png",
                 "Cache-Control" => "no-cache",
                 "Accept-Ranges" => "none",
                 "Content-Disposition" => "attachment; filename*=UTF-8''" . rawurlencode("example.png")]);
        }
        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 PNG 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 the API client instance
            $client = new \Pdfcrowd\HtmlToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

            // configure the conversion
            $client->setOutputFormat("png");

            // run the conversion and store the result into the "image" variable
            $image = $client->convertFile("/path/to/MyLayout.html");

            // send the result and set HTTP response headers
            return new Response(
                $image,
                Response::HTTP_OK,
                ["Content-Type" => "image/png",
                 "Cache-Control" => "no-cache",
                 "Accept-Ranges" => "none",
                 "Content-Disposition" => "attachment; filename*=UTF-8''" . rawurlencode("MyLayout.png")]);
        }
        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 PNG 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 the API client instance
            $client = new \Pdfcrowd\HtmlToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

            // configure the conversion
            $client->setOutputFormat("png");

            // run the conversion and store the result into the "image" variable
            $image = $client->convertString("<html><body><h1>Hello World!</h1></body></html>");

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