diff --git a/DotNET/Endpoint Examples/JSON Payload/markdown.cs b/DotNET/Endpoint Examples/JSON Payload/markdown.cs new file mode 100644 index 00000000..38dcde88 --- /dev/null +++ b/DotNET/Endpoint Examples/JSON Payload/markdown.cs @@ -0,0 +1,46 @@ +using Newtonsoft.Json.Linq; +using System.Text; + +using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") }) +{ + using (var uploadRequest = new HttpRequestMessage(HttpMethod.Post, "upload")) + { + uploadRequest.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"); + uploadRequest.Headers.Accept.Add(new("application/json")); + + var uploadByteArray = File.ReadAllBytes("/path/to/file"); + var uploadByteAryContent = new ByteArrayContent(uploadByteArray); + uploadByteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/octet-stream"); + uploadByteAryContent.Headers.TryAddWithoutValidation("Content-Filename", "filename.pdf"); + + uploadRequest.Content = uploadByteAryContent; + var uploadResponse = await httpClient.SendAsync(uploadRequest); + + var uploadResult = await uploadResponse.Content.ReadAsStringAsync(); + + Console.WriteLine("Upload response received."); + Console.WriteLine(uploadResult); + + JObject uploadResultJson = JObject.Parse(uploadResult); + var uploadedID = uploadResultJson["files"][0]["id"]; + using (var markdownRequest = new HttpRequestMessage(HttpMethod.Post, "markdown")) + { + markdownRequest.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"); + markdownRequest.Headers.Accept.Add(new("application/json")); + markdownRequest.Headers.TryAddWithoutValidation("Content-Type", "application/json"); + + JObject parameterJson = new JObject + { + ["id"] = uploadedID, + }; + + markdownRequest.Content = new StringContent(parameterJson.ToString(), Encoding.UTF8, "application/json"); + var markdownResponse = await httpClient.SendAsync(markdownRequest); + + var markdownResult = await markdownResponse.Content.ReadAsStringAsync(); + + Console.WriteLine("Markdown response received."); + Console.WriteLine(markdownResult); + } + } +} \ No newline at end of file diff --git a/DotNET/Endpoint Examples/Multipart Payload/markdown.cs b/DotNET/Endpoint Examples/Multipart Payload/markdown.cs new file mode 100644 index 00000000..0013848a --- /dev/null +++ b/DotNET/Endpoint Examples/Multipart Payload/markdown.cs @@ -0,0 +1,27 @@ +using System.Text; + +using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") }) +{ + using (var request = new HttpRequestMessage(HttpMethod.Post, "markdown")) + { + 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"); + + var byteArrayOption = new ByteArrayContent(Encoding.UTF8.GetBytes("on")); + multipartContent.Add(byteArrayOption, "page_break_comments"); + + request.Content = multipartContent; + var response = await httpClient.SendAsync(request); + + var apiResult = await response.Content.ReadAsStringAsync(); + + Console.WriteLine("Markdown API response received."); + Console.WriteLine(apiResult); + } +} \ No newline at end of file diff --git a/Java/Endpoint Examples/JSON Payload/Markdown.java b/Java/Endpoint Examples/JSON Payload/Markdown.java new file mode 100644 index 00000000..f95a3aff --- /dev/null +++ b/Java/Endpoint Examples/JSON Payload/Markdown.java @@ -0,0 +1,92 @@ +import io.github.cdimascio.dotenv.Dotenv; +import java.io.File; +import java.io.IOException; +import java.util.concurrent.TimeUnit; +import okhttp3.*; +import org.json.JSONArray; +import org.json.JSONObject; + +public class Markdown { + + // 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(); + + String uploadString = uploadFile(inputFile); + JSONObject uploadJSON = new JSONObject(uploadString); + if (uploadJSON.has("error")) { + System.out.println("Error during upload: " + uploadString); + return; + } + JSONArray fileArray = uploadJSON.getJSONArray("files"); + JSONObject fileObject = fileArray.getJSONObject(0); + String uploadedID = fileObject.get("id").toString(); + + String JSONString = + String.format("{\"id\":\"%s\", \"page_break_comments\":\"on\"}", uploadedID); + final RequestBody requestBody = + RequestBody.create(JSONString, MediaType.parse("application/json")); + + Request request = + new Request.Builder() + .header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY)) + .url("https://api.pdfrest.com/markdown") + .post(requestBody) + .build(); + try { + OkHttpClient client = + new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build(); + + Response response = client.newCall(request).execute(); + System.out.println("Markdown 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); + } + + // This function is just a copy of the 'Upload.java' file to upload a binary file + private static String uploadFile(File inputFile) { + final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load(); + final RequestBody requestBody = + RequestBody.create(inputFile, MediaType.parse("application/pdf")); + + Request request = + new Request.Builder() + .header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY)) + .header("Content-Filename", "File.pdf") + .url("https://api.pdfrest.com/upload") + .post(requestBody) + .build(); + try { + OkHttpClient client = new OkHttpClient().newBuilder().build(); + Response response = client.newCall(request).execute(); + System.out.println("Upload Result code " + response.code()); + if (response.body() != null) { + return response.body().string(); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + return ""; + } +} diff --git a/Java/Endpoint Examples/Multipart Payload/Markdown.java b/Java/Endpoint Examples/Multipart Payload/Markdown.java new file mode 100644 index 00000000..79661866 --- /dev/null +++ b/Java/Endpoint Examples/Multipart Payload/Markdown.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 Markdown { + + // 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("page_break_comments", "on") + .build(); + Request request = + new Request.Builder() + .header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY)) + .url("https://api.pdfrest.com/markdown") + .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/JSON Payload/markdown.js b/JavaScript/Endpoint Examples/JSON Payload/markdown.js new file mode 100644 index 00000000..91b24b69 --- /dev/null +++ b/JavaScript/Endpoint Examples/JSON Payload/markdown.js @@ -0,0 +1,54 @@ +var axios = require("axios"); +var fs = require("fs"); + +var upload_data = fs.createReadStream("/path/to/file.pdf"); + +var upload_config = { + method: "post", + maxBodyLength: Infinity, + url: "https://api.pdfrest.com/upload", + headers: { + "Api-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", // Replace with your API key + "Content-Filename": "filename.pdf", + "Content-Type": "application/octet-stream", + }, + data: upload_data, +}; + +// Send upload request +axios(upload_config) + .then(function (upload_response) { + console.log("Upload response:"); + console.log(JSON.stringify(upload_response.data, null, 2)); + + var uploaded_id = upload_response.data.files[0].id; + + var markdown_config = { + method: "post", + maxBodyLength: Infinity, + url: "https://api.pdfrest.com/markdown", + headers: { + "Api-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", // Replace with your API key + "Content-Type": "application/json", + }, + data: { + id: uploaded_id, + page_break_comments: "on" + }, + }; + + // Send markdown request + axios(markdown_config) + .then(function (markdown_response) { + console.log("Markdown response:"); + console.log(JSON.stringify(markdown_response.data, null, 2)); + }) + .catch(function (error) { + console.error("Markdown request error:"); + console.error(error.response?.data || error.message); + }); + }) + .catch(function (error) { + console.error("Upload request error:"); + console.error(error.response?.data || error.message); + }); \ No newline at end of file diff --git a/JavaScript/Endpoint Examples/Multipart Payload/markdown.js b/JavaScript/Endpoint Examples/Multipart Payload/markdown.js new file mode 100644 index 00000000..eb0110b8 --- /dev/null +++ b/JavaScript/Endpoint Examples/Multipart Payload/markdown.js @@ -0,0 +1,30 @@ +// This request demonstrates how to generate markdown from a PDF document. +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("page_break_comments", "on"); + +// define configuration options for axios request +var config = { + method: "post", + maxBodyLength: Infinity, // set maximum length of the request body + url: "https://api.pdfrest.com/markdown", + 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); + }); \ No newline at end of file diff --git a/PHP/Endpoint Examples/JSON Payload/markdown.php b/PHP/Endpoint Examples/JSON Payload/markdown.php new file mode 100644 index 00000000..914edb91 --- /dev/null +++ b/PHP/Endpoint Examples/JSON Payload/markdown.php @@ -0,0 +1,33 @@ + 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; + +$markdown_client = new Client(['http_errors' => false]); +$markdown_headers = [ + 'api-key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', + 'Content-Type' => 'application/json' +]; +$markdown_body = '{"id":"'.$uploaded_id.'","page_break_comments":"on"}'; +$markdown_request = new Request('POST', 'https://api.pdfrest.com/markdown', $markdown_headers, $markdown_body); +$markdown_res = $markdown_client->sendAsync($markdown_request)->wait(); +echo $markdown_res->getBody() . PHP_EOL; \ No newline at end of file diff --git a/PHP/Endpoint Examples/Multipart Payload/markdown.php b/PHP/Endpoint Examples/Multipart Payload/markdown.php new file mode 100644 index 00000000..2c17cc27 --- /dev/null +++ b/PHP/Endpoint Examples/Multipart Payload/markdown.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 '/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' => 'application/pdf' // Set the Content-Type header for the file. + ] + ], + [ + 'name' => 'page_break_comments', // Specify the field name for the page_break_comments option. + 'contents' => 'on' // Set the value for the page_break_comments option (in this case, 'on'). + ] + ] +]; + +$request = new Request('POST', 'https://api.pdfrest.com/markdown', $headers); // Create a new HTTP POST request with the updated /markdown 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 generated markdown from the document. \ No newline at end of file diff --git a/Python/Endpoint Examples/JSON Payload/markdown.py b/Python/Endpoint Examples/JSON Payload/markdown.py new file mode 100644 index 00000000..630b5008 --- /dev/null +++ b/Python/Endpoint Examples/JSON Payload/markdown.py @@ -0,0 +1,45 @@ +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'] + markdown_data = { "id": uploaded_id, "page_break_comments": "on" } + print(json.dumps(markdown_data, indent=2)) + + print("Processing file...") + markdown_response = requests.post( + url='https://api.pdfrest.com/markdown', + data=json.dumps(markdown_data), + headers={ + 'Content-Type': 'application/json', + 'API-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' + } + ) + + print("Processing response status code: " + str(markdown_response.status_code)) + if markdown_response.ok: + markdown_response_json = markdown_response.json() + print(json.dumps(markdown_response_json, indent=2)) + else: + print(markdown_response.text) +else: + print(upload_response.text) \ No newline at end of file diff --git a/Python/Endpoint Examples/Multipart Payload/markdown.py b/Python/Endpoint Examples/Multipart Payload/markdown.py new file mode 100644 index 00000000..fdb69eb0 --- /dev/null +++ b/Python/Endpoint Examples/Multipart Payload/markdown.py @@ -0,0 +1,33 @@ +from requests_toolbelt import MultipartEncoder +import requests +import json + +markdown_endpoint_url = 'https://api.pdfrest.com/markdown' + +# The /markdown endpoint can take a single PDF file or id as input. +# This sample demonstrates converting the document to markdown and returning it as JSON. +mp_encoder_markdown = MultipartEncoder( + fields={ + 'file': ('file_name.pdf', open('/path/to/file', 'rb'), 'application/pdf'), + 'page_break_comments': 'on', + } +) + +# Let's set the headers that the markdown 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_markdown.content_type, + 'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place your api key here +} + +print("Sending POST request to markdown endpoint...") +response = requests.post(markdown_endpoint_url, data=mp_encoder_markdown, 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) \ No newline at end of file diff --git a/README.md b/README.md index d441275c..8db3a63f 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ pdfRest is a REST API Toolkit for developers with all of the API Tools you'll ne | **[Flatten Forms](https://pdfrest.com/apitools/flatten-forms/)** | **[Import Form Data](https://pdfrest.com/apitools/import-form-data/)** | **[Export Form Data](https://pdfrest.com/apitools/export-form-data/)** | **[Extract Text](https://pdfrest.com/apitools/extract-text/)** | | **[PDF to Word](https://pdfrest.com/apitools/pdf-to-word/)** | **[PDF to Excel](https://pdfrest.com/apitools/pdf-to-excel/)** | **[PDF to PowerPoint](https://pdfrest.com/apitools/pdf-to-powerpoint/)** | **[Extract Images](https://pdfrest.com/apitools/extract-images/)** | | **[OCR to PDF](https://pdfrest.com/apitools/ocr-pdf/)** | **[API Polling](https://pdfrest.com/apitools/api-polling/)** | **[Rasterize PDF](https://pdfrest.com/apitools/rasterize-pdf/)** | **[Convert PDF Colors](https://pdfrest.com/apitools/convert-pdf-colors/)** | -| **[Redact PDF](https://pdfrest.com/apitools/redact-pdf/)** | +| **[Redact PDF](https://pdfrest.com/apitools/redact-pdf/)** | **[PDF to Markdown](https://pdfrest.com/apitools/pdf-to-markdown/)** |
diff --git a/cURL/Endpoint Examples/JSON Payload/markdown.sh b/cURL/Endpoint Examples/JSON Payload/markdown.sh new file mode 100644 index 00000000..e78cf293 --- /dev/null +++ b/cURL/Endpoint Examples/JSON Payload/markdown.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/markdown' \ +--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/markdown.sh b/cURL/Endpoint Examples/Multipart Payload/markdown.sh new file mode 100644 index 00000000..b99b7ed0 --- /dev/null +++ b/cURL/Endpoint Examples/Multipart Payload/markdown.sh @@ -0,0 +1,7 @@ +#!/bin/sh + +curl -X POST "https://api.pdfrest.com/markdown" \ + -H "Accept: application/json" \ + -H "Content-Type: multipart/form-data" \ + -H "Api-Key: xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" \ + -F "file=@/path/to/file"