How to Convert BMP to PDF with PHP

Learn how to use PHP to turn BMP images into PDF documents by calling the Convert to PDF API Tool from pdfRest.
Share this page

Why Convert BMP to PDF with PHP?

The pdfRest Convert to PDF API Tool is a powerful resource for developers looking to automate the conversion of various file types to PDF format. Utilizing this API, you can easily integrate PDF conversion functionality into your PHP applications, enhancing the capabilities of your software and providing a seamless user experience.

This tutorial will guide you through the process of sending an API call to Convert to PDF with PHP, illustrating how to interact with the pdfRest API to achieve your document processing goals.

In the real world, there are countless scenarios where converting documents to PDF can be beneficial. For instance, a business might need to convert invoices, reports, or contracts from their native formats into PDFs for archiving, sharing, or compliance purposes. By automating this process with a PHP script, the business can save time, reduce errors, and ensure consistency across all documents. The pdfRest API makes this automation possible, offering a reliable and straightforward method for converting documents to PDF.

Convert BMP 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' => 'compression',
      'contents' => 'lossless'
    ],
    [
      'name' => 'downsample',
      'contents' => '300'
    ],
    [
      'name' => 'tagged_pdf',
      'contents' => 'off'
    ],
    [
      'name' => 'output',
      'contents' => 'pdfrest_pdf'
    ]
  ]
];

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

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

echo $res->getBody();
?>

Source of the provided code: GitHub

Breaking Down the Code

The provided PHP script uses the Guzzle HTTP client to send an API request to the pdfRest Convert to PDF endpoint. Here's a breakdown of the key components:

require 'vendor/autoload.php';

This line includes the Composer-generated autoload file to automatically load the Guzzle HTTP client and other dependencies.

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

The $headers array contains the API key required for authentication with the pdfRest API.

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

The $options array specifies the multipart form data to be sent with the request. Each element in the 'multipart' array represents a different field to be included in the form data:

  • 'file': The file to be converted. It includes the file path, the file contents, and the Content-Type header.
  • 'compression': The compression setting for the PDF.
  • 'downsample': The downsample resolution for images within the PDF.
  • 'tagged_pdf': Whether to create a tagged PDF (useful for accessibility).
  • 'output': The name of the output file.
$request = new Request('POST', 'https://api.pdfrest.com/pdf', $headers);
$res = $client->sendAsync($request, $options)->wait();

A new HTTP POST request is created and sent asynchronously. The response is then waited on and the body of the response, which contains the processed PDF content, is outputted.

Beyond the Tutorial

By following the steps in this tutorial, you've learned how to make an API call to pdfRest's Convert to PDF endpoint using PHP. This allows you to programmatically convert files to PDF format, which can be integrated into various applications and workflows.

To explore and demo all of the pdfRest API Tools, visit the API Lab. For a comprehensive understanding of the capabilities and options available, refer to the API Reference Guide.

Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at GitHub.

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