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.
<?php require "pdfcrowd.php"; try { // Create an API client instance. $client = new \Pdfcrowd\HtmlToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d"); // Configure the conversion. $client->setOutputFormat("png"); // Run the conversion and save 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; } ?>
<?php require "pdfcrowd.php"; try { // Create an API client instance. $client = new \Pdfcrowd\HtmlToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d"); // Configure the conversion. $client->setOutputFormat("png"); // Run the conversion and store the result in 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; } ?>
<?php require "pdfcrowd.php"; try { // Create an 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 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; } ?>
<?php require "pdfcrowd.php"; try { // Create an API client instance. $client = new \Pdfcrowd\HtmlToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d"); // Configure the conversion. $client->setOutputFormat("png"); // Run the conversion and save 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; } ?>
<?php require "pdfcrowd.php"; try { // Create an API client instance. $client = new \Pdfcrowd\HtmlToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d"); // Configure the conversion. $client->setOutputFormat("png"); // Run the conversion and store the result in 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; } ?>
<?php require "pdfcrowd.php"; try { // Create an 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 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; } ?>
<?php require "pdfcrowd.php"; try { // Create an API client instance. $client = new \Pdfcrowd\HtmlToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d"); // Configure the conversion. $client->setOutputFormat("png"); // Run the conversion and save 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; } ?>
<?php require "pdfcrowd.php"; try { // Create an API client instance. $client = new \Pdfcrowd\HtmlToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d"); // Configure the conversion. $client->setOutputFormat("png"); // Run the conversion and store the result in 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; } ?>
<?php require "pdfcrowd.php"; try { // Create an 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 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; } ?>
<?php require "pdfcrowd.php"; try { // Create an API client instance. $client = new \Pdfcrowd\HtmlToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d"); // Configure the conversion. $client->setOutputFormat("png"); $client->setDebugLog(true); // Run the conversion and save 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; } ?>
<?php require "pdfcrowd.php"; try { // Create an 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 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 require "pdfcrowd.php"; try { // Create an 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 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; } ?>
<?php require "pdfcrowd.php"; try { // Create an 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 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 require "pdfcrowd.php"; try { // Create an 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 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 require 'pdfcrowd.php'; // The recommended method is POST. if($_SERVER['REQUEST_METHOD'] == 'POST') { try { // Create an 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}"; } } ?>
<?php require 'pdfcrowd.php'; // The recommended method is POST. if($_SERVER['REQUEST_METHOD'] == 'POST') { try { // Create an 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}"; } } ?>
<?php require 'pdfcrowd.php'; // The recommended method is POST. if($_SERVER['REQUEST_METHOD'] == 'POST') { try { // Create an 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}"; } } ?>
<?php // The recommended method is POST. Route::post('/', function () { try { // Create an API client instance. $client = new \Pdfcrowd\HtmlToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d"); // Configure the conversion. $client->setOutputFormat("png"); // Run the conversion and store the result in 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"); } }); ?>
<?php // The recommended method is POST. Route::post('/', function () { try { // Create an API client instance. $client = new \Pdfcrowd\HtmlToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d"); // Configure the conversion. $client->setOutputFormat("png"); // Run the conversion and store the result in 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"); } }); ?>
<?php // The recommended method is POST. Route::post('/', function () { try { // Create an API client instance. $client = new \Pdfcrowd\HtmlToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d"); // Configure the conversion. $client->setOutputFormat("png"); // Run the conversion and store the result in 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"); } }); ?>
<?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\HtmlToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d"); // Configure the conversion. $client->setOutputFormat("png"); // Run the conversion and store the result in 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"]); } } }
<?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\HtmlToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d"); // Configure the conversion. $client->setOutputFormat("png"); // Run the conversion and store the result in 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"]); } } }
<?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\HtmlToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d"); // Configure the conversion. $client->setOutputFormat("png"); // Run the conversion and store the result in 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"]); } } }