How to Convert PDF to BMP with PHP

Learn how to use pdfRest PDF to Images API Tool to convert PDF pages to BMP image files using PHP
Share this page

Convert PDF Pages to BMP Images with PHP

Applications sometimes need to deliver a PDF page to a component that works with bitmap images but cannot open PDFs. The pdfRest PDF to Images API Tool renders PDF pages as BMP files through the /bmp endpoint, providing a consistent API workflow without requiring PDF rendering software inside the PHP application.

BMP is typically chosen for compatibility with a receiving system that specifically requires it. Because BMP output can be substantially larger than compressed PNG or JPEG images, it is generally not the first choice for website thumbnails or bandwidth-sensitive delivery. pdfRest supports several image output formats through related endpoints, so the application can use BMP where required and choose a more compact format elsewhere.

For example, a PHP-based records portal can receive a multipage PDF and render selected pages as BMP files for a legacy OCR or imaging service that only accepts that format. The portal keeps the original PDF as the authoritative document, submits only the pages needed by the older component, and associates each returned image with its source page.

This example uses Guzzle to send a local PDF and rendering parameters as multipart form data.

PHP PDF-to-BMP Code Example

Install Guzzle with Composer, set the API key in secure configuration, and replace the local PDF path. The corrected sample declares the file as application/pdf; the placeholder in the older code is not a valid content type.

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.
use GuzzleHttp\Psr7\Utils; // Import the PSR-7 Utils class for working with streams.

// 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' => 'file', // Specify the field name for the file.
      'contents' => Utils::tryFopen('/path/to/file.pdf', 'r'), // Open the file specified by the '/path/to/file' for reading.
      'filename' => 'file.pdf', // Set the filename for the file to be converted, in this case, '/path/to/file'.
      'headers' => [
        'Content-Type' => 'application/pdf' // Set the Content-Type header for the file.
      ]
    ],
    [
      'name' => 'pages', // Specify the field name for the pages option.
      'contents' => '1-last' // Set the value for the pages option (in this case, '1-last').
    ],
    [
      'name' => 'resolution', // Specify the field name for the resolution option.
      'contents' => '300' // Set the value for the resolution option (in this case, '300').
    ],
    [
      'name' => 'color_model', // Specify the field name for the color_model option.
      'contents' => 'rgb' // Set the value for the color_model option (in this case, 'rgb').
    ],
    [
      'name' => 'output', // Specify the field name for the output option.
      'contents' => 'pdfrest_bmp' // Set the value for the output option (in this case, 'pdfrest_bmp').
    ]
  ]
];

$request = new Request('POST', $apiUrl.'/bmp', $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 converted BMP image.

Source: pdfRest PDF-to-BMP multipart PHP sample

Set the Rendering Parameters

Guzzle's multipart option contains the uploaded PDF and the endpoint fields. The pages value determines which pages are rendered. Use 1-last for the complete document or specify individual pages and ranges when the workflow needs only part of the file.

The resolution value determines pixel density. Higher DPI increases detail and file size, so select it according to the downstream requirement. A document-inspection or OCR process may benefit from more resolution than a simple screen preview, but increasing DPI cannot restore detail that is absent from the original page.

The color_model setting should match the consuming system. RGB is a common default; grayscale can reduce output size when color is unnecessary. The selected color model is applied consistently across every requested page, creating output aligned with the consuming system.

Process Multiple BMP Results

The endpoint generates one image for each selected PDF page. Parse the response as a collection that maps the generated outputs to the requested page range. Download the images from their output URLs when they need to leave the pdfRest workflow, or retain resource IDs for compatible follow-up operations. The Zip Files API Tool can combine several output resources when the application needs one downloadable package.

Catch Guzzle request and connection exceptions, inspect the HTTP status, and validate the response schema before using output fields. Apply timeouts appropriate to the expected PDF size and resolution. Keep the API key outside source code, close file resources, and avoid exposing document content in logs or error pages.

When the source PDF is already uploaded, use its resource ID in a JSON request. See the official PDF-to-BMP JSON-payload PHP sample. Use API Lab to configure output settings and consult the PDF to Images API reference for the current parameter ranges and response structure.

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