How to Generate PDF Receipts from HTML in PHP
In this post, you’ll learn how to generate PDF receipts from HTML using our PHP PDF Generator API. With our API, you receive 100 credits with the free plan. Different operations on a document consume different amounts of credits, so the number of PDF documents you can generate may vary. To access your API key, sign up for a free account.
To help you get started, we’ve provided a free receipt template in HTML and CSS that can be customized to meet your specific requirements. You can easily style your receipts by updating the CSS file with your own custom images and fonts. For receipts that are longer than one page, you can add a header and footer that repeats across all your pages.
Requirements
To get started, you’ll need:
-
PHP
To access your PSPDFKit API key, sign up for a free account. Your account includes 100 credits, which can be used for various document operations. Once you’ve signed up, you can find your API key in the Dashboard > API Keys section.
You can install PHP via Homebrew, or you can check out the other installation options.
Setup
Download the receipt template and extract the contents of the ZIP file into a folder. You’ll get an HTML file, Inter fonts, a Space Mono font, an SVG logo, and a README file.
Interacting with the API
Create a new PHP file named index.php
in the root directory. In this post, you’ll use cURL
(client URL library) to interact with PSPDFKit API.
cURL
is a library that lets you make HTTP requests in PHP. Start by initializing a cURL session using curl_init()
:
$curl = curl_init();
Preparing the Payload
Send the payload with the instructions
object. This object should be a JSON string; otherwise, the API will reject the request. Include your HTML file in the html
key and the other files in the assets
array. In this example, your assets include the SVG logo, the Inter fonts, and the Space Mono font:
$instructions = '{
"parts": [
{
"html": "index.html",
"assets": [
"logo.svg",
"Inter-Bold.ttf",
"Inter-Medium.ttf",
"Inter-Regular.ttf",
"SpaceMono-Regular.ttf"
]
}
]
}';
Making the Request
Start by opening a file named result.pdf
with read and write mode (w+
). If the file doesn’t exist, it’ll be created:
$FileHandle = fopen('result.pdf', 'w+');
Make a request with the curl_setopt_array()
function. This function takes an array of options and sets them.
The first argument is the cURL session, and the second argument is an array of options.
Don’t forget to replace <YOUR_API_KEY>
with your API key:
curl_setopt_array($curl, array( CURLOPT_URL => 'https://api.pspdfkit.com/build', CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_POSTFIELDS => array( 'instructions' => $instructions, 'index.html' => new CURLFILE('index.html'), 'logo.svg' => new CURLFILE('logo.svg'), 'Inter-Bold.ttf' => new CURLFILE('Inter-Bold.ttf'), 'Inter-Medium.ttf' => new CURLFILE('Inter-Medium.ttf'), 'Inter-Regular.ttf' => new CURLFILE('Inter-Regular.ttf'), 'SpaceMono-Regular.ttf' => new CURLFILE('SpaceMono-Regular.ttf') ), CURLOPT_HTTPHEADER => array( 'Authorization: Bearer <YOUR_API_KEY>', // Replace <YOUR_API_KEY> with your API key. ), CURLOPT_FILE => $FileHandle, ));
The CURLOPT_POSTFIELDS
array contains all the parts you want to send it to the API. In this case, you’ve sent the instructions
object, the HTML file, the SVG logo, the Inter fonts, and the Space Mono font.
The CURLOPT_FILE
option is used to write the response to result.pdf
.
To execute your HTTP request, use the curl_exec()
function:
$response = curl_exec($curl);
Lastly, close the cURL session using the curl_close()
function, and close the file handle:
curl_close($curl); fclose($FileHandle);
Generating the PDF
Now, run your PHP application by executing the following command:
php index.php
This will create a result.pdf
file in the current directory.
You can see the full code below:
// index.php <?php $FileHandle = fopen('result.pdf', 'w+'); $curl = curl_init(); $instructions = '{ "parts": [ { "html": "index.html", "assets": [ "logo.svg", "Inter-Bold.ttf", "Inter-Medium.ttf", "Inter-Regular.ttf", "SpaceMono-Regular.ttf" ] } ] }'; curl_setopt_array($curl, array( CURLOPT_URL => 'https://api.pspdfkit.com/build', CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_POSTFIELDS => array( 'instructions' => $instructions, 'index.html' => new CURLFILE('index.html'), 'logo.svg' => new CURLFILE('logo.svg'), 'Inter-Bold.ttf' => new CURLFILE('Inter-Bold.ttf'), 'Inter-Medium.ttf' => new CURLFILE('Inter-Medium.ttf'), 'Inter-Regular.ttf' => new CURLFILE('Inter-Regular.ttf'), 'SpaceMono-Regular.ttf' => new CURLFILE('SpaceMono-Regular.ttf') ), CURLOPT_HTTPHEADER => array( 'Authorization: Bearer <YOUR_API_KEY>', // Replace <YOUR_API_KEY> with your API key. ), CURLOPT_FILE => $FileHandle, )); $response = curl_exec($curl); curl_close($curl); fclose($FileHandle); ?>
Conclusion
In this post, you generated a PDF receipt from an HTML template using our PHP PDF generation API. We created similar PDF receipt generation blog posts using sample code from other programming languages:
In addition to templates for generating receipts, we created free templates for other commonly used documents, like invoices, certificates, and reports. If you’re interested in generating other types of documents in PHP, check out the following posts:
- Generating PDF invoices using PHP
- Generating PDF certificates using PHP
- Generating PDF reports using PHP
All our templates are available for you to download on our PDF Generator API page. Feel free to customize or add any CSS to the template to fit your use case or help reflect your company’s brand.