Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CPlusPlus/Endpoint Examples/JSON Payload/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ if (cpr_FOUND AND nlohmann_json_FOUND)
add_executable(translated_pdf_text_json translated_pdf_text.cpp)
target_link_libraries(translated_pdf_text_json PRIVATE cpr::cpr nlohmann_json::nlohmann_json)
target_compile_features(translated_pdf_text_json PRIVATE cxx_std_20)

add_executable(blank_pdf_json blank_pdf.cpp)
target_link_libraries(blank_pdf_json PRIVATE cpr::cpr nlohmann_json::nlohmann_json)
target_compile_features(blank_pdf_json PRIVATE cxx_std_20)
endif()
115 changes: 115 additions & 0 deletions CPlusPlus/Endpoint Examples/JSON Payload/blank_pdf.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* What this sample does:
* - Calls /blank-pdf with a JSON payload to create an empty three-page PDF.
*
* Setup (environment):
* - Set PDFREST_API_KEY=your_api_key_here
* - Optional: set PDFREST_URL to override the API region. For EU/GDPR, use:
* PDFREST_URL=https://eu-api.pdfrest.com
* More info: https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work
*
* Usage:
* ./blank_pdf_json
*
* Output:
* - Prints the JSON response. Non-2xx responses exit non-zero.
*/

#include <cpr/cpr.h>
#include <nlohmann/json.hpp>

#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>

namespace fs = std::filesystem;
using json = nlohmann::json;

static std::string rtrim_slashes(std::string s) {
while (!s.empty() && (s.back() == '/' || s.back() == '\\')) {
s.pop_back();
}
return s;
}

static void load_dotenv_if_present(const fs::path &path) {
std::ifstream f(path);
if (!f.is_open()) return;
std::string line;
while (std::getline(f, line)) {
if (line.empty() || line[0] == '#') continue;
auto pos = line.find('=');
if (pos == std::string::npos) continue;
std::string key = line.substr(0, pos);
std::string val = line.substr(pos + 1);
auto trim = [](std::string &s) {
size_t start = s.find_first_not_of(" \t\r\n");
size_t end = s.find_last_not_of(" \t\r\n");
if (start == std::string::npos) { s.clear(); return; }
s = s.substr(start, end - start + 1);
};
trim(key);
trim(val);
if (key.empty()) continue;
if (std::getenv(key.c_str()) == nullptr) {
#ifdef _WIN32
_putenv_s(key.c_str(), val.c_str());
#else
setenv(key.c_str(), val.c_str(), 0);
#endif
}
}
}

static void load_env() {
const fs::path here = fs::current_path();
load_dotenv_if_present(here / ".env");
if (fs::exists(here.parent_path())) {
load_dotenv_if_present(here.parent_path() / ".env");
}
}

int main() {
load_env();

const char *api_key_c = std::getenv("PDFREST_API_KEY");
if (api_key_c == nullptr || std::string(api_key_c).empty()) {
std::cerr << "Missing required environment variable: PDFREST_API_KEY\n";
return 1;
}

const char *base_url_c = std::getenv("PDFREST_URL");
std::string base_url = base_url_c && std::string(base_url_c).size()
? base_url_c
: std::string("https://api.pdfrest.com");
base_url = rtrim_slashes(base_url);

json payload = {
{"page_size", "letter"},
{"page_count", 3},
{"page_orientation", "portrait"}
};

cpr::Header headers{
{"Api-Key", api_key_c},
{"Accept", "application/json"},
{"Content-Type", "application/json"}
};

auto res = cpr::Post(
cpr::Url{base_url + "/blank-pdf"},
headers,
cpr::Body{payload.dump()}
);

if (res.error || res.status_code < 200 || res.status_code >= 300) {
std::cerr << "blank-pdf failed (status " << res.status_code << "): "
<< res.error.message << "\n" << res.text << "\n";
return 1;
}

std::cout << res.text << "\n";
return 0;
}
4 changes: 4 additions & 0 deletions CPlusPlus/Endpoint Examples/Multipart Payload/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ if (cpr_FOUND)
add_executable(translated_pdf_text_multipart translated_pdf_text.cpp)
target_link_libraries(translated_pdf_text_multipart PRIVATE cpr::cpr)
target_compile_features(translated_pdf_text_multipart PRIVATE cxx_std_20)

add_executable(blank_pdf_multipart blank_pdf.cpp)
target_link_libraries(blank_pdf_multipart PRIVATE cpr::cpr)
target_compile_features(blank_pdf_multipart PRIVATE cxx_std_20)
endif()
94 changes: 94 additions & 0 deletions CPlusPlus/Endpoint Examples/Multipart Payload/blank_pdf.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* What this sample does:
* - Calls /blank-pdf via multipart/form-data to create a blank three-page PDF.
*
* Setup (environment):
* - Copy .env.example to .env
* - Set PDFREST_API_KEY=your_api_key_here
* - Optional: set PDFREST_URL to override the API region. For EU/GDPR compliance and proximity, use:
* PDFREST_URL=https://eu-api.pdfrest.com
* For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work
*
* Usage:
* ./blank_pdf_multipart
*
* Output:
* - Prints the JSON response to stdout; non-2xx exits with concise error.
*/

#include <cpr/cpr.h>

#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>

namespace fs = std::filesystem;

static std::string rtrim_slashes(std::string s) {
while (!s.empty() && (s.back() == '/' || s.back() == '\\')) s.pop_back();
return s;
}

static void load_dotenv_if_present(const fs::path &path) {
std::ifstream f(path);
if (!f.is_open()) return;
std::string line;
while (std::getline(f, line)) {
if (line.empty() || line[0] == '#') continue;
auto p = line.find('=');
if (p == std::string::npos) continue;
std::string k = line.substr(0, p);
std::string v = line.substr(p + 1);
auto trim = [](std::string &s) {
size_t b = s.find_first_not_of(" \t\r\n"), e = s.find_last_not_of(" \t\r\n");
if (b == std::string::npos) { s.clear(); return; }
s = s.substr(b, e - b + 1);
};
trim(k); trim(v);
if (k.empty()) continue;
if (!std::getenv(k.c_str())) {
#ifdef _WIN32
_putenv_s(k.c_str(), v.c_str());
#else
setenv(k.c_str(), v.c_str(), 0);
#endif
}
}
}

static void load_env() {
auto here = fs::current_path();
load_dotenv_if_present(here / ".env");
if (fs::exists(here.parent_path())) {
load_dotenv_if_present(here.parent_path() / ".env");
}
}

int main() {
load_env();
const char *key = getenv("PDFREST_API_KEY");
if (!key || !*key) {
std::cerr << "Missing PDFREST_API_KEY\n";
return 1;
}
std::string base = getenv("PDFREST_URL") ? getenv("PDFREST_URL") : "https://api.pdfrest.com";
base = rtrim_slashes(base);

cpr::Header hdr{{"Api-Key", key}, {"Accept", "application/json"}};
cpr::Multipart mp{
{"page_size", "letter"},
{"page_count", "3"},
{"page_orientation", "portrait"}
};

auto res = cpr::Post(cpr::Url{base + "/blank-pdf"}, hdr, mp);
if (res.error || res.status_code < 200 || res.status_code >= 300) {
std::cerr << "blank-pdf failed (" << res.status_code << ")\n"
<< res.error.message << "\n" << res.text << "\n";
return 1;
}
std::cout << res.text << "\n";
return 0;
}
65 changes: 65 additions & 0 deletions DotNET/Endpoint Examples/JSON Payload/blank-pdf.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* What this sample does:
* - Requests a new blank PDF using the JSON payload route.
*
* Setup (environment):
* - Copy .env.example to .env
* - Set PDFREST_API_KEY=your_api_key_here
* - Optional: set PDFREST_URL to override the API region. For EU/GDPR compliance and proximity, use:
* PDFREST_URL=https://eu-api.pdfrest.com
* For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work
*
* Usage:
* dotnet run -- blank-pdf
*
* Output:
* - Prints JSON response for the generated blank document.
*/
using Newtonsoft.Json.Linq;
using System.Text;

namespace Samples.EndpointExamples.JsonPayload
{
public static class BlankPdf
{
public static async Task Execute(string[] args)
{
var apiKey = Environment.GetEnvironmentVariable("PDFREST_API_KEY");
if (string.IsNullOrWhiteSpace(apiKey))
{
Console.Error.WriteLine("Missing required environment variable: PDFREST_API_KEY");
Environment.Exit(1);
return;
}

var baseUrl = Environment.GetEnvironmentVariable("PDFREST_URL") ?? "https://api.pdfrest.com";

using (var httpClient = new HttpClient { BaseAddress = new Uri(baseUrl) })
using (var request = new HttpRequestMessage(HttpMethod.Post, "blank-pdf"))
{
request.Headers.TryAddWithoutValidation("Api-Key", apiKey);
request.Headers.Accept.Add(new("application/json"));

var payload = new JObject
{
["page_size"] = "letter",
["page_count"] = 3,
["page_orientation"] = "portrait"
};

request.Content = new StringContent(payload.ToString(), Encoding.UTF8, "application/json");

var response = await httpClient.SendAsync(request);
var result = await response.Content.ReadAsStringAsync();

Console.WriteLine("Blank PDF response received.");
Console.WriteLine(result);

if (!response.IsSuccessStatusCode)
{
Environment.ExitCode = 1;
}
}
}
}
}
63 changes: 63 additions & 0 deletions DotNET/Endpoint Examples/Multipart Payload/blank-pdf.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* What this sample does:
* - Calls /blank-pdf via multipart/form-data to create a three-page blank PDF.
* - Routed from Program.cs as: `dotnet run -- blank-pdf-multipart`.
*
* Setup (environment):
* - Copy .env.example to .env
* - Set PDFREST_API_KEY=your_api_key_here
* - Optional: set PDFREST_URL to override the API region. For EU/GDPR compliance and proximity, use:
* PDFREST_URL=https://eu-api.pdfrest.com
* For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work
*
* Usage:
* dotnet run -- blank-pdf-multipart
*
* Output:
* - Prints the JSON response. Validation errors (args/env) exit non-zero.
*/

namespace Samples.EndpointExamples.MultipartPayload
{
public static class BlankPdf
{
public static async Task Execute(string[] args)
{
var apiKey = Environment.GetEnvironmentVariable("PDFREST_API_KEY");
if (string.IsNullOrWhiteSpace(apiKey))
{
Console.Error.WriteLine("Missing required environment variable: PDFREST_API_KEY");
Environment.Exit(1);
return;
}

var baseUrl = Environment.GetEnvironmentVariable("PDFREST_URL") ?? "https://api.pdfrest.com";

using (var httpClient = new HttpClient { BaseAddress = new Uri(baseUrl) })
using (var request = new HttpRequestMessage(HttpMethod.Post, "blank-pdf"))
{
request.Headers.TryAddWithoutValidation("Api-Key", apiKey);
request.Headers.Accept.Add(new("application/json"));

var multipartContent = new MultipartFormDataContent
{
{ new StringContent("letter"), "page_size" },
{ new StringContent("3"), "page_count" },
{ new StringContent("portrait"), "page_orientation" }
};

request.Content = multipartContent;
var response = await httpClient.SendAsync(request);
var apiResult = await response.Content.ReadAsStringAsync();

Console.WriteLine("blank-pdf response received.");
Console.WriteLine(apiResult);

if (!response.IsSuccessStatusCode)
{
Environment.ExitCode = 1;
}
}
}
}
}
8 changes: 8 additions & 0 deletions DotNET/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ static void PrintUsage()
Console.Error.WriteLine(" pdf-with-redacted-text-preview <pdf> Preview redactions");
Console.Error.WriteLine(" pdf-with-redacted-text-applied <pdf> Apply redactions");
Console.Error.WriteLine(" Page / Files:");
Console.Error.WriteLine(" blank-pdf Generate an empty PDF");
Console.Error.WriteLine(" split-pdf <pdf> Split by page ranges");
Console.Error.WriteLine(" merged-pdf <pdf1> <pdf2> Merge two PDFs by id");
Console.Error.WriteLine(" Resources / Packaging / Signing:");
Expand Down Expand Up @@ -95,6 +96,7 @@ static void PrintUsage()
Console.Error.WriteLine(" pdf-with-redacted-text-preview-multipart <pdf> Preview redactions");
Console.Error.WriteLine(" pdf-with-redacted-text-applied-multipart <pdf> Apply redactions");
Console.Error.WriteLine(" Page / Files:");
Console.Error.WriteLine(" blank-pdf-multipart Generate an empty PDF");
Console.Error.WriteLine(" split-pdf-multipart <pdf> Split by page ranges");
Console.Error.WriteLine(" merged-pdf-multipart <f1> <f2> Merge two files");
Console.Error.WriteLine(" Resources / Packaging / Signing:");
Expand Down Expand Up @@ -149,6 +151,9 @@ static void PrintUsage()
case "rasterized-pdf":
await Samples.EndpointExamples.JsonPayload.RasterizedPdf.Execute(rest);
break;
case "blank-pdf":
await Samples.EndpointExamples.JsonPayload.BlankPdf.Execute(rest);
break;
case "pdf-multipart":
await Samples.EndpointExamples.MultipartPayload.Pdf.Execute(rest);
break;
Expand Down Expand Up @@ -306,6 +311,9 @@ static void PrintUsage()
case "rasterized-pdf-multipart":
await Samples.EndpointExamples.MultipartPayload.RasterizedPdf.Execute(rest);
break;
case "blank-pdf-multipart":
await Samples.EndpointExamples.MultipartPayload.BlankPdf.Execute(rest);
break;
case "compressed-pdf":
await Samples.EndpointExamples.JsonPayload.CompressedPdf.Execute(rest);
break;
Expand Down
Loading