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>
|
2008-04-29 12:55:25 +00:00
|
|
|
#include <iostream>
|
2013-02-28 20:49:08 +00:00
|
|
|
#include <stdio.h>
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
static char const* version = "1.1";
|
|
|
|
static char const* whoami = 0;
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
void
|
|
|
|
usage()
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
std::cerr << "Usage: " << whoami
|
|
|
|
<< " --in in_file [--out out_file] [--key key [--val val]?]+\n"
|
|
|
|
<< "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
|
|
|
|
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")) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
char* fl_in = 0;
|
|
|
|
char* fl_out = 0;
|
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;
|
|
|
|
|
|
|
|
for (std::map<std::string, std::string>::const_iterator it =
|
2022-04-02 21:14:10 +00:00
|
|
|
Keys.begin();
|
|
|
|
Keys.end() != it;
|
|
|
|
++it) {
|
|
|
|
if (!fileinfo.isInitialized()) {
|
|
|
|
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-02 21:14:10 +00:00
|
|
|
if (it->second == "") {
|
2022-02-08 14:18:08 +00:00
|
|
|
fileinfo.removeKey(it->first);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
QPDFObjectHandle elt = fileinfo.newString(it->second);
|
|
|
|
elt.makeDirect();
|
|
|
|
fileinfo.replaceKey(it->first, elt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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(
|
|
|
|
"rename " + fl_tmp + " " + std::string(fl_out),
|
|
|
|
rename(fl_tmp.c_str(), fl_out));
|
|
|
|
} 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;
|
|
|
|
}
|