Skip to content

Commit

Permalink
Add upload + raw JSON php samples
Browse files Browse the repository at this point in the history
  • Loading branch information
datalogics-tsmith committed Nov 6, 2023
1 parent 62f4128 commit c713544
Show file tree
Hide file tree
Showing 31 changed files with 1,090 additions and 0 deletions.
3 changes: 3 additions & 0 deletions PHP/Endpoint Examples/JSON Payload/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This directory contains examples of how to call each endpoint of the
PdfRest API using PHP. Please refer to https://pdfrest.com/documentation.html
for accepted inputs, options, and other specifics about individual endpoints.
33 changes: 33 additions & 0 deletions PHP/Endpoint Examples/JSON Payload/bmp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
require 'vendor/autoload.php'; // Require the autoload file to load Guzzle HTTP client.

use GuzzleHttp\Client; // Import the Guzzle HTTP client namespace.
use GuzzleHttp\Psr7\Request; // Import the PSR-7 Request class.
use GuzzleHttp\Psr7\Utils; // Import the PSR-7 Utils class for working with streams.

$upload_client = new Client(['http_errors' => false]);
$upload_headers = [
'api-key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
'content-filename' => 'filename.pdf',
'Content-Type' => 'application/octet-stream'
];
$upload_body = file_get_contents('/path/to/file');
$upload_request = new Request('POST', 'https://api.pdfrest.com/upload', $upload_headers, $upload_body);
$upload_res = $upload_client->sendAsync($upload_request)->wait();
echo $upload_res->getBody() . PHP_EOL;

$upload_response_json = json_decode($upload_res->getBody());

$uploaded_id = $upload_response_json->{'files'}[0]->{'id'};

echo "Successfully uploaded with an id of: " . $uploaded_id . PHP_EOL;

$bmp_client = new Client(['http_errors' => false]);
$bmp_headers = [
'api-key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
'Content-Type' => 'application/json'
];
$bmp_body = '{"id":"'.$uploaded_id.'"}';
$bmp_request = new Request('POST', 'https://api.pdfrest.com/bmp', $bmp_headers, $bmp_body);
$bmp_res = $bmp_client->sendAsync($bmp_request)->wait();
echo $bmp_res->getBody() . PHP_EOL;
33 changes: 33 additions & 0 deletions PHP/Endpoint Examples/JSON Payload/compressed-pdf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
require 'vendor/autoload.php'; // Require the autoload file to load Guzzle HTTP client.

use GuzzleHttp\Client; // Import the Guzzle HTTP client namespace.
use GuzzleHttp\Psr7\Request; // Import the PSR-7 Request class.
use GuzzleHttp\Psr7\Utils; // Import the PSR-7 Utils class for working with streams.

$upload_client = new Client(['http_errors' => false]);
$upload_headers = [
'api-key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
'content-filename' => 'filename.pdf',
'Content-Type' => 'application/octet-stream'
];
$upload_body = file_get_contents('/path/to/file');
$upload_request = new Request('POST', 'https://api.pdfrest.com/upload', $upload_headers, $upload_body);
$upload_res = $upload_client->sendAsync($upload_request)->wait();
echo $upload_res->getBody() . PHP_EOL;

$upload_response_json = json_decode($upload_res->getBody());

$uploaded_id = $upload_response_json->{'files'}[0]->{'id'};

echo "Successfully uploaded with an id of: " . $uploaded_id . PHP_EOL;

$compress_client = new Client(['http_errors' => false]);
$compress_headers = [
'api-key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
'Content-Type' => 'application/json'
];
$compress_body = '{"id":"'.$uploaded_id.'", "compression_level": "medium"}';
$compress_request = new Request('POST', 'https://api.pdfrest.com/compressed-pdf', $compress_headers, $compress_body);
$compress_res = $compress_client->sendAsync($compress_request)->wait();
echo $compress_res->getBody() . PHP_EOL;
33 changes: 33 additions & 0 deletions PHP/Endpoint Examples/JSON Payload/decrypted-pdf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
require 'vendor/autoload.php'; // Require the autoload file to load Guzzle HTTP client.

use GuzzleHttp\Client; // Import the Guzzle HTTP client namespace.
use GuzzleHttp\Psr7\Request; // Import the PSR-7 Request class.
use GuzzleHttp\Psr7\Utils; // Import the PSR-7 Utils class for working with streams.

$upload_client = new Client(['http_errors' => false]);
$upload_headers = [
'api-key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
'content-filename' => 'filename.pdf',
'Content-Type' => 'application/octet-stream'
];
$upload_body = file_get_contents('/path/to/file');
$upload_request = new Request('POST', 'https://api.pdfrest.com/upload', $upload_headers, $upload_body);
$upload_res = $upload_client->sendAsync($upload_request)->wait();
echo $upload_res->getBody() . PHP_EOL;

$upload_response_json = json_decode($upload_res->getBody());

$uploaded_id = $upload_response_json->{'files'}[0]->{'id'};

echo "Successfully uploaded with an id of: " . $uploaded_id . PHP_EOL;

$decrypt_client = new Client(['http_errors' => false]);
$decrypt_headers = [
'api-key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
'Content-Type' => 'application/json'
];
$decrypt_body = '{"id":"'.$uploaded_id.'", "current_open_password": "password"}';
$decrypt_request = new Request('POST', 'https://api.pdfrest.com/decrypted-pdf', $decrypt_headers, $decrypt_body);
$decrypt_res = $decrypt_client->sendAsync($decrypt_request)->wait();
echo $decrypt_res->getBody() . PHP_EOL;
33 changes: 33 additions & 0 deletions PHP/Endpoint Examples/JSON Payload/encrypted-pdf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
require 'vendor/autoload.php'; // Require the autoload file to load Guzzle HTTP client.

use GuzzleHttp\Client; // Import the Guzzle HTTP client namespace.
use GuzzleHttp\Psr7\Request; // Import the PSR-7 Request class.
use GuzzleHttp\Psr7\Utils; // Import the PSR-7 Utils class for working with streams.

$upload_client = new Client(['http_errors' => false]);
$upload_headers = [
'api-key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
'content-filename' => 'filename.pdf',
'Content-Type' => 'application/octet-stream'
];
$upload_body = file_get_contents('/path/to/file');
$upload_request = new Request('POST', 'https://api.pdfrest.com/upload', $upload_headers, $upload_body);
$upload_res = $upload_client->sendAsync($upload_request)->wait();
echo $upload_res->getBody() . PHP_EOL;

$upload_response_json = json_decode($upload_res->getBody());

$uploaded_id = $upload_response_json->{'files'}[0]->{'id'};

echo "Successfully uploaded with an id of: " . $uploaded_id . PHP_EOL;

$encrypt_client = new Client(['http_errors' => false]);
$encrypt_headers = [
'api-key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
'Content-Type' => 'application/json'
];
$encrypt_body = '{"id":"'.$uploaded_id.'", "new_open_password": "password"}';
$encrypt_request = new Request('POST', 'https://api.pdfrest.com/encrypted-pdf', $encrypt_headers, $encrypt_body);
$encrypt_res = $encrypt_client->sendAsync($encrypt_request)->wait();
echo $encrypt_res->getBody() . PHP_EOL;
33 changes: 33 additions & 0 deletions PHP/Endpoint Examples/JSON Payload/exported-form-data.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
require 'vendor/autoload.php'; // Require the autoload file to load Guzzle HTTP client.

use GuzzleHttp\Client; // Import the Guzzle HTTP client namespace.
use GuzzleHttp\Psr7\Request; // Import the PSR-7 Request class.
use GuzzleHttp\Psr7\Utils; // Import the PSR-7 Utils class for working with streams.

$upload_client = new Client(['http_errors' => false]);
$upload_headers = [
'api-key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
'content-filename' => 'filename.pdf',
'Content-Type' => 'application/octet-stream'
];
$upload_body = file_get_contents('/path/to/file');
$upload_request = new Request('POST', 'https://api.pdfrest.com/upload', $upload_headers, $upload_body);
$upload_res = $upload_client->sendAsync($upload_request)->wait();
echo $upload_res->getBody() . PHP_EOL;

$upload_response_json = json_decode($upload_res->getBody());

$uploaded_id = $upload_response_json->{'files'}[0]->{'id'};

echo "Successfully uploaded with an id of: " . $uploaded_id . PHP_EOL;

$export_client = new Client(['http_errors' => false]);
$export_headers = [
'api-key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
'Content-Type' => 'application/json'
];
$export_body = '{"id":"'.$uploaded_id.'", "data_format": "xml"}';
$export_request = new Request('POST', 'https://api.pdfrest.com/exported-form-data', $export_headers, $export_body);
$export_res = $export_client->sendAsync($export_request)->wait();
echo $export_res->getBody() . PHP_EOL;
33 changes: 33 additions & 0 deletions PHP/Endpoint Examples/JSON Payload/extracted-text.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
require 'vendor/autoload.php'; // Require the autoload file to load Guzzle HTTP client.

use GuzzleHttp\Client; // Import the Guzzle HTTP client namespace.
use GuzzleHttp\Psr7\Request; // Import the PSR-7 Request class.
use GuzzleHttp\Psr7\Utils; // Import the PSR-7 Utils class for working with streams.

$upload_client = new Client(['http_errors' => false]);
$upload_headers = [
'api-key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
'content-filename' => 'filename.pdf',
'Content-Type' => 'application/octet-stream'
];
$upload_body = file_get_contents('/path/to/file');
$upload_request = new Request('POST', 'https://api.pdfrest.com/upload', $upload_headers, $upload_body);
$upload_res = $upload_client->sendAsync($upload_request)->wait();
echo $upload_res->getBody() . PHP_EOL;

$upload_response_json = json_decode($upload_res->getBody());

$uploaded_id = $upload_response_json->{'files'}[0]->{'id'};

echo "Successfully uploaded with an id of: " . $uploaded_id . PHP_EOL;

$extract_client = new Client(['http_errors' => false]);
$extract_headers = [
'api-key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
'Content-Type' => 'application/json'
];
$extract_body = '{"id":"'.$uploaded_id.'"}';
$extract_request = new Request('POST', 'https://api.pdfrest.com/extracted-text', $extract_headers, $extract_body);
$extract_res = $extract_client->sendAsync($extract_request)->wait();
echo $extract_res->getBody() . PHP_EOL;
33 changes: 33 additions & 0 deletions PHP/Endpoint Examples/JSON Payload/flattened-annotations-pdf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
require 'vendor/autoload.php'; // Require the autoload file to load Guzzle HTTP client.

use GuzzleHttp\Client; // Import the Guzzle HTTP client namespace.
use GuzzleHttp\Psr7\Request; // Import the PSR-7 Request class.
use GuzzleHttp\Psr7\Utils; // Import the PSR-7 Utils class for working with streams.

$upload_client = new Client(['http_errors' => false]);
$upload_headers = [
'api-key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
'content-filename' => 'filename.pdf',
'Content-Type' => 'application/octet-stream'
];
$upload_body = file_get_contents('/path/to/file');
$upload_request = new Request('POST', 'https://api.pdfrest.com/upload', $upload_headers, $upload_body);
$upload_res = $upload_client->sendAsync($upload_request)->wait();
echo $upload_res->getBody() . PHP_EOL;

$upload_response_json = json_decode($upload_res->getBody());

$uploaded_id = $upload_response_json->{'files'}[0]->{'id'};

echo "Successfully uploaded with an id of: " . $uploaded_id . PHP_EOL;

$flatten_client = new Client(['http_errors' => false]);
$flatten_headers = [
'api-key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
'Content-Type' => 'application/json'
];
$flatten_body = '{"id":"'.$uploaded_id.'"}';
$flatten_request = new Request('POST', 'https://api.pdfrest.com/flattened-annotations-pdf', $flatten_headers, $flatten_body);
$flatten_res = $flatten_client->sendAsync($flatten_request)->wait();
echo $flatten_res->getBody() . PHP_EOL;
33 changes: 33 additions & 0 deletions PHP/Endpoint Examples/JSON Payload/flattened-forms-pdf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
require 'vendor/autoload.php'; // Require the autoload file to load Guzzle HTTP client.

use GuzzleHttp\Client; // Import the Guzzle HTTP client namespace.
use GuzzleHttp\Psr7\Request; // Import the PSR-7 Request class.
use GuzzleHttp\Psr7\Utils; // Import the PSR-7 Utils class for working with streams.

$upload_client = new Client(['http_errors' => false]);
$upload_headers = [
'api-key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
'content-filename' => 'filename.pdf',
'Content-Type' => 'application/octet-stream'
];
$upload_body = file_get_contents('/path/to/file');
$upload_request = new Request('POST', 'https://api.pdfrest.com/upload', $upload_headers, $upload_body);
$upload_res = $upload_client->sendAsync($upload_request)->wait();
echo $upload_res->getBody() . PHP_EOL;

$upload_response_json = json_decode($upload_res->getBody());

$uploaded_id = $upload_response_json->{'files'}[0]->{'id'};

echo "Successfully uploaded with an id of: " . $uploaded_id . PHP_EOL;

$flatten_client = new Client(['http_errors' => false]);
$flatten_headers = [
'api-key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
'Content-Type' => 'application/json'
];
$flatten_body = '{"id":"'.$uploaded_id.'"}';
$flatten_request = new Request('POST', 'https://api.pdfrest.com/flattened-forms-pdf', $flatten_headers, $flatten_body);
$flatten_res = $flatten_client->sendAsync($flatten_request)->wait();
echo $flatten_res->getBody() . PHP_EOL;
33 changes: 33 additions & 0 deletions PHP/Endpoint Examples/JSON Payload/flattened-layers-pdf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
require 'vendor/autoload.php'; // Require the autoload file to load Guzzle HTTP client.

use GuzzleHttp\Client; // Import the Guzzle HTTP client namespace.
use GuzzleHttp\Psr7\Request; // Import the PSR-7 Request class.
use GuzzleHttp\Psr7\Utils; // Import the PSR-7 Utils class for working with streams.

$upload_client = new Client(['http_errors' => false]);
$upload_headers = [
'api-key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
'content-filename' => 'filename.pdf',
'Content-Type' => 'application/octet-stream'
];
$upload_body = file_get_contents('/path/to/file');
$upload_request = new Request('POST', 'https://api.pdfrest.com/upload', $upload_headers, $upload_body);
$upload_res = $upload_client->sendAsync($upload_request)->wait();
echo $upload_res->getBody() . PHP_EOL;

$upload_response_json = json_decode($upload_res->getBody());

$uploaded_id = $upload_response_json->{'files'}[0]->{'id'};

echo "Successfully uploaded with an id of: " . $uploaded_id . PHP_EOL;

$flatten_client = new Client(['http_errors' => false]);
$flatten_headers = [
'api-key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
'Content-Type' => 'application/json'
];
$flatten_body = '{"id":"'.$uploaded_id.'"}';
$flatten_request = new Request('POST', 'https://api.pdfrest.com/flattened-layers-pdf', $flatten_headers, $flatten_body);
$flatten_res = $flatten_client->sendAsync($flatten_request)->wait();
echo $flatten_res->getBody() . PHP_EOL;
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
require 'vendor/autoload.php'; // Require the autoload file to load Guzzle HTTP client.

use GuzzleHttp\Client; // Import the Guzzle HTTP client namespace.
use GuzzleHttp\Psr7\Request; // Import the PSR-7 Request class.
use GuzzleHttp\Psr7\Utils; // Import the PSR-7 Utils class for working with streams.

$upload_client = new Client(['http_errors' => false]);
$upload_headers = [
'api-key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
'content-filename' => 'filename.pdf',
'Content-Type' => 'application/octet-stream'
];
$upload_body = file_get_contents('/path/to/file');
$upload_request = new Request('POST', 'https://api.pdfrest.com/upload', $upload_headers, $upload_body);
$upload_res = $upload_client->sendAsync($upload_request)->wait();
echo $upload_res->getBody() . PHP_EOL;

$upload_response_json = json_decode($upload_res->getBody());

$uploaded_id = $upload_response_json->{'files'}[0]->{'id'};

echo "Successfully uploaded with an id of: " . $uploaded_id . PHP_EOL;

$flatten_client = new Client(['http_errors' => false]);
$flatten_headers = [
'api-key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
'Content-Type' => 'application/json'
];
$flatten_body = '{"id":"'.$uploaded_id.'"}';
$flatten_request = new Request('POST', 'https://api.pdfrest.com/flattened-transparencies-pdf', $flatten_headers, $flatten_body);
$flatten_res = $flatten_client->sendAsync($flatten_request)->wait();
echo $flatten_res->getBody() . PHP_EOL;
46 changes: 46 additions & 0 deletions PHP/Endpoint Examples/JSON Payload/get-resource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
// Resource UUIDs can be found in the JSON response of POST requests as "outputId". Resource UUIDs usually look like this: '0950b9bdf-0465-4d3f-8ea3-d2894f1ae839'.
$id = 'xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'; // place resource uuid here

// The response format can be 'file' or 'url'.
// If 'url', then JSON containing the url of the resource file is returned.
// If 'file', then the file itself is returned.
$format = 'file';

$resource_url = "https://api.pdfrest.com/resource/$id?format=$format";

// Initialize a cURL session.
$ch = curl_init();

// Set the url that will be sent to resource/{id} endpoint.
curl_setopt($ch, CURLOPT_URL, $resource_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

print "Sending GET request to /resource/{id} endpoint...\n";
$response = curl_exec($ch);

print "Response status code: " . curl_getinfo($ch, CURLINFO_HTTP_CODE) . "\n";

if($response === false){
print 'Error: ' . curl_error($ch) . "\n";
}else{
if($format == 'file'){
// For example, the string 'place_output_name_with_extension_here' can be replaced with the name of the output file with the proper extension.
// If you are expecting the resource to be a PDF file, then the 'place_output_name_with_extension_here' string could be replaced with 'out.pdf'.
// Given the example above, you will find a file named 'out.pdf' in the same folder as the sample when the sample executes successfully.
$output_file_name = 'place_output_name_with_extension_here';

if(file_put_contents($output_file_name, @file_get_contents($resource_url))){
print "The file $output_file_name was created.\n";
}else{
print json_encode(json_decode($response), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
print "\n";
}
}else{
print json_encode(json_decode($response), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
print "\n";
}
}

curl_close($ch);
?>
Loading

0 comments on commit c713544

Please sign in to comment.