Use the sample source code below to extract and convert spreadsheets between various formats such as TXT, XLS, XLSX, XML, CSV, PDF, HTML in PHP using ByteScout Cloud API.
You may also find useful the code snippets for other languages: Java, JavaScript, and jQuery.
Let’s see code first, then we’ll analyze it.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Spreadsheet Cloud API Example</title> </head></pre> <body> <form name="form1" enctype="multipart/form-data" method="post" action="spreadsheet.php"> <p> <label>Copy-paste your API Key for api.pdf.co here</label> <input type="text" name="apiKey" placeholder="API Key"/> </p> <p> <label>Input File</label> <input type="hidden" name="MAX_FILE_SIZE" value="8000000"/> <input type="file" name="file"/> </p> <p> <label>Convert To</label> <select name="targetFormat"> <option value="CSV">CSV</option> <option value="HTML">HTML</option> <option value="TXT">TXT</option> <option value="XLS">XLS</option> <option value="XLSX">XLSX</option> <option value="XML">XML</option> <option value="PDF">PDF</option> </select> </p> <input type="submit" name="submit" value="Proceed" /> </form> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Spreadsheet Conversion Results</title> </head> <body></pre> <?php // Get submitted form data $apiKey = $_POST["apiKey"]; $targetFormat = $_POST["targetFormat"]; // 1. UPLOAD FILE // Create File API URL $url = "https://api.pdf.co/api/v1/file/upload?apiKey=" . $apiKey; // Get your API key at http://www.api.pdf.co/ // Create request $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, array( "file" => "@" . $_FILES["file"]["tmp_name"] . ";filename=" . $_FILES["file"]["name"] )); // Execute request $result = curl_exec($curl); if (curl_errno($curl)) { // Display request error echo "Error: " . curl_error($curl); } else // Display request results { $status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); if ($status_code == 200) { // 2. Convert spreadsheet convertSpreadsheet($apiKey, $targetFormat, $result); } else { // Display service reported error echo "<p>Status code: " . $status_code . "</p>"; echo "<p>" . $result . "</p>"; } } // Cleanup curl_close($curl); function convertSpreadsheet($apiKey, $targetFormat, $uploadedFileId) { // Create Spreadsheet API URL $url = "https://api.pdf.co/api/v1/spreadsheet/convert?apiKey=" . $apiKey; // Create Spreadsheet API options $options = array( "properties" => array( "convertType" => $targetFormat, "createNavigationLinks" => true, "autoSize" => false ), "inputType" => "fileId", "input" => $uploadedFileId, "outputType" => "link" ); // Create request $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json")); curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($options)); // Execute request $result = curl_exec($curl); if (curl_errno($curl)) { // Display request error echo "Error: " . curl_error($curl); } else // Display request results { $status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); if ($status_code == 200) { // Display conversion result echo "<div><h2>Conversion Result:</h2><a href='" . $result . "' target='_blank'>" . $result . "</a></div>"; } else { // Display service reported errors echo "<p>Status code: " . $status_code . "</p>"; echo "<p>" . $result . "</p>"; } } // Cleanup curl_close($curl); } ?> </body> </html>
This html file contains structure for converting uploaded spreadsheet documents. Key structure elements are as following:
This php script file handles form post event, and performs spreadsheet conversion to format selected in dropdown. Let’s analyze main parts of logic.
We are fetching api key and target format in form post request.
$apiKey = $_POST["apiKey"];
$targetFormat = $_POST["targetFormat"];
After these information is received, we’ll continue with uploading file to cloud server.
// Create File API URL
$url = "https://api.pdf.co/api/v1/file/upload?apiKey=" . $apiKey;
We’re preparing file upload url, by passing api key received in form post. After url is set, POST event is executed with passing user uploaded file. Once file upload result is received and file is successfully uploaded to cloud, we’ll further process it by calling function “convertSpreadsheet“.
In function “convertSpreadsheet“, first of all we’re generating api call url by passing key to it.
$url = "https://api.pdf.co/api/v1/spreadsheet/convert?apiKey=" . $apiKey;
Next, we’re preparing api request options.
$options = array(
"properties" => array(
"convertType" => $targetFormat,
"createNavigationLinks" => true,
"autoSize" => false
),
"inputType" => "fileId",
"input" => $uploadedFileId,
"outputType" => "link"
);
Followings are the options we’re setting for this demo application. If you want to go through all options available, please refer api documentations.
Property Name | Usage |
convertType | To which format spreadsheet needs to be converted. Many options available such as “CSV”, “HTML”, “TXT”, “XLS”, “XLSX”, “XML” and “PDF”. |
createNavigationLinks | Indicates weather navigation links to be generated in result document. |
autoSize | Indicates whether spreadsheet workbook data should autosize as per result document. |
inputType | Represents which type of input we’re passing. In this case we are specifying “fileId”. |
input | Represents actual input passed. Here, we’re passing result received uploaded file cloud url. |
outputType | This field represents which type of output user intends to received. We’ve set “link” here, hence output file’s url will be received as result. |
After we have url and parameters ready, we’re simply executing post request and processing response. Here response will be result file url.
That’s all guys. With ByteScout Api it’s that simple to convert spreadsheet input file to desired output format.
Happy Coding!
IMPORTANT:
Cloud API is deprecated and was replaced with more powerful and secure www.PDF.co Web API
CLICK HERE
TO LEARN MORE
ABOUT NEW
www.PDF.co
w/ Web API
On-Premise API Server
Cloud API Server