How to Create a Blank PDF with PHP

Learn how to use the pdfRest Create Blank PDF API with PHP to generate an empty PDF that can be filled with content.
Share this page

Why Create Blank PDF with PHP?

The pdfRest Create Blank PDF API Tool offers a convenient way to generate blank PDF documents programmatically. This tutorial will guide you through the process of sending an API call to create a blank PDF using PHP. By leveraging the capabilities of the pdfRest API, developers can automate the creation of PDF documents with specific attributes such as page size, count, and orientation.

Imagine a scenario where a company needs to generate blank PDF templates for internal documentation or forms that require manual filling. Using the Create Blank PDF API, developers can automate the production of these templates, ensuring consistency and saving time. This API is particularly useful for applications that need to dynamically generate documents based on user input or other criteria.

Create Blank PDF with PHP Code Example

require 'vendor/autoload.php'; // Require the autoload file to load Guzzle HTTP client.

use GuzzleHttp\Client; // Import the Guzzle HTTP client namespace.
use GuzzleHttp\Psr7\Request; // Import the PSR-7 Request class.

// By default, we use the US-based API service. This is the primary endpoint for global use.
$apiUrl = "https://api.pdfrest.com";

/* For GDPR compliance and enhanced performance for European users, you can switch to the EU-based service by uncommenting the URL below.
 * For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work
 */
//$apiUrl = "https://eu-api.pdfrest.com";

$client = new Client(); // Create a new instance of the Guzzle HTTP client.

$headers = [
  'Api-Key' =--> 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' // Set the API key in the headers for authentication.
];

$options = [
  'multipart' => [
    [
      'name' => 'page_size',
      'contents' => 'letter'
    ],
    [
      'name' => 'page_count',
      'contents' => '3'
    ],
    [
      'name' => 'page_orientation',
      'contents' => 'portrait'
    ]
  ]
];

$request = new Request('POST', $apiUrl.'/blank-pdf', $headers); // Create a new HTTP POST request with the API endpoint and headers.

$res = $client->sendAsync($request, $options)->wait(); // Send the asynchronous request and wait for the response.

echo $res->getBody(); // Output the response body, which contains the JSON metadata for the blank PDF.

Source: GitHub

Breaking Down the Code

require 'vendor/autoload.php';

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;

The

$apiUrl

$client = new Client();

The

$headers
'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'

The

$options
'page_size'
'page_count'
'page_orientation'

$request = new Request('POST', $apiUrl.'/blank-pdf', $headers);

$res = $client->sendAsync($request, $options)->wait();

echo $res->getBody();

Beyond the Tutorial

In this tutorial, you learned how to use the pdfRest Create Blank PDF API tool to generate a blank PDF document using PHP. This approach can be extended to automate the creation of various document templates in your applications.

For further exploration, you are encouraged to try out all the pdfRest API Tools available in the API Lab. Detailed documentation can be found in the API Reference Guide. This example demonstrates a multipart API call, and code samples using JSON payloads can be found here.

Generate a self-service API Key now!
Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.