|
| 1 | +/* |
| 2 | + * What this sample does: |
| 3 | + * - Converts two different file types to PDF via multipart (/pdf), then merges them via /merged-pdf. |
| 4 | + * |
| 5 | + * Setup (environment): |
| 6 | + * - Copy .env.example to .env |
| 7 | + * - Set PDFREST_API_KEY=your_api_key_here |
| 8 | + * - Optional: set PDFREST_URL to override the API region. For EU/GDPR compliance and proximity, use: |
| 9 | + * PDFREST_URL=https://eu-api.pdfrest.com |
| 10 | + * For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work |
| 11 | + * |
| 12 | + * Usage: |
| 13 | + * ./merge_different_file_types image.png slides.pptx |
| 14 | + * |
| 15 | + * Output: |
| 16 | + * - Prints JSON responses for the two conversions and the final merge; non-2xx exits with concise error. |
| 17 | + */ |
| 18 | + |
| 19 | +#include <cpr/cpr.h> |
| 20 | +#include <nlohmann/json.hpp> |
| 21 | + |
| 22 | +#include <cstdlib> |
| 23 | +#include <filesystem> |
| 24 | +#include <fstream> |
| 25 | +#include <iostream> |
| 26 | +#include <string> |
| 27 | + |
| 28 | +namespace fs = std::filesystem; |
| 29 | +using json = nlohmann::json; |
| 30 | + |
| 31 | +static std::string rtrim_slashes(std::string s) { |
| 32 | + while (!s.empty() && (s.back() == '/' || s.back() == '\\')) { |
| 33 | + s.pop_back(); |
| 34 | + } |
| 35 | + return s; |
| 36 | +} |
| 37 | + |
| 38 | +static void load_dotenv_if_present(const fs::path &p) { |
| 39 | + std::ifstream f(p); |
| 40 | + if (!f.is_open()) return; |
| 41 | + std::string l; |
| 42 | + while (std::getline(f, l)) { |
| 43 | + if (l.empty() || l[0] == '#') continue; |
| 44 | + auto pos = l.find('='); |
| 45 | + if (pos == std::string::npos) continue; |
| 46 | + std::string k = l.substr(0, pos); |
| 47 | + std::string v = l.substr(pos + 1); |
| 48 | + auto trim = [](std::string &s) { |
| 49 | + size_t b = s.find_first_not_of(" \t\r\n"); |
| 50 | + size_t e = s.find_last_not_of(" \t\r\n"); |
| 51 | + if (b == std::string::npos) { s.clear(); return; } |
| 52 | + s = s.substr(b, e - b + 1); |
| 53 | + }; |
| 54 | + trim(k); trim(v); |
| 55 | + if (k.empty()) continue; |
| 56 | + if (!std::getenv(k.c_str())) { |
| 57 | +#ifdef _WIN32 |
| 58 | + _putenv_s(k.c_str(), v.c_str()); |
| 59 | +#else |
| 60 | + setenv(k.c_str(), v.c_str(), 0); |
| 61 | +#endif |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +static void load_env() { |
| 67 | + auto 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 | +static std::string convert_to_pdf_id(const std::string &base, |
| 75 | + const std::string &api_key, |
| 76 | + const fs::path &input) { |
| 77 | + cpr::Header hdr{{"Api-Key", api_key}, {"Accept", "application/json"}}; |
| 78 | + cpr::Multipart mp{{"file", cpr::File{input.string()}}}; |
| 79 | + auto res = cpr::Post(cpr::Url{base + "/pdf"}, hdr, mp); |
| 80 | + if (res.error || res.status_code < 200 || res.status_code >= 300) { |
| 81 | + throw std::runtime_error( |
| 82 | + "Convert failed: " + std::to_string(res.status_code) + "\n" + |
| 83 | + res.error.message + "\n" + res.text); |
| 84 | + } |
| 85 | + std::cout << res.text << "\n"; |
| 86 | + auto j = json::parse(res.text); |
| 87 | + return j.at("outputId").get<std::string>(); |
| 88 | +} |
| 89 | + |
| 90 | +int main(int argc, char **argv) { |
| 91 | + load_env(); |
| 92 | + if (argc < 3) { |
| 93 | + std::cerr << "Usage: merge_different_file_types <imageFile> <pptFile>\n"; |
| 94 | + return 1; |
| 95 | + } |
| 96 | + fs::path img = argv[1], ppt = argv[2]; |
| 97 | + if (!fs::exists(img) || !fs::exists(ppt)) { |
| 98 | + std::cerr << "One or more input files not found.\n"; |
| 99 | + return 1; |
| 100 | + } |
| 101 | + const char *key = getenv("PDFREST_API_KEY"); |
| 102 | + if (!key || !*key) { |
| 103 | + std::cerr << "Missing PDFREST_API_KEY\n"; |
| 104 | + return 1; |
| 105 | + } |
| 106 | + std::string base = getenv("PDFREST_URL") ? getenv("PDFREST_URL") : "https://api.pdfrest.com"; |
| 107 | + base = rtrim_slashes(base); |
| 108 | + |
| 109 | + try { |
| 110 | + std::string img_id = convert_to_pdf_id(base, key, img); |
| 111 | + std::string ppt_id = convert_to_pdf_id(base, key, ppt); |
| 112 | + |
| 113 | + cpr::Header hdr{{"Api-Key", key}, {"Accept", "application/json"}}; |
| 114 | + cpr::Multipart mp{ |
| 115 | + {"id[]", img_id}, {"type[]", "id"}, {"pages[]", "all"}, |
| 116 | + {"id[]", ppt_id}, {"type[]", "id"}, {"pages[]", "all"} |
| 117 | + }; |
| 118 | + auto merge = cpr::Post(cpr::Url{base + "/merged-pdf"}, hdr, mp); |
| 119 | + if (merge.error || merge.status_code < 200 || merge.status_code >= 300) { |
| 120 | + std::cerr << "Merge failed (" << merge.status_code << ")\n" |
| 121 | + << merge.error.message << "\n" << merge.text << "\n"; |
| 122 | + return 1; |
| 123 | + } |
| 124 | + std::cout << merge.text << "\n"; |
| 125 | + } catch (const std::exception &e) { |
| 126 | + std::cerr << e.what() << "\n"; |
| 127 | + return 1; |
| 128 | + } |
| 129 | + return 0; |
| 130 | +} |
0 commit comments