Blog Post

How to Generate PDF Invoices from HTML in PHP

Illustration: How to Generate PDF Invoices from HTML in PHP

In this post, you’ll learn how to generate PDF invoices from HTML using our PHP PDF Generator API. With our API, you can generate 100 PDF invoices per month for free. To access your API key, sign up for a free account.

To help you get started, we’ve provided a free invoice template in HTML and CSS that can be customized to meet your specific requirements. You can easily style your invoices by updating the CSS file with your own custom images and fonts.

If your invoices are longer than one page, you can add a header and footer section to your HTML file that repeats across all your pages. This ensures that the information you typically add to the top and bottom of your invoices (like the address, logo, page numbers, etc.) is displayed across all the pages.

Requirements

To get started, you’ll need:

To access your PSPDFKit API key, sign up for a free account. Your account lets you generate 100 documents for free every month. 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 invoice 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 invoice from an HTML template using our PHP PDF generation API. We created similar PDF invoice generation blog posts using sample code from other programming languages:

In addition to templates for generating invoices, we created free templates for other commonly used documents, like receipts, certificates, and reports. If you’re interested in generating other types of documents in PHP, check out the following posts:

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.

Related Products
Share Post
Free 60-Day Trial Try PSPDFKit in your app today.
Free Trial

Related Articles

Explore more
DESIGN  |  Web • Processor • API • PDF Generation • Tips

Adding Custom Fonts to HTML Documents

DESIGN  |  Web • Processor • API • PDF Generation • Tips

HTML-to-PDF Invoice Generation with Headers and Footers

TUTORIALS  |  API • Java • Tips

How to Delete PDF Pages Using Java