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
- Webpage to PDF file
- Webpage to in-memory PDF
- Webpage to PDF stream
- HTML file to PDF file
- HTML file to in-memory PDF
- HTML file to PDF stream
- HTML string to PDF file
- HTML string to in-memory PDF
- HTML string to PDF stream
- Get info about the current conversion
Advanced examples
- Customize the page size and the orientation
- Put the source URL in the header and the page number in the footer
- Create fillable PDF form
- Zoom the HTML document
- Set PDF metadata
- Create a Powerpoint like presentation from an HTML document
- Convert an HTML document section
- Inject an HTML code
- Convert a responsive web page as it appears on a large device
- Create an in-memory archive (ZIP) and convert it
- Renderer debugging - highlight HTML elements
- Renderer debugging - borders with spacing around HTML elements
Template rendering examples
- Create PDF from JSON data
- Create PDF from XML data
- Create PDF from YAML data
- Create PDF from CSV data
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", "demo"); // 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", "demo"); // 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", "demo"); // 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", "demo"); // 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", "demo"); // 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", "demo"); // 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", "demo"); // 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", "demo"); // 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", "demo"); // 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", "demo"); // 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", "demo"); // 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", "demo"); // 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", "demo"); // 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", "demo"); // 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", "demo"); // 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", "demo"); // 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", "demo"); // 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", "demo"); // 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", "demo"); // 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", "demo"); // 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", "demo"); // 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", "demo"); // 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", "demo"); // 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", "demo"); // 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", "demo"); // 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", "demo"); // 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
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", "demo"); // 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->getStatusCode()); 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", "demo"); // 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->getStatusCode()); 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", "demo"); // 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->getStatusCode()); 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", "demo"); // 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, $why->getStatusCode()) ->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", "demo"); // 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, $why->getStatusCode()) ->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", "demo"); // 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, $why->getStatusCode()) ->header("Content-Type", "text/plain"); } }); ?>
Symfony examples
Webpage to PDF in Symfony
<?php namespace App\Controller; use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\HttpFoundation\Response; class DemoController { // The recommended method is POST. #[Route('/', methods: ['POST'])] public function convert() { try { // Create an API client instance. $client = new \Pdfcrowd\HtmlToPdfClient("demo", "demo"); // 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, $why->getStatusCode(), ["Content-Type" => "text/plain"]); } } }
HTML file to PDF in Symfony
<?php namespace App\Controller; use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\HttpFoundation\Response; class DemoController { // The recommended method is POST. #[Route('/', methods: ['POST'])] public function convert() { try { // Create an API client instance. $client = new \Pdfcrowd\HtmlToPdfClient("demo", "demo"); // 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, $why->getStatusCode(), ["Content-Type" => "text/plain"]); } } }
HTML string to PDF in Symfony
<?php namespace App\Controller; use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\HttpFoundation\Response; class DemoController { // The recommended method is POST. #[Route('/', methods: ['POST'])] public function convert() { try { // Create an API client instance. $client = new \Pdfcrowd\HtmlToPdfClient("demo", "demo"); // 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, $why->getStatusCode(), ["Content-Type" => "text/plain"]); } } }