From 6f2541e059d06050a91f19bf848413dceedaf16e Mon Sep 17 00:00:00 2001 From: Christopher Green Date: Thu, 2 Nov 2023 16:00:45 -0500 Subject: [PATCH] Add PDF to Office samples --- .../Multipart Payload/excel.cs | 25 ++++++++ .../Multipart Payload/powerpoint.cs | 25 ++++++++ .../Multipart Payload/word.cs | 25 ++++++++ .../Multipart Payload/Excel.java | 61 +++++++++++++++++++ .../Multipart Payload/Powerpoint.java | 61 +++++++++++++++++++ .../Multipart Payload/Word.java | 61 +++++++++++++++++++ .../Multipart Payload/excel.js | 32 ++++++++++ .../Multipart Payload/powerpoint.js | 32 ++++++++++ .../Multipart Payload/word.js | 32 ++++++++++ .../Multipart Payload/excel.php | 35 +++++++++++ .../Multipart Payload/powerpoint.php | 35 +++++++++++ .../Multipart Payload/word.php | 35 +++++++++++ .../Endpoint Examples/JSON Payload/excel.py | 39 ++++++++++++ .../JSON Payload/powerpoint.py | 39 ++++++++++++ Python/Endpoint Examples/JSON Payload/word.py | 39 ++++++++++++ .../Multipart Payload/excel.py | 35 +++++++++++ .../Multipart Payload/powerpoint.py | 35 +++++++++++ .../Multipart Payload/word.py | 35 +++++++++++ cURL/Endpoint Examples/JSON Payload/excel.sh | 14 +++++ .../JSON Payload/powerpoint.sh | 14 +++++ cURL/Endpoint Examples/JSON Payload/word.sh | 14 +++++ .../Multipart Payload/excel.sh | 6 ++ .../Multipart Payload/powerpoint.sh | 6 ++ .../Multipart Payload/word.sh | 6 ++ 24 files changed, 741 insertions(+) create mode 100644 DotNET/Endpoint Examples/Multipart Payload/excel.cs create mode 100644 DotNET/Endpoint Examples/Multipart Payload/powerpoint.cs create mode 100644 DotNET/Endpoint Examples/Multipart Payload/word.cs create mode 100644 Java/Endpoint Examples/Multipart Payload/Excel.java create mode 100644 Java/Endpoint Examples/Multipart Payload/Powerpoint.java create mode 100644 Java/Endpoint Examples/Multipart Payload/Word.java create mode 100644 JavaScript/Endpoint Examples/Multipart Payload/excel.js create mode 100644 JavaScript/Endpoint Examples/Multipart Payload/powerpoint.js create mode 100644 JavaScript/Endpoint Examples/Multipart Payload/word.js create mode 100644 PHP/Endpoint Examples/Multipart Payload/excel.php create mode 100644 PHP/Endpoint Examples/Multipart Payload/powerpoint.php create mode 100644 PHP/Endpoint Examples/Multipart Payload/word.php create mode 100644 Python/Endpoint Examples/JSON Payload/excel.py create mode 100644 Python/Endpoint Examples/JSON Payload/powerpoint.py create mode 100644 Python/Endpoint Examples/JSON Payload/word.py create mode 100644 Python/Endpoint Examples/Multipart Payload/excel.py create mode 100644 Python/Endpoint Examples/Multipart Payload/powerpoint.py create mode 100644 Python/Endpoint Examples/Multipart Payload/word.py create mode 100644 cURL/Endpoint Examples/JSON Payload/excel.sh create mode 100644 cURL/Endpoint Examples/JSON Payload/powerpoint.sh create mode 100644 cURL/Endpoint Examples/JSON Payload/word.sh create mode 100644 cURL/Endpoint Examples/Multipart Payload/excel.sh create mode 100644 cURL/Endpoint Examples/Multipart Payload/powerpoint.sh create mode 100644 cURL/Endpoint Examples/Multipart Payload/word.sh diff --git a/DotNET/Endpoint Examples/Multipart Payload/excel.cs b/DotNET/Endpoint Examples/Multipart Payload/excel.cs new file mode 100644 index 0000000..3158fe9 --- /dev/null +++ b/DotNET/Endpoint Examples/Multipart Payload/excel.cs @@ -0,0 +1,25 @@ +using System.Text; + +using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") }) +{ + using (var request = new HttpRequestMessage(HttpMethod.Post, "excel")) + { + request.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"); + request.Headers.Accept.Add(new("application/json")); + var multipartContent = new MultipartFormDataContent(); + + var byteArray = File.ReadAllBytes("/path/to/file"); + var byteAryContent = new ByteArrayContent(byteArray); + multipartContent.Add(byteAryContent, "file", "file_name"); + byteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/pdf"); + + + request.Content = multipartContent; + var response = await httpClient.SendAsync(request); + + var apiResult = await response.Content.ReadAsStringAsync(); + + Console.WriteLine("API response received."); + Console.WriteLine(apiResult); + } +} diff --git a/DotNET/Endpoint Examples/Multipart Payload/powerpoint.cs b/DotNET/Endpoint Examples/Multipart Payload/powerpoint.cs new file mode 100644 index 0000000..fce529a --- /dev/null +++ b/DotNET/Endpoint Examples/Multipart Payload/powerpoint.cs @@ -0,0 +1,25 @@ +using System.Text; + +using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") }) +{ + using (var request = new HttpRequestMessage(HttpMethod.Post, "powerpoint")) + { + request.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"); + request.Headers.Accept.Add(new("application/json")); + var multipartContent = new MultipartFormDataContent(); + + var byteArray = File.ReadAllBytes("/path/to/file"); + var byteAryContent = new ByteArrayContent(byteArray); + multipartContent.Add(byteAryContent, "file", "file_name"); + byteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/pdf"); + + + request.Content = multipartContent; + var response = await httpClient.SendAsync(request); + + var apiResult = await response.Content.ReadAsStringAsync(); + + Console.WriteLine("API response received."); + Console.WriteLine(apiResult); + } +} diff --git a/DotNET/Endpoint Examples/Multipart Payload/word.cs b/DotNET/Endpoint Examples/Multipart Payload/word.cs new file mode 100644 index 0000000..65f1c07 --- /dev/null +++ b/DotNET/Endpoint Examples/Multipart Payload/word.cs @@ -0,0 +1,25 @@ +using System.Text; + +using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") }) +{ + using (var request = new HttpRequestMessage(HttpMethod.Post, "word")) + { + request.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"); + request.Headers.Accept.Add(new("application/json")); + var multipartContent = new MultipartFormDataContent(); + + var byteArray = File.ReadAllBytes("/path/to/file"); + var byteAryContent = new ByteArrayContent(byteArray); + multipartContent.Add(byteAryContent, "file", "file_name"); + byteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/pdf"); + + + request.Content = multipartContent; + var response = await httpClient.SendAsync(request); + + var apiResult = await response.Content.ReadAsStringAsync(); + + Console.WriteLine("API response received."); + Console.WriteLine(apiResult); + } +} diff --git a/Java/Endpoint Examples/Multipart Payload/Excel.java b/Java/Endpoint Examples/Multipart Payload/Excel.java new file mode 100644 index 0000000..d6ec400 --- /dev/null +++ b/Java/Endpoint Examples/Multipart Payload/Excel.java @@ -0,0 +1,61 @@ +import io.github.cdimascio.dotenv.Dotenv; +import java.io.File; +import java.io.IOException; +import okhttp3.MediaType; +import okhttp3.MultipartBody; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; +import org.json.JSONObject; + +public class Excel { + + // Specify the path to your file here, or as the first argument when running the program. + private static final String DEFAULT_FILE_PATH = "/path/to/file.pdf"; + + // Specify your API key here, or in the environment variable PDFREST_API_KEY. + // You can also put the environment variable in a .env file. + private static final String DEFAULT_API_KEY = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"; + + public static void main(String[] args) { + File inputFile; + if (args.length > 0) { + inputFile = new File(args[0]); + } else { + inputFile = new File(DEFAULT_FILE_PATH); + } + + final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load(); + + final RequestBody inputFileRequestBody = + RequestBody.create(inputFile, MediaType.parse("application/pdf")); + RequestBody requestBody = + new MultipartBody.Builder() + .setType(MultipartBody.FORM) + .addFormDataPart("file", inputFile.getName(), inputFileRequestBody) + .addFormDataPart("output", "pdfrest_excel") + .build(); + Request request = + new Request.Builder() + .header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY)) + .url("https://api.pdfrest.com/excel") + .post(requestBody) + .build(); + try { + OkHttpClient client = new OkHttpClient().newBuilder().build(); + Response response = client.newCall(request).execute(); + System.out.println("Result code " + response.code()); + if (response.body() != null) { + System.out.println(prettyJson(response.body().string())); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + private static String prettyJson(String json) { + // https://stackoverflow.com/a/9583835/11996393 + return new JSONObject(json).toString(4); + } +} diff --git a/Java/Endpoint Examples/Multipart Payload/Powerpoint.java b/Java/Endpoint Examples/Multipart Payload/Powerpoint.java new file mode 100644 index 0000000..0b6f03b --- /dev/null +++ b/Java/Endpoint Examples/Multipart Payload/Powerpoint.java @@ -0,0 +1,61 @@ +import io.github.cdimascio.dotenv.Dotenv; +import java.io.File; +import java.io.IOException; +import okhttp3.MediaType; +import okhttp3.MultipartBody; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; +import org.json.JSONObject; + +public class Powerpoint { + + // Specify the path to your file here, or as the first argument when running the program. + private static final String DEFAULT_FILE_PATH = "/path/to/file.pdf"; + + // Specify your API key here, or in the environment variable PDFREST_API_KEY. + // You can also put the environment variable in a .env file. + private static final String DEFAULT_API_KEY = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"; + + public static void main(String[] args) { + File inputFile; + if (args.length > 0) { + inputFile = new File(args[0]); + } else { + inputFile = new File(DEFAULT_FILE_PATH); + } + + final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load(); + + final RequestBody inputFileRequestBody = + RequestBody.create(inputFile, MediaType.parse("application/pdf")); + RequestBody requestBody = + new MultipartBody.Builder() + .setType(MultipartBody.FORM) + .addFormDataPart("file", inputFile.getName(), inputFileRequestBody) + .addFormDataPart("output", "pdfrest_powerpoint") + .build(); + Request request = + new Request.Builder() + .header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY)) + .url("https://api.pdfrest.com/powerpoint") + .post(requestBody) + .build(); + try { + OkHttpClient client = new OkHttpClient().newBuilder().build(); + Response response = client.newCall(request).execute(); + System.out.println("Result code " + response.code()); + if (response.body() != null) { + System.out.println(prettyJson(response.body().string())); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + private static String prettyJson(String json) { + // https://stackoverflow.com/a/9583835/11996393 + return new JSONObject(json).toString(4); + } +} diff --git a/Java/Endpoint Examples/Multipart Payload/Word.java b/Java/Endpoint Examples/Multipart Payload/Word.java new file mode 100644 index 0000000..7a8b902 --- /dev/null +++ b/Java/Endpoint Examples/Multipart Payload/Word.java @@ -0,0 +1,61 @@ +import io.github.cdimascio.dotenv.Dotenv; +import java.io.File; +import java.io.IOException; +import okhttp3.MediaType; +import okhttp3.MultipartBody; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; +import org.json.JSONObject; + +public class Word { + + // Specify the path to your file here, or as the first argument when running the program. + private static final String DEFAULT_FILE_PATH = "/path/to/file.pdf"; + + // Specify your API key here, or in the environment variable PDFREST_API_KEY. + // You can also put the environment variable in a .env file. + private static final String DEFAULT_API_KEY = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"; + + public static void main(String[] args) { + File inputFile; + if (args.length > 0) { + inputFile = new File(args[0]); + } else { + inputFile = new File(DEFAULT_FILE_PATH); + } + + final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load(); + + final RequestBody inputFileRequestBody = + RequestBody.create(inputFile, MediaType.parse("application/pdf")); + RequestBody requestBody = + new MultipartBody.Builder() + .setType(MultipartBody.FORM) + .addFormDataPart("file", inputFile.getName(), inputFileRequestBody) + .addFormDataPart("output", "pdfrest_word") + .build(); + Request request = + new Request.Builder() + .header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY)) + .url("https://api.pdfrest.com/word") + .post(requestBody) + .build(); + try { + OkHttpClient client = new OkHttpClient().newBuilder().build(); + Response response = client.newCall(request).execute(); + System.out.println("Result code " + response.code()); + if (response.body() != null) { + System.out.println(prettyJson(response.body().string())); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + private static String prettyJson(String json) { + // https://stackoverflow.com/a/9583835/11996393 + return new JSONObject(json).toString(4); + } +} diff --git a/JavaScript/Endpoint Examples/Multipart Payload/excel.js b/JavaScript/Endpoint Examples/Multipart Payload/excel.js new file mode 100644 index 0000000..39aadcb --- /dev/null +++ b/JavaScript/Endpoint Examples/Multipart Payload/excel.js @@ -0,0 +1,32 @@ +// This request demonstrates how to convert a PDF to an Excel file. +var axios = require('axios'); +var FormData = require('form-data'); +var fs = require('fs'); + +// Create a new form data instance and append the PDF file and parameters to it +var data = new FormData(); +data.append('file', fs.createReadStream('/path/to/file')); +data.append('output', 'pdfrest_excel_pdf'); + +// define configuration options for axios request +var config = { + method: 'post', + maxBodyLength: Infinity, // set maximum length of the request body + url: 'https://api.pdfrest.com/excel', + headers: { + 'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', // Replace with your API key + ...data.getHeaders() // set headers for the request + }, + data : data // set the data to be sent with the request +}; + +// send request and handle response or error +axios(config) +.then(function (response) { + console.log(JSON.stringify(response.data)); +}) +.catch(function (error) { + console.log(error); +}); + +// If you would like to download the file instead of getting the JSON response, please see the 'get-resource-id-endpoint.js' sample. \ No newline at end of file diff --git a/JavaScript/Endpoint Examples/Multipart Payload/powerpoint.js b/JavaScript/Endpoint Examples/Multipart Payload/powerpoint.js new file mode 100644 index 0000000..96f6571 --- /dev/null +++ b/JavaScript/Endpoint Examples/Multipart Payload/powerpoint.js @@ -0,0 +1,32 @@ +// This request demonstrates how to convert a PDF to a PowerPoint file. +var axios = require('axios'); +var FormData = require('form-data'); +var fs = require('fs'); + +// Create a new form data instance and append the PDF file and parameters to it +var data = new FormData(); +data.append('file', fs.createReadStream('/path/to/file')); +data.append('output', 'pdfrest_powerpoint_pdf'); + +// define configuration options for axios request +var config = { + method: 'post', + maxBodyLength: Infinity, // set maximum length of the request body + url: 'https://api.pdfrest.com/powerpoint', + headers: { + 'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', // Replace with your API key + ...data.getHeaders() // set headers for the request + }, + data : data // set the data to be sent with the request +}; + +// send request and handle response or error +axios(config) +.then(function (response) { + console.log(JSON.stringify(response.data)); +}) +.catch(function (error) { + console.log(error); +}); + +// If you would like to download the file instead of getting the JSON response, please see the 'get-resource-id-endpoint.js' sample. \ No newline at end of file diff --git a/JavaScript/Endpoint Examples/Multipart Payload/word.js b/JavaScript/Endpoint Examples/Multipart Payload/word.js new file mode 100644 index 0000000..5603122 --- /dev/null +++ b/JavaScript/Endpoint Examples/Multipart Payload/word.js @@ -0,0 +1,32 @@ +// This request demonstrates how to convert a PDF to a Word file. +var axios = require('axios'); +var FormData = require('form-data'); +var fs = require('fs'); + +// Create a new form data instance and append the PDF file and parameters to it +var data = new FormData(); +data.append('file', fs.createReadStream('/path/to/file')); +data.append('output', 'pdfrest_word_pdf'); + +// define configuration options for axios request +var config = { + method: 'post', + maxBodyLength: Infinity, // set maximum length of the request body + url: 'https://api.pdfrest.com/word', + headers: { + 'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', // Replace with your API key + ...data.getHeaders() // set headers for the request + }, + data : data // set the data to be sent with the request +}; + +// send request and handle response or error +axios(config) +.then(function (response) { + console.log(JSON.stringify(response.data)); +}) +.catch(function (error) { + console.log(error); +}); + +// If you would like to download the file instead of getting the JSON response, please see the 'get-resource-id-endpoint.js' sample. \ No newline at end of file diff --git a/PHP/Endpoint Examples/Multipart Payload/excel.php b/PHP/Endpoint Examples/Multipart Payload/excel.php new file mode 100644 index 0000000..127257b --- /dev/null +++ b/PHP/Endpoint Examples/Multipart Payload/excel.php @@ -0,0 +1,35 @@ + 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' // Set the API key in the headers for authentication. +]; + +$options = [ + 'multipart' => [ + [ + 'name' => 'file', // Specify the field name for the file. + 'contents' => Utils::tryFopen('/path/to/file', 'r'), // Open the file specified by the '/path/to/file' for reading. + 'filename' => '/path/to/file', // Set the filename for the file to be processed, in this case, '/path/to/file'. + 'headers' => [ + 'Content-Type' => '' // Set the Content-Type header for the file. + ] + ], + [ + 'name' => 'output', // Specify the field name for the output option. + 'contents' => 'pdfrest_excel' // Set the value for the output option (in this case, 'pdfrest_excel'). + ] + ] +]; + +$request = new Request('POST', 'https://api.pdfrest.com/excel', $headers); // Create a new HTTP POST request with the API endpoint and headers. + +$res = $client->sendAsync($request, $options)->wait(); // Send the asynchronous request and wait for the response. + +echo $res->getBody(); // Output the response body, which contains the Excel document. diff --git a/PHP/Endpoint Examples/Multipart Payload/powerpoint.php b/PHP/Endpoint Examples/Multipart Payload/powerpoint.php new file mode 100644 index 0000000..b4c188f --- /dev/null +++ b/PHP/Endpoint Examples/Multipart Payload/powerpoint.php @@ -0,0 +1,35 @@ + 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' // Set the API key in the headers for authentication. +]; + +$options = [ + 'multipart' => [ + [ + 'name' => 'file', // Specify the field name for the file. + 'contents' => Utils::tryFopen('/path/to/file', 'r'), // Open the file specified by the '/path/to/file' for reading. + 'filename' => '/path/to/file', // Set the filename for the file to be processed, in this case, '/path/to/file'. + 'headers' => [ + 'Content-Type' => '' // Set the Content-Type header for the file. + ] + ], + [ + 'name' => 'output', // Specify the field name for the output option. + 'contents' => 'pdfrest_powerpoint' // Set the value for the output option (in this case, 'pdfrest_powerpoint'). + ] + ] +]; + +$request = new Request('POST', 'https://api.pdfrest.com/powerpoint', $headers); // Create a new HTTP POST request with the API endpoint and headers. + +$res = $client->sendAsync($request, $options)->wait(); // Send the asynchronous request and wait for the response. + +echo $res->getBody(); // Output the response body, which contains the PowerPoint document. diff --git a/PHP/Endpoint Examples/Multipart Payload/word.php b/PHP/Endpoint Examples/Multipart Payload/word.php new file mode 100644 index 0000000..df442c7 --- /dev/null +++ b/PHP/Endpoint Examples/Multipart Payload/word.php @@ -0,0 +1,35 @@ + 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' // Set the API key in the headers for authentication. +]; + +$options = [ + 'multipart' => [ + [ + 'name' => 'file', // Specify the field name for the file. + 'contents' => Utils::tryFopen('/path/to/file', 'r'), // Open the file specified by the '/path/to/file' for reading. + 'filename' => '/path/to/file', // Set the filename for the file to be processed, in this case, '/path/to/file'. + 'headers' => [ + 'Content-Type' => '' // Set the Content-Type header for the file. + ] + ], + [ + 'name' => 'output', // Specify the field name for the output option. + 'contents' => 'pdfrest_word' // Set the value for the output option (in this case, 'pdfrest_word'). + ] + ] +]; + +$request = new Request('POST', 'https://api.pdfrest.com/word', $headers); // Create a new HTTP POST request with the API endpoint and headers. + +$res = $client->sendAsync($request, $options)->wait(); // Send the asynchronous request and wait for the response. + +echo $res->getBody(); // Output the response body, which contains the Word document. diff --git a/Python/Endpoint Examples/JSON Payload/excel.py b/Python/Endpoint Examples/JSON Payload/excel.py new file mode 100644 index 0000000..92512a3 --- /dev/null +++ b/Python/Endpoint Examples/JSON Payload/excel.py @@ -0,0 +1,39 @@ +import requests +import json + +with open('/path/to/file', 'rb') as f: + upload_data = f.read() + +print("Uploading file...") +upload_response = requests.post(url='https://api.pdfrest.com/upload', + data=upload_data, + headers={'Content-Type': 'application/octet-stream', 'Content-Filename': 'file.pdf', "API-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}) + +print("Upload response status code: " + str(upload_response.status_code)) + +if upload_response.ok: + upload_response_json = upload_response.json() + print(json.dumps(upload_response_json, indent = 2)) + + + uploaded_id = upload_response_json['files'][0]['id'] + excel_data = { "id" : uploaded_id } + print(json.dumps(excel_data, indent = 2)) + + + print("Processing file...") + excel_response = requests.post(url='https://api.pdfrest.com/excel', + data=json.dumps(excel_data), + headers={'Content-Type': 'application/json', "API-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}) + + + + print("Processing response status code: " + str(excel_response.status_code)) + if excel_response.ok: + excel_response_json = excel_response.json() + print(json.dumps(excel_response_json, indent = 2)) + + else: + print(excel_response.text) +else: + print(upload_response.text) diff --git a/Python/Endpoint Examples/JSON Payload/powerpoint.py b/Python/Endpoint Examples/JSON Payload/powerpoint.py new file mode 100644 index 0000000..69b5989 --- /dev/null +++ b/Python/Endpoint Examples/JSON Payload/powerpoint.py @@ -0,0 +1,39 @@ +import requests +import json + +with open('/path/to/file', 'rb') as f: + upload_data = f.read() + +print("Uploading file...") +upload_response = requests.post(url='https://api.pdfrest.com/upload', + data=upload_data, + headers={'Content-Type': 'application/octet-stream', 'Content-Filename': 'file.pdf', "API-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}) + +print("Upload response status code: " + str(upload_response.status_code)) + +if upload_response.ok: + upload_response_json = upload_response.json() + print(json.dumps(upload_response_json, indent = 2)) + + + uploaded_id = upload_response_json['files'][0]['id'] + powerpoint_data = { "id" : uploaded_id } + print(json.dumps(powerpoint_data, indent = 2)) + + + print("Processing file...") + powerpoint_response = requests.post(url='https://api.pdfrest.com/powerpoint', + data=json.dumps(powerpoint_data), + headers={'Content-Type': 'application/json', "API-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}) + + + + print("Processing response status code: " + str(powerpoint_response.status_code)) + if powerpoint_response.ok: + powerpoint_response_json = powerpoint_response.json() + print(json.dumps(powerpoint_response_json, indent = 2)) + + else: + print(powerpoint_response.text) +else: + print(upload_response.text) diff --git a/Python/Endpoint Examples/JSON Payload/word.py b/Python/Endpoint Examples/JSON Payload/word.py new file mode 100644 index 0000000..f31e624 --- /dev/null +++ b/Python/Endpoint Examples/JSON Payload/word.py @@ -0,0 +1,39 @@ +import requests +import json + +with open('/path/to/file', 'rb') as f: + upload_data = f.read() + +print("Uploading file...") +upload_response = requests.post(url='https://api.pdfrest.com/upload', + data=upload_data, + headers={'Content-Type': 'application/octet-stream', 'Content-Filename': 'file.pdf', "API-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}) + +print("Upload response status code: " + str(upload_response.status_code)) + +if upload_response.ok: + upload_response_json = upload_response.json() + print(json.dumps(upload_response_json, indent = 2)) + + + uploaded_id = upload_response_json['files'][0]['id'] + word_data = { "id" : uploaded_id } + print(json.dumps(word_data, indent = 2)) + + + print("Processing file...") + word_response = requests.post(url='https://api.pdfrest.com/word', + data=json.dumps(word_data), + headers={'Content-Type': 'application/json', "API-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}) + + + + print("Processing response status code: " + str(word_response.status_code)) + if word_response.ok: + word_response_json = word_response.json() + print(json.dumps(word_response_json, indent = 2)) + + else: + print(word_response.text) +else: + print(upload_response.text) diff --git a/Python/Endpoint Examples/Multipart Payload/excel.py b/Python/Endpoint Examples/Multipart Payload/excel.py new file mode 100644 index 0000000..e1b5a4a --- /dev/null +++ b/Python/Endpoint Examples/Multipart Payload/excel.py @@ -0,0 +1,35 @@ +from requests_toolbelt import MultipartEncoder +import requests +import json + +excel_endpoint_url = 'https://api.pdfrest.com/excel' + +# The /excel endpoint can take a single PDF file or id as input. +# This sample demonstrates converting a PDF to an Excel document. +mp_encoder_excel = MultipartEncoder( + fields={ + 'file': ('file_name', open('/path/to/file', 'rb'), 'application/pdf'), + 'output' : 'example_excel_out', + } +) + +# Let's set the headers that the Excel endpoint expects. +# Since MultipartEncoder is used, the 'Content-Type' header gets set to 'multipart/form-data' via the content_type attribute below. +headers = { + 'Accept': 'application/json', + 'Content-Type': mp_encoder_excel.content_type, + 'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place your api key here +} + +print("Sending POST request to excel endpoint...") +response = requests.post(excel_endpoint_url, data=mp_encoder_excel, headers=headers) + +print("Response status code: " + str(response.status_code)) + +if response.ok: + response_json = response.json() + print(json.dumps(response_json, indent = 2)) +else: + print(response.text) + +# If you would like to download the file instead of getting the JSON response, please see the 'get-resource-id-endpoint.py' sample. diff --git a/Python/Endpoint Examples/Multipart Payload/powerpoint.py b/Python/Endpoint Examples/Multipart Payload/powerpoint.py new file mode 100644 index 0000000..a807e38 --- /dev/null +++ b/Python/Endpoint Examples/Multipart Payload/powerpoint.py @@ -0,0 +1,35 @@ +from requests_toolbelt import MultipartEncoder +import requests +import json + +powerpoint_endpoint_url = 'https://api.pdfrest.com/powerpoint' + +# The /powerpoint endpoint can take a single PDF file or id as input. +# This sample demonstrates converting a PDF to a PowerPoint document. +mp_encoder_powerpoint = MultipartEncoder( + fields={ + 'file': ('file_name', open('/path/to/file', 'rb'), 'application/pdf'), + 'output' : 'example_powerpoint_out', + } +) + +# Let's set the headers that the PowerPoint endpoint expects. +# Since MultipartEncoder is used, the 'Content-Type' header gets set to 'multipart/form-data' via the content_type attribute below. +headers = { + 'Accept': 'application/json', + 'Content-Type': mp_encoder_powerpoint.content_type, + 'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place your api key here +} + +print("Sending POST request to powerpoint endpoint...") +response = requests.post(powerpoint_endpoint_url, data=mp_encoder_powerpoint, headers=headers) + +print("Response status code: " + str(response.status_code)) + +if response.ok: + response_json = response.json() + print(json.dumps(response_json, indent = 2)) +else: + print(response.text) + +# If you would like to download the file instead of getting the JSON response, please see the 'get-resource-id-endpoint.py' sample. diff --git a/Python/Endpoint Examples/Multipart Payload/word.py b/Python/Endpoint Examples/Multipart Payload/word.py new file mode 100644 index 0000000..b6530d3 --- /dev/null +++ b/Python/Endpoint Examples/Multipart Payload/word.py @@ -0,0 +1,35 @@ +from requests_toolbelt import MultipartEncoder +import requests +import json + +word_endpoint_url = 'https://api.pdfrest.com/word' + +# The /word endpoint can take a single PDF file or id as input. +# This sample demonstrates converting a PDF to a Word document. +mp_encoder_word = MultipartEncoder( + fields={ + 'file': ('file_name', open('/path/to/file', 'rb'), 'application/pdf'), + 'output' : 'example_word_out', + } +) + +# Let's set the headers that the word endpoint expects. +# Since MultipartEncoder is used, the 'Content-Type' header gets set to 'multipart/form-data' via the content_type attribute below. +headers = { + 'Accept': 'application/json', + 'Content-Type': mp_encoder_word.content_type, + 'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place your api key here +} + +print("Sending POST request to word endpoint...") +response = requests.post(word_endpoint_url, data=mp_encoder_word, headers=headers) + +print("Response status code: " + str(response.status_code)) + +if response.ok: + response_json = response.json() + print(json.dumps(response_json, indent = 2)) +else: + print(response.text) + +# If you would like to download the file instead of getting the JSON response, please see the 'get-resource-id-endpoint.py' sample. diff --git a/cURL/Endpoint Examples/JSON Payload/excel.sh b/cURL/Endpoint Examples/JSON Payload/excel.sh new file mode 100644 index 0000000..3da1a00 --- /dev/null +++ b/cURL/Endpoint Examples/JSON Payload/excel.sh @@ -0,0 +1,14 @@ +#!/bin/sh + +UPLOAD_ID=$(curl --location 'https://api.pdfrest.com/upload' \ +--header 'Api-Key: xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' \ +--header 'content-filename: filename.pdf' \ +--data-binary '@/path/to/file' \ + | jq -r '.files.[0].id') + +echo "File successfully uploaded with an ID of: $UPLOAD_ID" + +curl 'https://api.pdfrest.com/excel' \ +--header 'Api-Key: xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' \ +--header 'Content-Type: application/json' \ +--data-raw "{ \"id\": \"$UPLOAD_ID\"}" | jq -r '.' diff --git a/cURL/Endpoint Examples/JSON Payload/powerpoint.sh b/cURL/Endpoint Examples/JSON Payload/powerpoint.sh new file mode 100644 index 0000000..961bdf9 --- /dev/null +++ b/cURL/Endpoint Examples/JSON Payload/powerpoint.sh @@ -0,0 +1,14 @@ +#!/bin/sh + +UPLOAD_ID=$(curl --location 'https://api.pdfrest.com/upload' \ +--header 'Api-Key: xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' \ +--header 'content-filename: filename.pdf' \ +--data-binary '@/path/to/file' \ + | jq -r '.files.[0].id') + +echo "File successfully uploaded with an ID of: $UPLOAD_ID" + +curl 'https://api.pdfrest.com/powerpoint' \ +--header 'Api-Key: xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' \ +--header 'Content-Type: application/json' \ +--data-raw "{ \"id\": \"$UPLOAD_ID\"}" | jq -r '.' diff --git a/cURL/Endpoint Examples/JSON Payload/word.sh b/cURL/Endpoint Examples/JSON Payload/word.sh new file mode 100644 index 0000000..da5fbf2 --- /dev/null +++ b/cURL/Endpoint Examples/JSON Payload/word.sh @@ -0,0 +1,14 @@ +#!/bin/sh + +UPLOAD_ID=$(curl --location 'https://api.pdfrest.com/upload' \ +--header 'Api-Key: xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' \ +--header 'content-filename: filename.pdf' \ +--data-binary '@/path/to/file' \ + | jq -r '.files.[0].id') + +echo "File successfully uploaded with an ID of: $UPLOAD_ID" + +curl 'https://api.pdfrest.com/word' \ +--header 'Api-Key: xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' \ +--header 'Content-Type: application/json' \ +--data-raw "{ \"id\": \"$UPLOAD_ID\"}" | jq -r '.' diff --git a/cURL/Endpoint Examples/Multipart Payload/excel.sh b/cURL/Endpoint Examples/Multipart Payload/excel.sh new file mode 100644 index 0000000..ee4dcbf --- /dev/null +++ b/cURL/Endpoint Examples/Multipart Payload/excel.sh @@ -0,0 +1,6 @@ +curl -X POST "https://api.pdfrest.com/excel" \ + -H "Accept: application/json" \ + -H "Content-Type: multipart/form-data" \ + -H "Api-Key: xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" \ + -F "file=@/path/to/file" \ + -F "output=example_out" diff --git a/cURL/Endpoint Examples/Multipart Payload/powerpoint.sh b/cURL/Endpoint Examples/Multipart Payload/powerpoint.sh new file mode 100644 index 0000000..49c8673 --- /dev/null +++ b/cURL/Endpoint Examples/Multipart Payload/powerpoint.sh @@ -0,0 +1,6 @@ +curl -X POST "https://api.pdfrest.com/powerpoint" \ + -H "Accept: application/json" \ + -H "Content-Type: multipart/form-data" \ + -H "Api-Key: xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" \ + -F "file=@/path/to/file" \ + -F "output=example_out" diff --git a/cURL/Endpoint Examples/Multipart Payload/word.sh b/cURL/Endpoint Examples/Multipart Payload/word.sh new file mode 100644 index 0000000..ce39735 --- /dev/null +++ b/cURL/Endpoint Examples/Multipart Payload/word.sh @@ -0,0 +1,6 @@ +curl -X POST "https://api.pdfrest.com/word" \ + -H "Accept: application/json" \ + -H "Content-Type: multipart/form-data" \ + -H "Api-Key: xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" \ + -F "file=@/path/to/file" \ + -F "output=example_out"