2022-01-26 20:46:28 +00:00
|
|
|
#include <qpdf/QPDFJob.hh>
|
|
|
|
#include <qpdf/QUtil.hh>
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <iostream>
|
2022-01-26 20:46:28 +00:00
|
|
|
|
2023-05-27 17:19:52 +00:00
|
|
|
// This program is a simple demonstration of different ways to use the QPDFJob API.
|
2022-01-26 20:46:28 +00:00
|
|
|
|
2022-07-26 11:37:50 +00:00
|
|
|
static char const* whoami = nullptr;
|
2022-01-26 20:46:28 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
static void
|
|
|
|
usage()
|
2022-01-26 20:46:28 +00:00
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
std::cerr << "Usage: " << whoami << std::endl
|
2023-12-23 02:45:10 +00:00
|
|
|
<< "This program linearizes the first page of in.pdf to out1.pdf, out2.pdf, and"
|
|
|
|
<< std::endl
|
|
|
|
<< " out3.pdf, each demonstrating a different way to use the QPDFJob API"
|
|
|
|
<< std::endl;
|
2022-01-26 20:46:28 +00:00
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
int
|
|
|
|
main(int argc, char* argv[])
|
2022-01-26 20:46:28 +00:00
|
|
|
{
|
|
|
|
whoami = QUtil::getWhoami(argv[0]);
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (argc != 1) {
|
2022-02-01 14:58:32 +00:00
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
2023-05-27 17:19:52 +00:00
|
|
|
// The examples below all catch std::exception. Note that QPDFUsage can be caught separately to
|
|
|
|
// report on errors in using the API itself. For CLI, this is command-line usage. For JSON or
|
|
|
|
// the API, it would be errors from the equivalent invocation.
|
2022-02-01 14:58:32 +00:00
|
|
|
|
|
|
|
// Note that staticId is used for testing only.
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
try {
|
2022-02-01 14:58:32 +00:00
|
|
|
// Use the config API
|
2022-01-26 20:46:28 +00:00
|
|
|
QPDFJob j;
|
|
|
|
j.config()
|
2022-02-01 14:58:32 +00:00
|
|
|
->inputFile("in.pdf")
|
|
|
|
->outputFile("out1.pdf")
|
2022-01-26 20:46:28 +00:00
|
|
|
->pages()
|
2024-01-09 16:41:18 +00:00
|
|
|
// Prior to qpdf 11.9.0, call ->pageSpec(file, range, password)
|
|
|
|
->file(".")
|
|
|
|
->range("1")
|
2022-01-26 20:46:28 +00:00
|
|
|
->endPages()
|
2022-02-01 14:58:32 +00:00
|
|
|
->linearize()
|
2023-12-23 02:45:10 +00:00
|
|
|
->staticId() // for testing only
|
2023-12-20 18:37:21 +00:00
|
|
|
->compressStreams("n") // avoid dependency on zlib output
|
2022-01-29 14:01:20 +00:00
|
|
|
->checkConfiguration();
|
2022-01-26 20:46:28 +00:00
|
|
|
j.run();
|
2022-02-01 14:58:32 +00:00
|
|
|
std::cout << "out1 status: " << j.getExitCode() << std::endl;
|
2022-04-02 21:14:10 +00:00
|
|
|
} catch (std::exception& e) {
|
2022-02-01 14:58:32 +00:00
|
|
|
std::cerr << "exception: " << e.what() << std::endl;
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
try {
|
2022-02-01 14:58:32 +00:00
|
|
|
char const* new_argv[] = {
|
|
|
|
whoami,
|
|
|
|
"in.pdf",
|
|
|
|
"out2.pdf",
|
|
|
|
"--linearize",
|
|
|
|
"--pages",
|
|
|
|
".",
|
|
|
|
"1",
|
|
|
|
"--",
|
|
|
|
"--static-id",
|
2023-12-20 18:37:21 +00:00
|
|
|
"--compress-streams=n", // avoid dependency on zlib output
|
2022-04-02 21:14:10 +00:00
|
|
|
nullptr};
|
2022-02-01 14:58:32 +00:00
|
|
|
QPDFJob j;
|
2022-02-01 18:49:11 +00:00
|
|
|
j.initializeFromArgv(new_argv);
|
2022-02-01 14:58:32 +00:00
|
|
|
j.run();
|
|
|
|
std::cout << "out2 status: " << j.getExitCode() << std::endl;
|
2022-04-02 21:14:10 +00:00
|
|
|
} catch (std::exception& e) {
|
2022-01-26 20:46:28 +00:00
|
|
|
std::cerr << "exception: " << e.what() << std::endl;
|
|
|
|
return 2;
|
|
|
|
}
|
2022-02-01 14:58:32 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
try {
|
2022-02-01 14:58:32 +00:00
|
|
|
// Use the JSON API
|
|
|
|
QPDFJob j;
|
|
|
|
j.initializeFromJson(R"({
|
|
|
|
"inputFile": "in.pdf",
|
|
|
|
"outputFile": "out3.pdf",
|
|
|
|
"staticId": "",
|
|
|
|
"linearize": "",
|
2023-12-20 18:37:21 +00:00
|
|
|
"compressStreams": "n",
|
2022-02-01 14:58:32 +00:00
|
|
|
"pages": [
|
|
|
|
{
|
|
|
|
"file": ".",
|
|
|
|
"range": "1"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
)");
|
|
|
|
j.run();
|
|
|
|
std::cout << "out3 status: " << j.getExitCode() << std::endl;
|
2022-04-02 21:14:10 +00:00
|
|
|
} catch (std::exception& e) {
|
2022-02-01 14:58:32 +00:00
|
|
|
std::cerr << "exception: " << e.what() << std::endl;
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2022-01-26 20:46:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|