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

Why Convert PDF to BMP with PHP?

The pdfRest PDF to Images API Tool provides a convenient way to convert PDF documents into image files programmatically. In this tutorial, we'll demonstrate how to send an API call to the PDF to Images API using PHP.

This functionality is particularly useful in scenarios where you need to display PDF content as images on a website, extract images for further processing, or create thumbnails for document previews.

Convert PDF to BMP 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' => 'pages',
      'contents' => '1-last'
    ],
    [
      'name' => 'resolution',
      'contents' => '300'
    ],
    [
      'name' => 'color_model',
      'contents' => 'rgb'
    ],
    [
      'name' => 'output',
      'contents' => 'pdfrest_bmp'
    ]
  ]
];

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

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

echo $res->getBody();
?>

Source: GitHub - datalogics/pdf-rest-api-samples

Breaking Down the Code

The code provided above is a PHP script that uses the Guzzle HTTP client to make a multipart POST request to the pdfRest API, specifically to the endpoint for converting a PDF to BMP images.

The $headers array includes the 'Api-Key' required for authentication with the API.

The $options array contains the 'multipart' field, which is an array of arrays representing the different parts of the multipart request:

  • 'file': The PDF file to be converted. The 'contents' key should be a stream of the file, and the 'filename' key should be the path to the file.
  • 'pages': The range of pages to convert, e.g., '1-last' to convert all pages.
  • 'resolution': The resolution of the output images in DPI (dots per inch).
  • 'color_model': The color model of the output images, e.g., 'rgb' for Red-Green-Blue.
  • 'output': The prefix for the output file names.

The Request object is created with the HTTP method 'POST', the API endpoint URL, and the headers. The request is sent asynchronously, and the response is waited upon. Finally, the response body is echoed, which contains the converted BMP images.

Beyond the Tutorial

By following the steps above, you've learned how to make an API call to the pdfRest PDF to Images API using PHP. You can now integrate this functionality into your PHP applications to convert PDFs to images programmatically.

To explore all of the pdfRest API Tools, visit the API Lab, and for more detailed information, refer to the API Reference documentation.

Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at GitHub - datalogics/pdf-rest-api-samples.

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