How to generate PDF invoice from HTML template for HTML to PDF API in PHP and PDF.co Web API

How to generate PDF invoice from HTML template for HTML to PDF API in PHP: Step By Step Tutorial

The documentation is designed to help you to implement the features on your side. HTML to PDF API in PHP can be implemented with PDF.co Web API. PDF.co Web API is the flexible Web API that includes full set of functions from e-signature requests to data extraction, OCR, images recognition, pdf splitting and pdf splitting. Can also generate barcodes and read barcodes from images, scans and pdf.

The SDK samples like this one below explain how to quickly make your application do HTML to PDF API in PHP with the help of PDF.co Web API. This PHP sample code should be copied and pasted into your project. After doing this just compile your project and click Run. You can use these PHP sample examples in one or many applications.

Our website provides free trial version of PDF.co Web API that includes source code samples to help with your PHP project.

On-demand (REST Web API) version:
 Web API (on-demand version)

On-premise offline SDK for Windows:
 60 Day Free Trial (on-premise)

generate-invoice.php
      
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>PDF Invoice Generation Results</title> </head> <body> <?php // Get submitted form data $apiKey = $_POST["apiKey"]; // The authentication key (API Key). Get your own by registering at https://app.pdf.co/documentation/api // HTML template $template = file_get_contents("./invoice_template.html"); // Data to fill the template $templateData = file_get_contents("./invoice_data.json"); // Prepare URL for HTML to PDF API call $url = "https://api.pdf.co/v1/pdf/convert/from/html?name=result.pdf"; // Create HTML to PDF options $data = json_encode(array( "html" => utf8_encode($template), "templateData" => utf8_encode($templateData) )); // Create request $curl = curl_init(); curl_setopt($curl, CURLOPT_HTTPHEADER, array("x-api-key: " . $apiKey, "Content-type: application/json")); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Execute request $result = curl_exec($curl); if (curl_errno($curl) == 0) { $status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); if ($status_code == 200) { $json = json_decode($result, true); if ($json["error"] == false) { $resultFileUrl = $json["url"]; // Display link to the file with conversion results echo "<div><h2>Conversion Result:</h2><a href='" . $resultFileUrl . "' target='_blank'>" . $resultFileUrl . "</a></div>"; } else { // Display service reported error echo "<p>Error: " . $json["message"] . "</p>"; } } else { // Display request error echo "<p>Status code: " . $status_code . "</p>"; echo "<p>" . $result . "</p>"; } } else { // Display CURL error echo "Error: " . curl_error($curl); } // Cleanup curl_close($curl); ?> </body> </html>

ON-PREMISE OFFLINE SDK

60 Day Free Trial or Visit PDF.co Web API Home Page

Explore PDF.co Web API Documentation

Explore Samples

Sign Up for PDF.co Web API Online Training

ON-DEMAND REST WEB API

Get Your API Key

Explore Web API Docs

Explore Web API Samples

VIDEO

ON-PREMISE OFFLINE SDK

60 Day Free Trial or Visit PDF.co Web API Home Page

Explore PDF.co Web API Documentation

Explore Samples

Sign Up for PDF.co Web API Online Training

ON-DEMAND REST WEB API

Get Your API Key

Explore Web API Docs

Explore Web API Samples

Related Samples: