How to Convert PDF to Excel with PHP

Learn how you can use PHP to call the PDF to Excel API Tool from pdfRest. Convert your PDF formatted file into Microsoft Excel easily.
Share this page

Why Convert PDF to Excel with PHP?

The pdfRest PDF to Excel API Tool is a powerful resource for developers looking to automate the conversion of PDF documents into Excel spreadsheets. This capability is particularly useful in scenarios where data needs to be extracted from PDFs for analysis or reporting purposes. For example, a financial analyst might need to convert bank statements provided in PDF format into Excel to perform further calculations and data manipulation.

This tutorial will demonstrate how to send an API call to the PDF to Excel endpoint using PHP, allowing for seamless integration of this functionality into PHP-based applications or workflows.

PDF to Excel 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_excel'
    ]
  ]
];

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

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

echo $res->getBody();
?>

Source: GitHub Repository

Breaking Down the PHP Code

The provided PHP code utilizes the Guzzle HTTP client to send an API request to pdfRest's PDF to Excel conversion service. Here's a breakdown of the key components:

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

This snippet includes the necessary classes from the Guzzle library and initializes the HTTP client.

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

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

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

The $options array specifies the multipart payload for the request. The 'file' part includes the file to be converted, and the 'output' part indicates the desired output option.

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

A new POST request is created with the endpoint URL and headers, and the request is sent asynchronously. The response body, which contains the Excel document, is then outputted.

Beyond the Tutorial

By following the steps above, you've learned how to convert a PDF document to an Excel spreadsheet using the pdfRest API with PHP. This can be a significant timesaver and can help streamline data processing tasks.

To explore more capabilities and demo all of the pdfRest API Tools, visit the API Lab. For further reference and documentation, check out the Cloud API Reference Guide.

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

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