How to Convert PDF to XLSX from Uploaded File for PDF to Excel API in PHP and PDF.co Web API

PDF.co Web API: the flexible Web API that includes a full set of functions from e-signature requests to data extraction, OCR, images recognition, PDF splitting and PDF merging. It can also generate barcodes and read barcodes from images, scans and PDF.

How to Convert PDF to XLSX from Uploaded File for PDF to Excel API in PHP: Step-By-Step Tutorial

The coding tutorials are designed to help you test the features without the need to write your own code. PDF.co Web API was made to help with PDF to Excel API in PHP. PDF.co Web API is the Rest API that provides a set of data extraction functions, and tools for document manipulation, splitting, and merging of PDF files. Includes built-in OCR, and image recognition, and can generate and read barcodes from images, scans, and PDF.

PHP code samples for PHP developers help to speed up the application’s code writing when using PDF.co Web API. Follow the instruction and copy–paste code for PHP into your project’s code editor. You can use these PHP sample examples in one or many applications. The full code snippet is also available at our GitHub repository at this location.

On-demand (REST Web API) version:
 Web API (on-demand version)

On-premise offline SDK for Windows:
 60 Day Free Trial (on-premise)

pdf-to-excel.php

      
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>PDF To Excel Extraction Results</title> </head> <body> <?php // Note: If you have input files large than 200kb we highly recommend to check "async" mode example. // Get submitted form data $apiKey = $_POST["apiKey"]; // The authentication key (API Key). Get your own by registering at https://app.pdf.co/documentation/api $pages = $_POST["pages"]; // 1. RETRIEVE THE PRESIGNED URL TO UPLOAD THE FILE. // * If you already have the direct PDF file link, go to the step 3. // Create URL $url = "https://api.pdf.co/v1/file/upload/get-presigned-url" . "?name=" . $_FILES["file"]["name"] . "&contenttype=application/octet-stream"; // Create request $curl = curl_init(); curl_setopt($curl, CURLOPT_HTTPHEADER, array("x-api-key: " . $apiKey)); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // Execute request $result = curl_exec($curl); if (curl_errno($curl) == 0) { $status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); if ($status_code == 200) { $json = json_decode($result, true); // Get URL to use for the file upload $uploadFileUrl = $json["presignedUrl"]; // Get URL of uploaded file to use with later API calls $uploadedFileUrl = $json["url"]; // 2. UPLOAD THE FILE TO CLOUD. $localFile = $_FILES["file"]["tmp_name"]; $fileHandle = fopen($localFile, "r"); curl_setopt($curl, CURLOPT_URL, $uploadFileUrl); curl_setopt($curl, CURLOPT_HTTPHEADER, array("content-type: application/octet-stream")); curl_setopt($curl, CURLOPT_PUT, true); curl_setopt($curl, CURLOPT_INFILE, $fileHandle); curl_setopt($curl, CURLOPT_INFILESIZE, filesize($localFile)); // Execute request curl_exec($curl); fclose($fileHandle); if (curl_errno($curl) == 0) { $status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); if ($status_code == 200) { // 3. CONVERT UPLOADED PDF FILE TO Excel ExtractExcel($apiKey, $uploadedFileUrl, $pages); } else { // Display request error echo "<p>Status code: " . $status_code . "</p>"; echo "<p>" . $result . "</p>"; } } else { // Display CURL error echo "Error: " . curl_error($curl); } } else { // Display service reported error echo "<p>Status code: " . $status_code . "</p>"; echo "<p>" . $result . "</p>"; } curl_close($curl); } else { // Display CURL error echo "Error: " . curl_error($curl); } function ExtractExcel($apiKey, $uploadedFileUrl, $pages) { // Create URL $url = "https://api.pdf.co/v1/pdf/convert/to/xlsx"; // (!) If you need the old XLS format use `https://api.pdf.co/v1/pdf/convert/to/xls` endpoint, // Prepare requests params $parameters = array(); $parameters["url"] = $uploadedFileUrl; $parameters["pages"] = $pages; // Create Json payload $data = json_encode($parameters); // Create request $curl = curl_init(); curl_setopt($curl, CURLOPT_HTTPHEADER, array("x-api-key: " . $apiKey, "Content-type: application/json")); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Execute request $result = curl_exec($curl); if (curl_errno($curl) == 0) { $status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); if ($status_code == 200) { $json = json_decode($result, true); if ($json["error"] == false) { $resultFileUrl = $json["url"]; // Display link to the file with conversion results echo "<div><h2>Conversion Result:</h2><a href='" . $resultFileUrl . "' target='_blank'>" . $resultFileUrl . "</a></div>"; } else { // Display service reported error echo "<p>Error: " . $json["message"] . "</p>"; } } else { // Display request error echo "<p>Status code: " . $status_code . "</p>"; echo "<p>" . $result . "</p>"; } } else { // Display CURL error echo "Error: " . curl_error($curl); } // Cleanup curl_close($curl); } ?> </body> </html>

Code Analysis

PDFco_PDFtoExcel_PHP
Now that we’ve already seen the source code and its output let’s analyze the code. Source code consists of an HTML file containing input controls and displays the output. Input controls are within the form and that form submit event is handled by the PHP file pdf-to-excel.php.

PHP file contains logic

This PHP file pdf-to-excel.php contains logic to convert PDF files to Excel. We can logically divide this into two parts. One part would be uploading the input PDF file and getting its public URL and another part is processing PDF to Excel conversion using that output URL.

How to perform the upload

PDF.co endpoint /v1/file/upload/get-presigned-url is used to perform upload of input file. The input file name is passed as input to this request. Please note, that we’re not performing actual upload of any file here but just providing input file names. Response of this API request contains pre-signed URL ($json[“presignedUrl”]) and public URL ($json[“url”]) of the uploaded file. This pre-signed URL is being used to upload the actual files to PDF.co and the public URL points to that file. This public URL is temporary with a life span of a few minutes.

How to perform the PDF to Excel conversion

We’re using PDF.co endpoint /v1/pdf/convert/to/xlsx to perform PDF to Excel conversion. As an input to this request, we’re providing the URL of the source file and pages whose data needs to be converted to XLSX. The response of this API call contains the URL of the converted Excel file. If the input file is big, we can call this API in the asynchronous mode also.

This sample contains a very basic use case just to demonstrate how easy it is to convert from PDF to Excel with PDF.co. Please refer to the documentation for more options available for Excel conversion.

To get more out of this article, please try to implement this program on your machine. Thank you for reading!

VIDEO

ON-PREMISE OFFLINE SDK

Get 60 Day Free Trial

See also:

ON-DEMAND REST WEB API

Get Your API Key

See also:

Related Pages:

Related Samples: