|
| 1 | +/* |
| 2 | + * What this sample does: |
| 3 | + * - Calls /blank-pdf with a JSON payload to create an empty three-page PDF. |
| 4 | + * |
| 5 | + * Setup (environment): |
| 6 | + * - Set PDFREST_API_KEY=your_api_key_here |
| 7 | + * - Optional: set PDFREST_URL to override the API region. For EU/GDPR, use: |
| 8 | + * PDFREST_URL=https://eu-api.pdfrest.com |
| 9 | + * More info: https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work |
| 10 | + * |
| 11 | + * Usage: |
| 12 | + * ./blank_pdf_json |
| 13 | + * |
| 14 | + * Output: |
| 15 | + * - Prints the JSON response. Non-2xx responses exit non-zero. |
| 16 | + */ |
| 17 | + |
| 18 | +#include <cpr/cpr.h> |
| 19 | +#include <nlohmann/json.hpp> |
| 20 | + |
| 21 | +#include <cstdlib> |
| 22 | +#include <filesystem> |
| 23 | +#include <fstream> |
| 24 | +#include <iostream> |
| 25 | +#include <string> |
| 26 | + |
| 27 | +namespace fs = std::filesystem; |
| 28 | +using json = nlohmann::json; |
| 29 | + |
| 30 | +static std::string rtrim_slashes(std::string s) { |
| 31 | + while (!s.empty() && (s.back() == '/' || s.back() == '\\')) { |
| 32 | + s.pop_back(); |
| 33 | + } |
| 34 | + return s; |
| 35 | +} |
| 36 | + |
| 37 | +static void load_dotenv_if_present(const fs::path &path) { |
| 38 | + std::ifstream f(path); |
| 39 | + if (!f.is_open()) return; |
| 40 | + std::string line; |
| 41 | + while (std::getline(f, line)) { |
| 42 | + if (line.empty() || line[0] == '#') continue; |
| 43 | + auto pos = line.find('='); |
| 44 | + if (pos == std::string::npos) continue; |
| 45 | + std::string key = line.substr(0, pos); |
| 46 | + std::string val = line.substr(pos + 1); |
| 47 | + auto trim = [](std::string &s) { |
| 48 | + size_t start = s.find_first_not_of(" \t\r\n"); |
| 49 | + size_t end = s.find_last_not_of(" \t\r\n"); |
| 50 | + if (start == std::string::npos) { s.clear(); return; } |
| 51 | + s = s.substr(start, end - start + 1); |
| 52 | + }; |
| 53 | + trim(key); |
| 54 | + trim(val); |
| 55 | + if (key.empty()) continue; |
| 56 | + if (std::getenv(key.c_str()) == nullptr) { |
| 57 | +#ifdef _WIN32 |
| 58 | + _putenv_s(key.c_str(), val.c_str()); |
| 59 | +#else |
| 60 | + setenv(key.c_str(), val.c_str(), 0); |
| 61 | +#endif |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +static void load_env() { |
| 67 | + const fs::path here = fs::current_path(); |
| 68 | + load_dotenv_if_present(here / ".env"); |
| 69 | + if (fs::exists(here.parent_path())) { |
| 70 | + load_dotenv_if_present(here.parent_path() / ".env"); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +int main() { |
| 75 | + load_env(); |
| 76 | + |
| 77 | + const char *api_key_c = std::getenv("PDFREST_API_KEY"); |
| 78 | + if (api_key_c == nullptr || std::string(api_key_c).empty()) { |
| 79 | + std::cerr << "Missing required environment variable: PDFREST_API_KEY\n"; |
| 80 | + return 1; |
| 81 | + } |
| 82 | + |
| 83 | + const char *base_url_c = std::getenv("PDFREST_URL"); |
| 84 | + std::string base_url = base_url_c && std::string(base_url_c).size() |
| 85 | + ? base_url_c |
| 86 | + : std::string("https://api.pdfrest.com"); |
| 87 | + base_url = rtrim_slashes(base_url); |
| 88 | + |
| 89 | + json payload = { |
| 90 | + {"page_size", "letter"}, |
| 91 | + {"page_count", 3}, |
| 92 | + {"page_orientation", "portrait"} |
| 93 | + }; |
| 94 | + |
| 95 | + cpr::Header headers{ |
| 96 | + {"Api-Key", api_key_c}, |
| 97 | + {"Accept", "application/json"}, |
| 98 | + {"Content-Type", "application/json"} |
| 99 | + }; |
| 100 | + |
| 101 | + auto res = cpr::Post( |
| 102 | + cpr::Url{base_url + "/blank-pdf"}, |
| 103 | + headers, |
| 104 | + cpr::Body{payload.dump()} |
| 105 | + ); |
| 106 | + |
| 107 | + if (res.error || res.status_code < 200 || res.status_code >= 300) { |
| 108 | + std::cerr << "blank-pdf failed (status " << res.status_code << "): " |
| 109 | + << res.error.message << "\n" << res.text << "\n"; |
| 110 | + return 1; |
| 111 | + } |
| 112 | + |
| 113 | + std::cout << res.text << "\n"; |
| 114 | + return 0; |
| 115 | +} |
0 commit comments