forked from pdfrest/pdfrest-api-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsummarized_pdf_text.cpp
More file actions
80 lines (73 loc) · 2.83 KB
/
Copy pathsummarized_pdf_text.cpp
File metadata and controls
80 lines (73 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
* What this sample does:
* - Summarizes PDF content via multipart/form-data (file + options).
*
* 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:
* ./summarized_pdf_text_multipart /path/to/input.pdf
*
* Output:
* - Prints the JSON response to stdout; non-2xx exits with concise error.
*/
#include <cpr/cpr.h>
#include <filesystem>
#include <iostream>
#include <fstream>
#include <cstdlib>
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);
#ifdef _WIN32
_putenv_s(k.c_str(), v.c_str());
#else
if (!std::getenv(k.c_str())) 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(int argc, char **argv) {
load_env();
if (argc < 2) { std::cerr << "Usage: summarized_pdf_text_multipart <input.pdf>\n"; return 1; }
fs::path input = argv[1];
if (!fs::exists(input)) { std::cerr << "File not found: " << input << "\n"; return 1; }
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{
{"file", cpr::File{input.string()}},
{"target_word_count", "100"}
};
auto res = cpr::Post(cpr::Url{base + "/summarized-pdf-text"}, hdr, mp);
if (res.error || res.status_code < 200 || res.status_code >= 300) {
std::cerr << "Request failed (" << res.status_code << ")\n" << res.error.message << "\n" << res.text << "\n";
return 1;
}
std::cout << res.text << "\n";
return 0;
}