2008-04-29 12:55:25 +00:00
|
|
|
// Author: Vitaliy Pavlyuk
|
|
|
|
|
|
|
|
#include <qpdf/QPDF.hh>
|
|
|
|
#include <qpdf/QPDFObjectHandle.hh>
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <qpdf/QPDFWriter.hh>
|
2008-04-29 12:55:25 +00:00
|
|
|
#include <qpdf/QTC.hh>
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <qpdf/QUtil.hh>
|
2023-05-20 11:22:32 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2023-05-20 18:13:09 +00:00
|
|
|
#include <iostream>
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
static char const* version = "1.1";
|
2022-07-26 11:37:50 +00:00
|
|
|
static char const* whoami = nullptr;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
void
|
|
|
|
usage()
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2023-05-21 17:35:09 +00:00
|
|
|
std::cerr << "Usage: " << whoami << " --in in_file [--out out_file] [--key key [--val val]?]+\n"
|
2022-04-02 21:14:10 +00:00
|
|
|
<< "Modifies/Adds/Removes PDF /Info entries in the in_file\n"
|
|
|
|
<< "and stores the result in out_file\n"
|
|
|
|
<< "Special mode: " << whoami << " --dump file\n"
|
|
|
|
<< "dumps all /Info entries to stdout\n";
|
2008-04-29 12:55:25 +00:00
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
void
|
2023-05-21 17:35:09 +00:00
|
|
|
dumpInfoDict(QPDF& pdf, std::ostream& os = std::cout, std::string const& sep = ":\t")
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
QPDFObjectHandle trailer = pdf.getTrailer();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (trailer.hasKey("/Info")) {
|
2022-04-30 13:43:07 +00:00
|
|
|
for (auto& it: trailer.getKey("/Info").ditems()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
std::string val;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (it.second.isString()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
val = it.second.getStringValue();
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (it.second.isName()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
val = it.second.getName();
|
2022-04-02 21:14:10 +00:00
|
|
|
} else // according to PDF Spec 1.5, shouldn't happen
|
2022-02-08 14:18:08 +00:00
|
|
|
{
|
|
|
|
val = it.second.unparseResolved();
|
|
|
|
}
|
|
|
|
os << it.first.substr(1) << sep << val << std::endl; // skip '/'
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
void
|
|
|
|
pdfDumpInfoDict(char const* fname)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
try {
|
2022-02-08 14:18:08 +00:00
|
|
|
QPDF pdf;
|
|
|
|
pdf.processFile(fname);
|
|
|
|
dumpInfoDict(pdf);
|
2022-04-02 21:14:10 +00:00
|
|
|
} catch (std::exception& e) {
|
2022-02-08 14:18:08 +00:00
|
|
|
std::cerr << e.what() << std::endl;
|
|
|
|
exit(2);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
int
|
|
|
|
main(int argc, char* argv[])
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
bool static_id = false;
|
|
|
|
std::map<std::string, std::string> Keys;
|
|
|
|
|
2009-07-15 04:26:32 +00:00
|
|
|
whoami = QUtil::getWhoami(argv[0]);
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if ((argc == 2) && (!strcmp(argv[1], "--version"))) {
|
2022-02-08 14:18:08 +00:00
|
|
|
std::cout << whoami << " version " << version << std::endl;
|
|
|
|
exit(0);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if ((argc == 3) && (!strcmp(argv[1], "--dump"))) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("examples", "pdf-mod-info --dump");
|
|
|
|
pdfDumpInfoDict(argv[2]);
|
|
|
|
exit(0);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2022-07-26 11:37:50 +00:00
|
|
|
char* fl_in = nullptr;
|
|
|
|
char* fl_out = nullptr;
|
2009-10-14 01:06:18 +00:00
|
|
|
std::string cur_key;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
for (int i = 1; i < argc; ++i) {
|
|
|
|
if ((!strcmp(argv[i], "--in")) && (++i < argc)) {
|
2022-02-08 14:18:08 +00:00
|
|
|
fl_in = argv[i];
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if ((!strcmp(argv[i], "--out")) && (++i < argc)) {
|
2022-02-08 14:18:08 +00:00
|
|
|
fl_out = argv[i];
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (!strcmp(argv[i], "--static-id")) // don't document
|
2022-02-08 14:18:08 +00:00
|
|
|
{
|
|
|
|
static_id = true; // this should be used in test suites only
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if ((!strcmp(argv[i], "--key")) && (++i < argc)) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("examples", "pdf-mod-info -key");
|
|
|
|
cur_key = argv[i];
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!((cur_key.length() > 0) && (cur_key.at(0) == '/'))) {
|
2022-02-08 14:18:08 +00:00
|
|
|
cur_key = "/" + cur_key;
|
|
|
|
}
|
|
|
|
Keys[cur_key] = "";
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if ((!strcmp(argv[i], "--val")) && (++i < argc)) {
|
|
|
|
if (cur_key.empty()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("examples", "pdf-mod-info usage wrong val");
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
QTC::TC("examples", "pdf-mod-info -val");
|
|
|
|
Keys[cur_key] = argv[i];
|
|
|
|
cur_key.clear();
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("examples", "pdf-mod-info usage junk");
|
|
|
|
usage();
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!fl_in) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("examples", "pdf-mod-info no in file");
|
|
|
|
usage();
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!fl_out) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("examples", "pdf-mod-info in-place");
|
|
|
|
fl_out = fl_in;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (Keys.size() == 0) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("examples", "pdf-mod-info no keys");
|
|
|
|
usage();
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2009-07-15 04:26:32 +00:00
|
|
|
std::string fl_tmp = fl_out;
|
|
|
|
fl_tmp += ".tmp";
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
try {
|
2022-02-08 14:18:08 +00:00
|
|
|
QPDF file;
|
|
|
|
file.processFile(fl_in);
|
|
|
|
|
|
|
|
QPDFObjectHandle filetrailer = file.getTrailer();
|
|
|
|
QPDFObjectHandle fileinfo;
|
|
|
|
|
2022-04-30 17:23:18 +00:00
|
|
|
for (auto const& it: Keys) {
|
2024-06-19 16:18:43 +00:00
|
|
|
if (!fileinfo) {
|
2022-04-02 21:14:10 +00:00
|
|
|
if (filetrailer.hasKey("/Info")) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("examples", "pdf-mod-info has info");
|
|
|
|
fileinfo = filetrailer.getKey("/Info");
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("examples", "pdf-mod-info file no info");
|
|
|
|
fileinfo = QPDFObjectHandle::newDictionary();
|
|
|
|
filetrailer.replaceKey("/Info", fileinfo);
|
|
|
|
}
|
|
|
|
}
|
2022-04-30 17:23:18 +00:00
|
|
|
if (it.second == "") {
|
|
|
|
fileinfo.removeKey(it.first);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-04-30 17:23:18 +00:00
|
|
|
QPDFObjectHandle elt = fileinfo.newString(it.second);
|
2022-02-08 14:18:08 +00:00
|
|
|
elt.makeDirect();
|
2022-04-30 17:23:18 +00:00
|
|
|
fileinfo.replaceKey(it.first, elt);
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
QPDFWriter w(file, fl_tmp.c_str());
|
|
|
|
w.setStreamDataMode(qpdf_s_preserve);
|
|
|
|
w.setLinearization(true);
|
|
|
|
w.setStaticID(static_id); // for testing only
|
|
|
|
w.write();
|
2022-04-02 21:14:10 +00:00
|
|
|
} catch (std::exception& e) {
|
2022-02-08 14:18:08 +00:00
|
|
|
std::cerr << e.what() << std::endl;
|
|
|
|
exit(2);
|
2009-07-15 04:26:32 +00:00
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
try {
|
|
|
|
(void)remove(fl_out);
|
|
|
|
QUtil::os_wrapper(
|
2023-05-21 17:35:09 +00:00
|
|
|
"rename " + fl_tmp + " " + std::string(fl_out), rename(fl_tmp.c_str(), fl_out));
|
2022-04-02 21:14:10 +00:00
|
|
|
} catch (std::exception& e) {
|
2022-02-08 14:18:08 +00:00
|
|
|
std::cerr << e.what() << std::endl;
|
|
|
|
exit(2);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|