How to convert PDF to XLSX from uploaded file for PDF to excel API in PHP using PDF.co Web API

How to convert PDF to XLSX from the 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, tools for document manipulation, splitting, and merging of pdf files. Includes built-in OCR, images recognition, 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. Full code snippet is also available at our GitHub repository at this location.

Our website provides free trial version of PDF.co Web API that includes source code samples to help with your PHP project.

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

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

Let’s review source code and it’s output, then we’ll review code briefly.

Source Code

pdf-to-excel.php

      
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>PDF To Excel Extraction Results</title> </head> <body> <?php // 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 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>

Output

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

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

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

We’re using PDF.co endpoint /v1/pdf/convert/to/xlsx to perform PDF to Excel conversion. As an input of 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 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 documentations for more options available for excel conversion.

To get more out of this article, please try to implement this program in 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 Samples: