Skip to content

Commit

Permalink
= regular update
Browse files Browse the repository at this point in the history
  • Loading branch information
estarkov committed Nov 14, 2022
1 parent 6a1bd8a commit a073836
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
$uploadedFileUrl = $json["url"];

// 2. UPLOAD THE FILE TO CLOUD.

//echo json_encode($_FILES["file"]);
//print_r($_FILES["file"]);
$localFile = $_FILES["file"]["tmp_name"];
$fileHandle = fopen($localFile, "r");

Expand Down Expand Up @@ -107,6 +108,7 @@ function ExtractExcel($apiKey, $uploadedFileUrl, $pages)
$parameters = array();
$parameters["url"] = $uploadedFileUrl;
$parameters["pages"] = $pages;
$parameters["async"] = true; // (!) Make asynchronous job

// Create Json payload
$data = json_encode($parameters);
Expand All @@ -132,10 +134,97 @@ function ExtractExcel($apiKey, $uploadedFileUrl, $pages)

if (!isset($json["error"]) || $json["error"] == false)
{
$resultFileUrl = $json["url"];
// URL of generated XLSX file that will available after the job completion
$resultFileUrl = $json["url"];
// Asynchronous job ID
$jobId = $json["jobId"];

// Check the job status in a loop
do
{
$status = CheckJobStatus($jobId, $apiKey); // Possible statuses: "working", "failed", "aborted", "success".

// Display link to the file with conversion results
echo "<div><h2>Conversion Result:</h2><a href='" . $resultFileUrl . "' target='_blank'>" . $resultFileUrl . "</a></div>";
// Display timestamp and status (for demo purposes)
echo "<p>" . date(DATE_RFC2822) . ": " . $status . "</p>";

if ($status == "success")
{
// Display link to the file with conversion results
echo "<div><h2>Conversion Result:</h2><a href='" . $resultFileUrl . "' target='_blank'>" . $resultFileUrl . "</a></div>";
break;
}
else if ($status == "working")
{
// Pause for a few seconds
sleep(3);
}
else
{
echo $status . "<br/>";
break;
}
}
while (true);
}
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);
}

function CheckJobStatus($jobId, $apiKey)
{
$status = null;

// Create URL
$url = "https://api.pdf.co/v1/job/check";

// Prepare requests params
$parameters = array();
$parameters["jobid"] = $jobId;

// 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 (!isset($json["error"]) || $json["error"] == false)
{
$status = $json["status"];
}
else
{
Expand All @@ -158,9 +247,11 @@ function ExtractExcel($apiKey, $uploadedFileUrl, $pages)

// Cleanup
curl_close($curl);

return $status;
}

?>

</body>
</html>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
</p>
<p>
<label>Input File (*.pdf)</label>
<input type="hidden" name="MAX_FILE_SIZE" value="8000000"/>
<input type="hidden" name="MAX_FILE_SIZE" value="8000000000"/>
<input type="file" name="file"/>
</p>
<p>
<label>Comma-separated list of page indices (or ranges) to process. Leave empty for all pages. Example: '0,2-5,7-'.</label>
<br/>
<input type="number" name="pages" value="0">
<input type="text" name="pages" value="0">
</p>
<input type="submit" name="submit" value="Proceed" />
</form>
Expand Down

0 comments on commit a073836

Please sign in to comment.