How to Convert TIFF to PDF with PHP

Learn how to change TIFF images into PDF documents using the Convert to PDF API Tool in the pdfRest Cloud API.
Share this page

Why Use Convert to PDF with PHP?

The pdfRest Convert to PDF API Tool allows developers to convert various file formats into PDFs. This tutorial will guide you through sending an API call to Convert to PDF using PHP. Imagine you have an application that generates images in different formats, such as TIFF or JPEG, and you need to convert these into a standardized PDF format for distribution. This is where the Convert to PDF API comes in handy.

Convert to PDF with PHP Code Example

 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
];

$options = [
  'multipart' => [
    [
      'name' => 'file',
      'contents' => Utils::tryFopen('/path/to/file', 'r'),
      'filename' => '/path/to/file',
      'headers' => [
        'Content-Type' => ''
      ]
    ],
    [
      'name' => 'output',
      'contents' => 'pdfrest_pdf'
    ]
  ]
];

$request = new Request('POST', 'https://api.pdfrest.com/pdf', $headers);

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

echo $res->getBody();
?>

Reference to the source code: https://github.com/datalogics/pdf-rest-api-samples/blob/main/PHP/Endpoint%20Examples/Multipart%20Payload/pdf.php

Breaking Down the Code

The code example above demonstrates how to make a multipart/form-data POST request to the pdfRest API to convert a file to PDF. Let's break down the key parts of the code:

require 'vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Utils;

This section includes the necessary libraries and sets up the namespaces needed to make HTTP requests with Guzzle, a PHP HTTP client.

$headers = [
  'Api-Key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
];

The API key is required for authentication with the pdfRest API. Replace 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' with your actual API key.

$options = [
  'multipart' => [
    // ... multipart form fields ...
  ]
];

This array contains the multipart form fields. Each field corresponds to an option or file required by the API. The 'file' field includes the file contents, the 'compression' field sets the compression level, 'downsample' specifies the downsample rate, 'tagged_pdf' indicates whether to create a tagged PDF, and 'output' names the output file.

$request = new Request('POST', 'https://api.pdfrest.com/pdf', $headers);
$res = $client->sendAsync($request, $options)->wait();
echo $res->getBody();

A new POST request is created with the specified URL and headers. The request is sent asynchronously, and the script waits for the response. The response body, which contains the processed PDF content, is then outputted.

Beyond the Tutorial

In this tutorial, we have successfully made a call to the pdfRest Convert to PDF API using PHP. This allows for the conversion of files to PDF format programmatically. To explore all of the pdfRest API Tools, visit the API Lab at https://pdfrest.com/apilab/. For more detailed information, refer to the API Reference documentation at https://pdfrest.com/documentation/.

Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at https://github.com/datalogics/pdf-rest-api-samples/tree/main/PHP/Endpoint%20Examples/JSON%20Payload/pdf.php.

Generate a self-service API Key now!

Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.

Compare Plans
Contact Us