Blog Post

How to Generate PDF Certificates from HTML in PHP

Illustration: How to Generate PDF Certificates from HTML in PHP

In this post, you’ll learn how to generate PDF certificates 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.

Certificates are most commonly generated by educational institutions, online course platforms, and corporate training programs. PDF certificates can automatically be generated when a student or employee successfully completes their program. To further streamline the process, a student’s name, program, and completion date can be pulled from a database and dynamically inserted into the certificate.

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

Requirements

To get started, you’ll need:

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 certificate template and extract the contents of the ZIP file into a folder. You’ll get an HTML file, Inter fonts, a Space Mono font, SVG logos, 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 logos, 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",
				"ribbon.svg"
      ]
    }
  ]
}';

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'),
		'ribbon.svg' => new CURLFILE('ribbon.svg')
  ),
  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 logos, 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",
				"ribbon.svg"
      ]
    }
  ]
}';

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'),
		'ribbon.svg' => new CURLFILE('ribbon.svg')
  ),
  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 certificate from an HTML template using our PHP PDF generation API. We created similar PDF certificate generation blog posts using sample code from other programming languages:

In addition to templates for generating certificates, we created free templates for other commonly used documents, like receipts, invoices, 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.

Author
Hulya Masharipov Technical Writer

Hulya is a frontend web developer and technical writer at Nutrient who enjoys creating responsive, scalable, and maintainable web experiences. She’s passionate about open source, web accessibility, cybersecurity privacy, and blockchain.

Related products
Share post
Free trial Ready to get started?
Free trial