2022-01-03 15:16:28 +00:00
|
|
|
#include <qpdf/QPDFJob.hh>
|
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
#include <qpdf/ClosedFileInputSource.hh>
|
|
|
|
#include <qpdf/FileInputSource.hh>
|
|
|
|
#include <qpdf/Pl_Count.hh>
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <qpdf/Pl_DCT.hh>
|
|
|
|
#include <qpdf/Pl_Discard.hh>
|
2022-01-03 15:16:28 +00:00
|
|
|
#include <qpdf/Pl_Flate.hh>
|
2022-05-06 20:37:37 +00:00
|
|
|
#include <qpdf/Pl_OStream.hh>
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <qpdf/Pl_StdioFile.hh>
|
2022-05-06 21:49:28 +00:00
|
|
|
#include <qpdf/Pl_String.hh>
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <qpdf/QIntC.hh>
|
2022-01-03 15:16:28 +00:00
|
|
|
#include <qpdf/QPDF.hh>
|
|
|
|
#include <qpdf/QPDFAcroFormDocumentHelper.hh>
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <qpdf/QPDFArgParser.hh>
|
2022-01-03 15:16:28 +00:00
|
|
|
#include <qpdf/QPDFCryptoProvider.hh>
|
|
|
|
#include <qpdf/QPDFEmbeddedFileDocumentHelper.hh>
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <qpdf/QPDFExc.hh>
|
2022-06-05 17:30:42 +00:00
|
|
|
#include <qpdf/QPDFLogger.hh>
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <qpdf/QPDFOutlineDocumentHelper.hh>
|
|
|
|
#include <qpdf/QPDFPageDocumentHelper.hh>
|
|
|
|
#include <qpdf/QPDFPageLabelDocumentHelper.hh>
|
|
|
|
#include <qpdf/QPDFPageObjectHelper.hh>
|
|
|
|
#include <qpdf/QPDFSystemError.hh>
|
|
|
|
#include <qpdf/QPDFUsage.hh>
|
2022-01-03 15:16:28 +00:00
|
|
|
#include <qpdf/QPDFWriter.hh>
|
2022-06-05 17:30:42 +00:00
|
|
|
#include <qpdf/QTC.hh>
|
|
|
|
#include <qpdf/QUtil.hh>
|
2022-01-03 15:16:28 +00:00
|
|
|
|
2022-01-26 21:48:18 +00:00
|
|
|
#include <qpdf/auto_job_schema.hh> // JOB_SCHEMA_DATA
|
|
|
|
|
2022-01-03 15:16:28 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
class ImageOptimizer: public QPDFObjectHandle::StreamDataProvider
|
|
|
|
{
|
|
|
|
public:
|
2022-04-02 21:14:10 +00:00
|
|
|
ImageOptimizer(
|
|
|
|
QPDFJob& o,
|
|
|
|
size_t oi_min_width,
|
|
|
|
size_t oi_min_height,
|
|
|
|
size_t oi_min_area,
|
|
|
|
QPDFObjectHandle& image);
|
2022-04-15 23:44:07 +00:00
|
|
|
virtual ~ImageOptimizer() = default;
|
2022-07-24 13:16:37 +00:00
|
|
|
virtual void provideStreamData(QPDFObjGen const&, Pipeline* pipeline);
|
2022-04-02 21:14:10 +00:00
|
|
|
std::shared_ptr<Pipeline>
|
|
|
|
makePipeline(std::string const& description, Pipeline* next);
|
2022-01-03 15:16:28 +00:00
|
|
|
bool evaluate(std::string const& description);
|
|
|
|
|
|
|
|
private:
|
|
|
|
QPDFJob& o;
|
2022-01-26 21:40:14 +00:00
|
|
|
size_t oi_min_width;
|
|
|
|
size_t oi_min_height;
|
|
|
|
size_t oi_min_area;
|
2022-01-03 15:16:28 +00:00
|
|
|
QPDFObjectHandle image;
|
|
|
|
};
|
|
|
|
|
|
|
|
class DiscardContents: public QPDFObjectHandle::ParserCallbacks
|
|
|
|
{
|
|
|
|
public:
|
2022-04-15 23:44:07 +00:00
|
|
|
virtual ~DiscardContents() = default;
|
2022-04-02 21:14:10 +00:00
|
|
|
virtual void
|
|
|
|
handleObject(QPDFObjectHandle)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
virtual void
|
|
|
|
handleEOF()
|
|
|
|
{
|
|
|
|
}
|
2022-01-03 15:16:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct QPDFPageData
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
QPDFPageData(
|
|
|
|
std::string const& filename, QPDF* qpdf, std::string const& range);
|
2022-01-03 15:16:28 +00:00
|
|
|
QPDFPageData(QPDFPageData const& other, int page);
|
|
|
|
|
|
|
|
std::string filename;
|
|
|
|
QPDF* qpdf;
|
|
|
|
std::vector<QPDFObjectHandle> orig_pages;
|
|
|
|
std::vector<int> selected_pages;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ProgressReporter: public QPDFWriter::ProgressReporter
|
|
|
|
{
|
|
|
|
public:
|
2022-04-02 21:14:10 +00:00
|
|
|
ProgressReporter(
|
2022-06-05 17:30:42 +00:00
|
|
|
Pipeline& p, std::string const& prefix, char const* filename) :
|
|
|
|
p(p),
|
2022-01-05 20:01:35 +00:00
|
|
|
prefix(prefix),
|
2022-01-03 15:16:28 +00:00
|
|
|
filename(filename)
|
|
|
|
{
|
|
|
|
}
|
2022-04-15 23:44:07 +00:00
|
|
|
virtual ~ProgressReporter() = default;
|
2022-01-03 15:16:28 +00:00
|
|
|
virtual void reportProgress(int);
|
2022-04-02 21:14:10 +00:00
|
|
|
|
2022-01-03 15:16:28 +00:00
|
|
|
private:
|
2022-06-05 17:30:42 +00:00
|
|
|
Pipeline& p;
|
2022-01-05 20:01:35 +00:00
|
|
|
std::string prefix;
|
2022-01-03 15:16:28 +00:00
|
|
|
std::string filename;
|
|
|
|
};
|
2022-04-02 21:14:10 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
ImageOptimizer::ImageOptimizer(
|
|
|
|
QPDFJob& o,
|
|
|
|
size_t oi_min_width,
|
|
|
|
size_t oi_min_height,
|
|
|
|
size_t oi_min_area,
|
|
|
|
QPDFObjectHandle& image) :
|
2022-01-05 21:50:18 +00:00
|
|
|
o(o),
|
2022-01-26 21:40:14 +00:00
|
|
|
oi_min_width(oi_min_width),
|
|
|
|
oi_min_height(oi_min_height),
|
|
|
|
oi_min_area(oi_min_area),
|
2022-01-05 21:50:18 +00:00
|
|
|
image(image)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-01-22 22:37:51 +00:00
|
|
|
std::shared_ptr<Pipeline>
|
2022-01-05 21:50:18 +00:00
|
|
|
ImageOptimizer::makePipeline(std::string const& description, Pipeline* next)
|
|
|
|
{
|
2022-01-22 22:37:51 +00:00
|
|
|
std::shared_ptr<Pipeline> result;
|
2022-01-05 21:50:18 +00:00
|
|
|
QPDFObjectHandle dict = image.getDict();
|
|
|
|
QPDFObjectHandle w_obj = dict.getKey("/Width");
|
|
|
|
QPDFObjectHandle h_obj = dict.getKey("/Height");
|
|
|
|
QPDFObjectHandle colorspace_obj = dict.getKey("/ColorSpace");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!(w_obj.isNumber() && h_obj.isNumber())) {
|
|
|
|
if (!description.empty()) {
|
2022-06-05 17:30:42 +00:00
|
|
|
o.doIfVerbose([&](Pipeline& v, std::string const& prefix) {
|
|
|
|
v << prefix << ": " << description
|
|
|
|
<< ": not optimizing because image dictionary"
|
|
|
|
<< " is missing required keys\n";
|
2022-01-05 21:50:18 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
QPDFObjectHandle components_obj = dict.getKey("/BitsPerComponent");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!(components_obj.isInteger() && (components_obj.getIntValue() == 8))) {
|
2022-02-01 12:49:00 +00:00
|
|
|
QTC::TC("qpdf", "QPDFJob image optimize bits per component");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!description.empty()) {
|
2022-06-05 17:30:42 +00:00
|
|
|
o.doIfVerbose([&](Pipeline& v, std::string const& prefix) {
|
|
|
|
v << prefix << ": " << description
|
|
|
|
<< ": not optimizing because image has other than"
|
|
|
|
<< " 8 bits per component\n";
|
2022-01-05 21:50:18 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
// Files have been seen in the wild whose width and height are
|
|
|
|
// floating point, which is goofy, but we can deal with it.
|
|
|
|
JDIMENSION w = 0;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (w_obj.isInteger()) {
|
2022-01-05 21:50:18 +00:00
|
|
|
w = w_obj.getUIntValueAsUInt();
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-01-05 21:50:18 +00:00
|
|
|
w = static_cast<JDIMENSION>(w_obj.getNumericValue());
|
|
|
|
}
|
|
|
|
JDIMENSION h = 0;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (h_obj.isInteger()) {
|
2022-01-05 21:50:18 +00:00
|
|
|
h = h_obj.getUIntValueAsUInt();
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-01-05 21:50:18 +00:00
|
|
|
h = static_cast<JDIMENSION>(h_obj.getNumericValue());
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
std::string colorspace =
|
|
|
|
(colorspace_obj.isName() ? colorspace_obj.getName() : std::string());
|
2022-01-05 21:50:18 +00:00
|
|
|
int components = 0;
|
|
|
|
J_COLOR_SPACE cs = JCS_UNKNOWN;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (colorspace == "/DeviceRGB") {
|
2022-01-05 21:50:18 +00:00
|
|
|
components = 3;
|
|
|
|
cs = JCS_RGB;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (colorspace == "/DeviceGray") {
|
2022-01-05 21:50:18 +00:00
|
|
|
components = 1;
|
|
|
|
cs = JCS_GRAYSCALE;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (colorspace == "/DeviceCMYK") {
|
2022-01-05 21:50:18 +00:00
|
|
|
components = 4;
|
|
|
|
cs = JCS_CMYK;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-01 12:49:00 +00:00
|
|
|
QTC::TC("qpdf", "QPDFJob image optimize colorspace");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!description.empty()) {
|
2022-06-05 17:30:42 +00:00
|
|
|
o.doIfVerbose([&](Pipeline& v, std::string const& prefix) {
|
|
|
|
v << prefix << ": " << description
|
|
|
|
<< ": not optimizing because qpdf can't optimize"
|
|
|
|
<< " images with this colorspace\n";
|
2022-01-05 21:50:18 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2022-01-26 21:40:14 +00:00
|
|
|
if (((this->oi_min_width > 0) && (w <= this->oi_min_width)) ||
|
|
|
|
((this->oi_min_height > 0) && (h <= this->oi_min_height)) ||
|
2022-04-02 21:14:10 +00:00
|
|
|
((this->oi_min_area > 0) && ((w * h) <= this->oi_min_area))) {
|
2022-02-01 12:49:00 +00:00
|
|
|
QTC::TC("qpdf", "QPDFJob image optimize too small");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!description.empty()) {
|
2022-06-05 17:30:42 +00:00
|
|
|
o.doIfVerbose([&](Pipeline& v, std::string const& prefix) {
|
|
|
|
v << prefix << ": " << description
|
|
|
|
<< ": not optimizing because image"
|
|
|
|
<< " is smaller than requested minimum dimensions\n";
|
2022-01-05 21:50:18 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-01-22 22:37:51 +00:00
|
|
|
result = std::make_shared<Pl_DCT>("jpg", next, w, h, components, cs);
|
2022-01-05 21:50:18 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ImageOptimizer::evaluate(std::string const& description)
|
|
|
|
{
|
2022-07-26 11:37:50 +00:00
|
|
|
if (!image.pipeStreamData(nullptr, 0, qpdf_dl_specialized, true)) {
|
2022-02-01 12:49:00 +00:00
|
|
|
QTC::TC("qpdf", "QPDFJob image optimize no pipeline");
|
2022-06-05 17:30:42 +00:00
|
|
|
o.doIfVerbose([&](Pipeline& v, std::string const& prefix) {
|
|
|
|
v << prefix << ": " << description
|
|
|
|
<< ": not optimizing because unable to decode data"
|
|
|
|
<< " or data already uses DCT\n";
|
2022-01-05 21:50:18 +00:00
|
|
|
});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Pl_Discard d;
|
|
|
|
Pl_Count c("count", &d);
|
2022-01-22 22:37:51 +00:00
|
|
|
std::shared_ptr<Pipeline> p = makePipeline(description, &c);
|
2022-08-07 09:33:25 +00:00
|
|
|
if (p == nullptr) {
|
2022-01-05 21:50:18 +00:00
|
|
|
// message issued by makePipeline
|
|
|
|
return false;
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!image.pipeStreamData(p.get(), 0, qpdf_dl_specialized)) {
|
2022-01-05 21:50:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
long long orig_length = image.getDict().getKey("/Length").getIntValue();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (c.getCount() >= orig_length) {
|
2022-02-01 12:49:00 +00:00
|
|
|
QTC::TC("qpdf", "QPDFJob image optimize no shrink");
|
2022-06-05 17:30:42 +00:00
|
|
|
o.doIfVerbose([&](Pipeline& v, std::string const& prefix) {
|
|
|
|
v << prefix << ": " << description
|
|
|
|
<< ": not optimizing because DCT compression does not"
|
|
|
|
<< " reduce image size\n";
|
2022-01-05 21:50:18 +00:00
|
|
|
});
|
|
|
|
return false;
|
|
|
|
}
|
2022-06-05 17:30:42 +00:00
|
|
|
o.doIfVerbose([&](Pipeline& v, std::string const& prefix) {
|
|
|
|
v << prefix << ": " << description
|
|
|
|
<< ": optimizing image reduces size from " << orig_length << " to "
|
|
|
|
<< c.getCount() << "\n";
|
2022-01-05 21:50:18 +00:00
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-07-24 13:16:37 +00:00
|
|
|
ImageOptimizer::provideStreamData(QPDFObjGen const&, Pipeline* pipeline)
|
2022-01-05 21:50:18 +00:00
|
|
|
{
|
2022-01-22 22:37:51 +00:00
|
|
|
std::shared_ptr<Pipeline> p = makePipeline("", pipeline);
|
2022-08-07 09:33:25 +00:00
|
|
|
if (p == nullptr) {
|
2022-01-05 21:50:18 +00:00
|
|
|
// Should not be possible
|
|
|
|
image.warnIfPossible("unable to create pipeline after previous"
|
|
|
|
" success; image data will be lost");
|
|
|
|
pipeline->finish();
|
|
|
|
return;
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
image.pipeStreamData(p.get(), 0, qpdf_dl_specialized, false, false);
|
2022-01-05 21:50:18 +00:00
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
QPDFJob::PageSpec::PageSpec(
|
|
|
|
std::string const& filename,
|
|
|
|
char const* password,
|
|
|
|
std::string const& range) :
|
2022-01-22 23:43:05 +00:00
|
|
|
filename(filename),
|
|
|
|
range(range)
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (password) {
|
2022-01-22 23:43:05 +00:00
|
|
|
this->password = QUtil::make_shared_cstr(password);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
QPDFPageData::QPDFPageData(
|
|
|
|
std::string const& filename, QPDF* qpdf, std::string const& range) :
|
2022-01-03 15:16:28 +00:00
|
|
|
filename(filename),
|
|
|
|
qpdf(qpdf),
|
|
|
|
orig_pages(qpdf->getAllPages())
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
try {
|
|
|
|
this->selected_pages = QUtil::parse_numrange(
|
|
|
|
range.c_str(), QIntC::to_int(this->orig_pages.size()));
|
|
|
|
} catch (std::runtime_error& e) {
|
2022-01-03 15:16:28 +00:00
|
|
|
throw std::runtime_error(
|
|
|
|
"parsing numeric range for " + filename + ": " + e.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QPDFPageData::QPDFPageData(QPDFPageData const& other, int page) :
|
|
|
|
filename(other.filename),
|
|
|
|
qpdf(other.qpdf),
|
|
|
|
orig_pages(other.orig_pages)
|
|
|
|
{
|
|
|
|
this->selected_pages.push_back(page);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ProgressReporter::reportProgress(int percentage)
|
|
|
|
{
|
2022-06-05 17:30:42 +00:00
|
|
|
this->p << prefix << ": " << filename << ": write progress: " << percentage
|
|
|
|
<< "%\n";
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
|
2022-02-26 13:16:12 +00:00
|
|
|
// These default values are duplicated in help and docs.
|
|
|
|
static int constexpr DEFAULT_KEEP_FILES_OPEN_THRESHOLD = 200;
|
|
|
|
static int constexpr DEFAULT_OI_MIN_WIDTH = 128;
|
|
|
|
static int constexpr DEFAULT_OI_MIN_HEIGHT = 128;
|
|
|
|
static int constexpr DEFAULT_OI_MIN_AREA = 16384;
|
|
|
|
static int constexpr DEFAULT_II_MIN_BYTES = 1024;
|
|
|
|
|
2022-01-03 15:16:28 +00:00
|
|
|
QPDFJob::Members::Members() :
|
2022-06-05 17:30:42 +00:00
|
|
|
log(QPDFLogger::defaultLogger()),
|
2022-01-05 20:01:35 +00:00
|
|
|
message_prefix("qpdf"),
|
2022-01-03 15:16:28 +00:00
|
|
|
warnings(false),
|
2022-01-24 00:29:18 +00:00
|
|
|
encryption_status(0),
|
2022-01-26 19:56:24 +00:00
|
|
|
verbose(false),
|
2022-07-26 11:37:50 +00:00
|
|
|
password(nullptr),
|
2022-01-03 15:16:28 +00:00
|
|
|
linearize(false),
|
|
|
|
decrypt(false),
|
|
|
|
split_pages(0),
|
|
|
|
progress(false),
|
2022-06-19 00:44:44 +00:00
|
|
|
progress_handler(nullptr),
|
2022-01-03 15:16:28 +00:00
|
|
|
suppress_warnings(false),
|
2022-01-05 22:20:20 +00:00
|
|
|
warnings_exit_zero(false),
|
2022-01-03 15:16:28 +00:00
|
|
|
copy_encryption(false),
|
2022-07-26 11:37:50 +00:00
|
|
|
encryption_file_password(nullptr),
|
2022-01-03 15:16:28 +00:00
|
|
|
encrypt(false),
|
|
|
|
password_is_hex_key(false),
|
|
|
|
suppress_password_recovery(false),
|
|
|
|
password_mode(pm_auto),
|
|
|
|
allow_insecure(false),
|
|
|
|
allow_weak_crypto(false),
|
|
|
|
keylen(0),
|
|
|
|
r2_print(true),
|
|
|
|
r2_modify(true),
|
|
|
|
r2_extract(true),
|
|
|
|
r2_annotate(true),
|
|
|
|
r3_accessibility(true),
|
|
|
|
r3_extract(true),
|
|
|
|
r3_assemble(true),
|
|
|
|
r3_annotate_and_form(true),
|
|
|
|
r3_form_filling(true),
|
|
|
|
r3_modify_other(true),
|
|
|
|
r3_print(qpdf_r3p_full),
|
|
|
|
force_V4(false),
|
|
|
|
force_R5(false),
|
|
|
|
cleartext_metadata(false),
|
|
|
|
use_aes(false),
|
|
|
|
stream_data_set(false),
|
|
|
|
stream_data_mode(qpdf_s_compress),
|
|
|
|
compress_streams(true),
|
|
|
|
compress_streams_set(false),
|
|
|
|
recompress_flate(false),
|
|
|
|
recompress_flate_set(false),
|
|
|
|
compression_level(-1),
|
|
|
|
decode_level(qpdf_dl_generalized),
|
|
|
|
decode_level_set(false),
|
|
|
|
normalize_set(false),
|
|
|
|
normalize(false),
|
|
|
|
suppress_recovery(false),
|
|
|
|
object_stream_set(false),
|
|
|
|
object_stream_mode(qpdf_o_preserve),
|
|
|
|
ignore_xref_streams(false),
|
|
|
|
qdf_mode(false),
|
|
|
|
preserve_unreferenced_objects(false),
|
|
|
|
remove_unreferenced_page_resources(re_auto),
|
|
|
|
keep_files_open(true),
|
|
|
|
keep_files_open_set(false),
|
2022-02-26 13:16:12 +00:00
|
|
|
keep_files_open_threshold(DEFAULT_KEEP_FILES_OPEN_THRESHOLD),
|
2022-01-03 15:16:28 +00:00
|
|
|
newline_before_endstream(false),
|
|
|
|
coalesce_contents(false),
|
|
|
|
flatten_annotations(false),
|
|
|
|
flatten_annotations_required(0),
|
|
|
|
flatten_annotations_forbidden(an_invisible | an_hidden),
|
|
|
|
generate_appearances(false),
|
|
|
|
show_npages(false),
|
|
|
|
deterministic_id(false),
|
|
|
|
static_id(false),
|
|
|
|
static_aes_iv(false),
|
|
|
|
suppress_original_object_id(false),
|
|
|
|
show_encryption(false),
|
|
|
|
show_encryption_key(false),
|
|
|
|
check_linearization(false),
|
|
|
|
show_linearization(false),
|
|
|
|
show_xref(false),
|
|
|
|
show_trailer(false),
|
|
|
|
show_obj(0),
|
|
|
|
show_gen(0),
|
|
|
|
show_raw_stream_data(false),
|
|
|
|
show_filtered_stream_data(false),
|
|
|
|
show_pages(false),
|
|
|
|
show_page_images(false),
|
|
|
|
collate(0),
|
|
|
|
flatten_rotation(false),
|
|
|
|
list_attachments(false),
|
2022-01-31 19:20:06 +00:00
|
|
|
json_version(0),
|
2022-05-07 17:33:45 +00:00
|
|
|
json_stream_data(qpdf_sj_none),
|
2022-05-18 22:22:57 +00:00
|
|
|
json_stream_data_set(false),
|
2022-05-06 21:29:22 +00:00
|
|
|
test_json_schema(false),
|
2022-01-03 15:16:28 +00:00
|
|
|
check(false),
|
|
|
|
optimize_images(false),
|
|
|
|
externalize_inline_images(false),
|
|
|
|
keep_inline_images(false),
|
|
|
|
remove_page_labels(false),
|
2022-02-26 13:16:12 +00:00
|
|
|
oi_min_width(DEFAULT_OI_MIN_WIDTH),
|
|
|
|
oi_min_height(DEFAULT_OI_MIN_HEIGHT),
|
|
|
|
oi_min_area(DEFAULT_OI_MIN_AREA),
|
|
|
|
ii_min_bytes(DEFAULT_II_MIN_BYTES),
|
2022-01-03 15:16:28 +00:00
|
|
|
underlay("underlay"),
|
|
|
|
overlay("overlay"),
|
2022-07-26 11:37:50 +00:00
|
|
|
under_overlay(nullptr),
|
2022-01-03 15:16:28 +00:00
|
|
|
require_outfile(true),
|
|
|
|
replace_input(false),
|
|
|
|
check_is_encrypted(false),
|
2022-05-18 22:22:57 +00:00
|
|
|
check_requires_password(false),
|
|
|
|
json_input(false),
|
2022-07-31 00:53:30 +00:00
|
|
|
json_output(false)
|
2022-01-26 19:56:24 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QPDFJob::QPDFJob() :
|
2022-01-03 15:16:28 +00:00
|
|
|
m(new Members())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-01-28 12:46:04 +00:00
|
|
|
void
|
|
|
|
QPDFJob::usage(std::string const& msg)
|
|
|
|
{
|
|
|
|
throw QPDFUsage(msg);
|
|
|
|
}
|
|
|
|
|
2022-01-03 17:16:16 +00:00
|
|
|
void
|
2022-01-05 20:01:35 +00:00
|
|
|
QPDFJob::setMessagePrefix(std::string const& message_prefix)
|
2022-01-03 17:16:16 +00:00
|
|
|
{
|
2022-01-05 20:01:35 +00:00
|
|
|
this->m->message_prefix = message_prefix;
|
2022-01-03 17:16:16 +00:00
|
|
|
}
|
|
|
|
|
2022-06-19 12:20:43 +00:00
|
|
|
std::string
|
|
|
|
QPDFJob::getMessagePrefix() const
|
|
|
|
{
|
|
|
|
return this->m->message_prefix;
|
|
|
|
}
|
|
|
|
|
2022-06-05 17:30:42 +00:00
|
|
|
std::shared_ptr<QPDFLogger>
|
|
|
|
QPDFJob::getLogger()
|
|
|
|
{
|
|
|
|
return this->m->log;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFJob::setLogger(std::shared_ptr<QPDFLogger> l)
|
|
|
|
{
|
|
|
|
this->m->log = l;
|
|
|
|
}
|
|
|
|
|
2022-01-03 15:16:28 +00:00
|
|
|
void
|
|
|
|
QPDFJob::setOutputStreams(std::ostream* out, std::ostream* err)
|
|
|
|
{
|
2022-06-05 17:30:42 +00:00
|
|
|
setLogger(std::make_shared<QPDFLogger>());
|
|
|
|
this->m->log->setOutputStreams(out, err);
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
|
2022-06-19 00:44:44 +00:00
|
|
|
void
|
2022-06-25 12:29:07 +00:00
|
|
|
QPDFJob::registerProgressReporter(std::function<void(int)> handler)
|
|
|
|
{
|
2022-06-19 00:44:44 +00:00
|
|
|
this->m->progress_handler = handler;
|
|
|
|
}
|
|
|
|
|
2022-01-03 17:16:16 +00:00
|
|
|
void
|
|
|
|
QPDFJob::doIfVerbose(
|
2022-06-05 17:30:42 +00:00
|
|
|
std::function<void(Pipeline&, std::string const& prefix)> fn)
|
2022-01-03 17:16:16 +00:00
|
|
|
{
|
2022-06-05 17:30:42 +00:00
|
|
|
if (this->m->verbose) {
|
|
|
|
fn(*this->m->log->getInfo(), this->m->message_prefix);
|
2022-01-03 17:16:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-25 16:07:53 +00:00
|
|
|
std::shared_ptr<QPDFJob::Config>
|
2022-01-24 00:29:18 +00:00
|
|
|
QPDFJob::config()
|
|
|
|
{
|
2022-01-25 16:07:53 +00:00
|
|
|
return std::shared_ptr<Config>(new Config(*this));
|
2022-01-24 00:29:18 +00:00
|
|
|
}
|
|
|
|
|
2022-01-26 21:48:18 +00:00
|
|
|
std::string
|
2022-01-31 12:32:19 +00:00
|
|
|
QPDFJob::job_json_schema_v1()
|
2022-01-26 21:48:18 +00:00
|
|
|
{
|
2022-07-31 12:03:18 +00:00
|
|
|
return job_json_schema(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
QPDFJob::job_json_schema(int version)
|
|
|
|
{
|
|
|
|
if (version != LATEST_JOB_JSON) {
|
|
|
|
throw std::runtime_error("job_json_schema: version must be 1");
|
|
|
|
}
|
2022-01-26 21:48:18 +00:00
|
|
|
return JOB_SCHEMA_DATA;
|
|
|
|
}
|
|
|
|
|
2022-01-26 14:09:29 +00:00
|
|
|
void
|
|
|
|
QPDFJob::parseRotationParameter(std::string const& parameter)
|
|
|
|
{
|
|
|
|
std::string angle_str;
|
|
|
|
std::string range;
|
|
|
|
size_t colon = parameter.find(':');
|
|
|
|
int relative = 0;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (colon != std::string::npos) {
|
|
|
|
if (colon > 0) {
|
2022-01-26 14:09:29 +00:00
|
|
|
angle_str = parameter.substr(0, colon);
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (colon + 1 < parameter.length()) {
|
2022-01-26 14:09:29 +00:00
|
|
|
range = parameter.substr(colon + 1);
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-01-26 14:09:29 +00:00
|
|
|
angle_str = parameter;
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (angle_str.length() > 0) {
|
2022-01-26 14:09:29 +00:00
|
|
|
char first = angle_str.at(0);
|
2022-04-02 21:14:10 +00:00
|
|
|
if ((first == '+') || (first == '-')) {
|
2022-01-26 14:09:29 +00:00
|
|
|
relative = ((first == '+') ? 1 : -1);
|
|
|
|
angle_str = angle_str.substr(1);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (!QUtil::is_digit(angle_str.at(0))) {
|
2022-01-26 14:09:29 +00:00
|
|
|
angle_str = "";
|
|
|
|
}
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (range.empty()) {
|
2022-01-26 14:09:29 +00:00
|
|
|
range = "1-z";
|
|
|
|
}
|
|
|
|
bool range_valid = false;
|
2022-04-02 21:14:10 +00:00
|
|
|
try {
|
2022-01-26 14:38:34 +00:00
|
|
|
QUtil::parse_numrange(range.c_str(), 0);
|
2022-01-26 14:09:29 +00:00
|
|
|
range_valid = true;
|
2022-04-02 21:14:10 +00:00
|
|
|
} catch (std::runtime_error const&) {
|
2022-01-26 14:09:29 +00:00
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
if (range_valid &&
|
2022-04-02 21:14:10 +00:00
|
|
|
((angle_str == "0") || (angle_str == "90") || (angle_str == "180") ||
|
|
|
|
(angle_str == "270"))) {
|
2022-01-26 14:09:29 +00:00
|
|
|
int angle = QUtil::string_to_int(angle_str.c_str());
|
2022-04-02 21:14:10 +00:00
|
|
|
if (relative == -1) {
|
2022-01-26 14:09:29 +00:00
|
|
|
angle = -angle;
|
|
|
|
}
|
2022-01-26 19:56:24 +00:00
|
|
|
m->rotations[range] = RotationSpec(angle, (relative != 0));
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-01-28 12:46:04 +00:00
|
|
|
usage("invalid parameter to rotate: " + parameter);
|
2022-01-26 14:09:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<int>
|
2022-01-26 14:38:34 +00:00
|
|
|
QPDFJob::parseNumrange(char const* range, int max)
|
2022-01-26 14:09:29 +00:00
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
try {
|
2022-01-26 14:09:29 +00:00
|
|
|
return QUtil::parse_numrange(range, max);
|
2022-04-02 21:14:10 +00:00
|
|
|
} catch (std::runtime_error& e) {
|
2022-01-28 12:46:04 +00:00
|
|
|
usage(e.what());
|
2022-01-26 14:09:29 +00:00
|
|
|
}
|
|
|
|
return std::vector<int>();
|
|
|
|
}
|
|
|
|
|
2022-01-05 21:50:18 +00:00
|
|
|
void
|
|
|
|
QPDFJob::run()
|
2022-01-03 15:16:28 +00:00
|
|
|
{
|
2022-01-26 20:46:02 +00:00
|
|
|
checkConfiguration();
|
2022-01-22 22:37:51 +00:00
|
|
|
std::shared_ptr<QPDF> pdf_ph;
|
2022-04-02 21:14:10 +00:00
|
|
|
try {
|
2022-05-18 22:22:57 +00:00
|
|
|
pdf_ph =
|
|
|
|
processFile(m->infilename.get(), m->password.get(), true, true);
|
2022-04-02 21:14:10 +00:00
|
|
|
} catch (QPDFExc& e) {
|
2022-01-05 21:50:18 +00:00
|
|
|
if ((e.getErrorCode() == qpdf_e_password) &&
|
2022-04-02 21:14:10 +00:00
|
|
|
(m->check_is_encrypted || m->check_requires_password)) {
|
2022-01-05 21:50:18 +00:00
|
|
|
// Allow --is-encrypted and --requires-password to
|
|
|
|
// work when an incorrect password is supplied.
|
|
|
|
this->m->encryption_status =
|
2022-04-02 21:14:10 +00:00
|
|
|
qpdf_es_encrypted | qpdf_es_password_incorrect;
|
2022-01-05 21:50:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
QPDF& pdf = *pdf_ph;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (pdf.isEncrypted()) {
|
2022-01-05 21:50:18 +00:00
|
|
|
this->m->encryption_status = qpdf_es_encrypted;
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->check_is_encrypted || m->check_requires_password) {
|
2022-01-05 21:50:18 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-05-08 17:42:16 +00:00
|
|
|
|
|
|
|
// If we are updating from JSON, this has to be done first before
|
|
|
|
// other options may cause transformations to the input.
|
|
|
|
if (!this->m->update_from_json.empty()) {
|
|
|
|
pdf.updateFromJSON(this->m->update_from_json);
|
|
|
|
}
|
|
|
|
|
2022-01-05 21:50:18 +00:00
|
|
|
bool other_warnings = false;
|
2022-01-22 22:37:51 +00:00
|
|
|
std::vector<std::shared_ptr<QPDF>> page_heap;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!m->page_specs.empty()) {
|
2022-01-05 21:50:18 +00:00
|
|
|
handlePageSpecs(pdf, other_warnings, page_heap);
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!m->rotations.empty()) {
|
2022-01-05 21:50:18 +00:00
|
|
|
handleRotations(pdf);
|
|
|
|
}
|
|
|
|
handleUnderOverlay(pdf);
|
|
|
|
handleTransformations(pdf);
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!createsOutput()) {
|
2022-01-05 21:50:18 +00:00
|
|
|
doInspection(pdf);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (m->split_pages) {
|
2022-01-05 21:50:18 +00:00
|
|
|
doSplitPages(pdf, other_warnings);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-01-05 21:50:18 +00:00
|
|
|
writeOutfile(pdf);
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!pdf.getWarnings().empty()) {
|
2022-01-05 21:50:18 +00:00
|
|
|
this->m->warnings = true;
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (this->m->warnings && (!this->m->suppress_warnings)) {
|
|
|
|
if (createsOutput()) {
|
2022-06-05 17:30:42 +00:00
|
|
|
*this->m->log->getWarn()
|
2022-02-01 12:28:26 +00:00
|
|
|
<< this->m->message_prefix
|
|
|
|
<< ": operation succeeded with warnings;"
|
2022-06-05 17:30:42 +00:00
|
|
|
<< " resulting file may have some problems\n";
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-06-05 17:30:42 +00:00
|
|
|
*this->m->log->getWarn() << this->m->message_prefix
|
|
|
|
<< ": operation succeeded with warnings\n";
|
2022-02-01 12:28:26 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-05 21:50:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2022-02-01 12:28:26 +00:00
|
|
|
QPDFJob::hasWarnings() const
|
2022-01-05 21:50:18 +00:00
|
|
|
{
|
|
|
|
return this->m->warnings;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2022-01-23 18:04:08 +00:00
|
|
|
QPDFJob::createsOutput() const
|
2022-01-05 21:50:18 +00:00
|
|
|
{
|
2022-01-26 19:56:24 +00:00
|
|
|
return ((m->outfilename != nullptr) || m->replace_input);
|
2022-01-05 21:50:18 +00:00
|
|
|
}
|
|
|
|
|
2022-02-01 12:28:26 +00:00
|
|
|
int
|
|
|
|
QPDFJob::getExitCode() const
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (this->m->check_is_encrypted) {
|
|
|
|
if (this->m->encryption_status & qpdf_es_encrypted) {
|
2022-02-01 12:49:00 +00:00
|
|
|
QTC::TC("qpdf", "QPDFJob check encrypted encrypted");
|
2022-02-01 12:28:26 +00:00
|
|
|
return 0;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-01 12:49:00 +00:00
|
|
|
QTC::TC("qpdf", "QPDFJob check encrypted not encrypted");
|
2022-02-01 12:28:26 +00:00
|
|
|
return EXIT_IS_NOT_ENCRYPTED;
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (this->m->check_requires_password) {
|
|
|
|
if (this->m->encryption_status & qpdf_es_encrypted) {
|
|
|
|
if (this->m->encryption_status & qpdf_es_password_incorrect) {
|
2022-02-01 12:49:00 +00:00
|
|
|
QTC::TC("qpdf", "QPDFJob check password password incorrect");
|
2022-02-01 12:28:26 +00:00
|
|
|
return 0;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-01 12:49:00 +00:00
|
|
|
QTC::TC("qpdf", "QPDFJob check password password correct");
|
2022-02-01 12:28:26 +00:00
|
|
|
return EXIT_CORRECT_PASSWORD;
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-01 12:49:00 +00:00
|
|
|
QTC::TC("qpdf", "QPDFJob check password not encrypted");
|
2022-02-01 12:28:26 +00:00
|
|
|
return EXIT_IS_NOT_ENCRYPTED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (this->m->warnings && (!this->m->warnings_exit_zero)) {
|
2022-02-01 12:28:26 +00:00
|
|
|
return EXIT_WARNING;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-01-23 18:11:46 +00:00
|
|
|
void
|
|
|
|
QPDFJob::checkConfiguration()
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->replace_input) {
|
|
|
|
if (m->outfilename) {
|
2022-01-23 18:11:46 +00:00
|
|
|
usage("--replace-input may not be used when"
|
|
|
|
" an output file is specified");
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (m->split_pages) {
|
2022-01-23 18:11:46 +00:00
|
|
|
usage("--split-pages may not be used with --replace-input");
|
2022-07-31 14:34:05 +00:00
|
|
|
} else if (m->json_version) {
|
|
|
|
usage("--json may not be used with --replace-input");
|
2022-01-23 18:11:46 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-31 14:34:05 +00:00
|
|
|
if (m->json_version && (m->outfilename == nullptr)) {
|
|
|
|
// The output file is optional with --json for backward
|
|
|
|
// compatibility and defaults to standard output.
|
|
|
|
m->outfilename = QUtil::make_shared_cstr("-");
|
|
|
|
}
|
2022-07-26 11:37:50 +00:00
|
|
|
if (m->infilename == nullptr) {
|
2022-01-23 18:11:46 +00:00
|
|
|
usage("an input file name is required");
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (
|
2022-07-26 11:37:50 +00:00
|
|
|
m->require_outfile && (m->outfilename == nullptr) &&
|
|
|
|
(!m->replace_input)) {
|
2022-01-23 18:11:46 +00:00
|
|
|
usage("an output file name is required; use - for standard output");
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (
|
2022-07-26 11:37:50 +00:00
|
|
|
(!m->require_outfile) &&
|
|
|
|
((m->outfilename != nullptr) || m->replace_input)) {
|
2022-01-23 18:11:46 +00:00
|
|
|
usage("no output file may be given for this option");
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->check_requires_password && m->check_is_encrypted) {
|
2022-01-23 18:11:46 +00:00
|
|
|
usage("--requires-password and --is-encrypted may not be given"
|
|
|
|
" together");
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->encrypt && (!m->allow_insecure) &&
|
|
|
|
(m->owner_password.empty() && (!m->user_password.empty()) &&
|
|
|
|
(m->keylen == 256))) {
|
2022-01-23 18:11:46 +00:00
|
|
|
// Note that empty owner passwords for R < 5 are copied from
|
|
|
|
// the user password, so this lack of security is not an issue
|
|
|
|
// for those files. Also we are consider only the ability to
|
|
|
|
// open the file without a password to be insecure. We are not
|
|
|
|
// concerned about whether the viewer enforces security
|
|
|
|
// settings when the user and owner password match.
|
|
|
|
usage("A PDF with a non-empty user password and an empty owner"
|
|
|
|
" password encrypted with a 256-bit key is insecure as it"
|
|
|
|
" can be opened without a password. If you really want to"
|
|
|
|
" do this, you must also give the --allow-insecure option"
|
|
|
|
" before the -- that follows --encrypt.");
|
|
|
|
}
|
|
|
|
|
2022-06-18 13:40:41 +00:00
|
|
|
bool save_to_stdout = false;
|
2022-01-26 19:56:24 +00:00
|
|
|
if (m->require_outfile && m->outfilename &&
|
2022-04-02 21:14:10 +00:00
|
|
|
(strcmp(m->outfilename.get(), "-") == 0)) {
|
|
|
|
if (m->split_pages) {
|
2022-01-23 18:11:46 +00:00
|
|
|
usage("--split-pages may not be used when"
|
|
|
|
" writing to standard output");
|
|
|
|
}
|
2022-06-18 13:40:41 +00:00
|
|
|
save_to_stdout = true;
|
|
|
|
}
|
|
|
|
if (!m->attachment_to_show.empty()) {
|
|
|
|
save_to_stdout = true;
|
|
|
|
}
|
|
|
|
if (save_to_stdout) {
|
2022-06-18 17:38:36 +00:00
|
|
|
this->m->log->saveToStandardOutput(true);
|
2022-01-23 18:11:46 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if ((!m->split_pages) &&
|
|
|
|
QUtil::same_file(m->infilename.get(), m->outfilename.get())) {
|
2022-02-01 12:49:00 +00:00
|
|
|
QTC::TC("qpdf", "QPDFJob same file error");
|
2022-01-23 18:11:46 +00:00
|
|
|
usage("input file and output file are the same;"
|
2022-02-01 12:18:23 +00:00
|
|
|
" use --replace-input to intentionally"
|
|
|
|
" overwrite the input file");
|
2022-01-23 18:11:46 +00:00
|
|
|
}
|
2022-05-07 17:33:45 +00:00
|
|
|
|
2022-07-31 00:53:30 +00:00
|
|
|
if (m->json_version == 1) {
|
|
|
|
if (m->json_keys.count("qpdf")) {
|
|
|
|
usage("json key \"qpdf\" is only valid for json version > 1");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (m->json_keys.count("objectinfo") || m->json_keys.count("objects")) {
|
|
|
|
usage("json keys \"objects\" and \"objectinfo\" are only valid for"
|
|
|
|
" json version 1");
|
|
|
|
}
|
2022-05-07 17:33:45 +00:00
|
|
|
}
|
2022-01-23 18:11:46 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 21:50:18 +00:00
|
|
|
unsigned long
|
|
|
|
QPDFJob::getEncryptionStatus()
|
|
|
|
{
|
|
|
|
return this->m->encryption_status;
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 21:13:43 +00:00
|
|
|
void
|
|
|
|
QPDFJob::setQPDFOptions(QPDF& pdf)
|
2022-01-03 15:16:28 +00:00
|
|
|
{
|
2022-06-05 17:30:42 +00:00
|
|
|
pdf.setLogger(this->m->log);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->ignore_xref_streams) {
|
2022-01-03 15:16:28 +00:00
|
|
|
pdf.setIgnoreXRefStreams(true);
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->suppress_recovery) {
|
2022-01-03 15:16:28 +00:00
|
|
|
pdf.setAttemptRecovery(false);
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->password_is_hex_key) {
|
2022-01-03 15:16:28 +00:00
|
|
|
pdf.setPasswordIsHexKey(true);
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->suppress_warnings) {
|
2022-01-03 15:16:28 +00:00
|
|
|
pdf.setSuppressWarnings(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
static std::string
|
|
|
|
show_bool(bool v)
|
2022-01-03 15:16:28 +00:00
|
|
|
{
|
|
|
|
return v ? "allowed" : "not allowed";
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
static std::string
|
|
|
|
show_encryption_method(QPDF::encryption_method_e method)
|
2022-01-03 15:16:28 +00:00
|
|
|
{
|
|
|
|
std::string result = "unknown";
|
2022-04-02 21:14:10 +00:00
|
|
|
switch (method) {
|
|
|
|
case QPDF::e_none:
|
2022-01-03 15:16:28 +00:00
|
|
|
result = "none";
|
|
|
|
break;
|
2022-04-02 21:14:10 +00:00
|
|
|
case QPDF::e_unknown:
|
2022-01-03 15:16:28 +00:00
|
|
|
result = "unknown";
|
|
|
|
break;
|
2022-04-02 21:14:10 +00:00
|
|
|
case QPDF::e_rc4:
|
2022-01-03 15:16:28 +00:00
|
|
|
result = "RC4";
|
|
|
|
break;
|
2022-04-02 21:14:10 +00:00
|
|
|
case QPDF::e_aes:
|
2022-01-03 15:16:28 +00:00
|
|
|
result = "AESv2";
|
|
|
|
break;
|
2022-04-02 21:14:10 +00:00
|
|
|
case QPDF::e_aesv3:
|
2022-01-03 15:16:28 +00:00
|
|
|
result = "AESv3";
|
|
|
|
break;
|
|
|
|
// no default so gcc will warn for missing case
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-01-03 17:16:16 +00:00
|
|
|
void
|
|
|
|
QPDFJob::showEncryption(QPDF& pdf)
|
2022-01-03 15:16:28 +00:00
|
|
|
{
|
|
|
|
// Extract /P from /Encrypt
|
|
|
|
int R = 0;
|
|
|
|
int P = 0;
|
|
|
|
int V = 0;
|
|
|
|
QPDF::encryption_method_e stream_method = QPDF::e_unknown;
|
|
|
|
QPDF::encryption_method_e string_method = QPDF::e_unknown;
|
|
|
|
QPDF::encryption_method_e file_method = QPDF::e_unknown;
|
2022-06-05 17:30:42 +00:00
|
|
|
auto& cout = *this->m->log->getInfo();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!pdf.isEncrypted(R, P, V, stream_method, string_method, file_method)) {
|
2022-06-05 17:30:42 +00:00
|
|
|
cout << "File is not encrypted\n";
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-06-05 17:30:42 +00:00
|
|
|
cout << "R = " << R << "\n";
|
|
|
|
cout << "P = " << P << "\n";
|
2022-01-03 15:16:28 +00:00
|
|
|
std::string user_password = pdf.getTrimmedUserPassword();
|
|
|
|
std::string encryption_key = pdf.getEncryptionKey();
|
2022-06-05 17:30:42 +00:00
|
|
|
cout << "User password = " << user_password << "\n";
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->show_encryption_key) {
|
|
|
|
cout << "Encryption key = " << QUtil::hex_encode(encryption_key)
|
2022-06-05 17:30:42 +00:00
|
|
|
<< "\n";
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (pdf.ownerPasswordMatched()) {
|
2022-06-05 17:30:42 +00:00
|
|
|
cout << "Supplied password is owner password\n";
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (pdf.userPasswordMatched()) {
|
2022-06-05 17:30:42 +00:00
|
|
|
cout << "Supplied password is user password\n";
|
2022-01-05 20:01:35 +00:00
|
|
|
}
|
|
|
|
cout << "extract for accessibility: "
|
2022-06-05 17:30:42 +00:00
|
|
|
<< show_bool(pdf.allowAccessibility()) << "\n"
|
2022-04-02 21:14:10 +00:00
|
|
|
<< "extract for any purpose: " << show_bool(pdf.allowExtractAll())
|
2022-06-05 17:30:42 +00:00
|
|
|
<< "\n"
|
2022-04-02 21:14:10 +00:00
|
|
|
<< "print low resolution: " << show_bool(pdf.allowPrintLowRes())
|
2022-06-05 17:30:42 +00:00
|
|
|
<< "\n"
|
2022-04-02 21:14:10 +00:00
|
|
|
<< "print high resolution: " << show_bool(pdf.allowPrintHighRes())
|
2022-06-05 17:30:42 +00:00
|
|
|
<< "\n"
|
2022-01-05 20:01:35 +00:00
|
|
|
<< "modify document assembly: "
|
2022-06-05 17:30:42 +00:00
|
|
|
<< show_bool(pdf.allowModifyAssembly()) << "\n"
|
|
|
|
<< "modify forms: " << show_bool(pdf.allowModifyForm()) << "\n"
|
2022-04-02 21:14:10 +00:00
|
|
|
<< "modify annotations: " << show_bool(pdf.allowModifyAnnotation())
|
2022-06-05 17:30:42 +00:00
|
|
|
<< "\n"
|
|
|
|
<< "modify other: " << show_bool(pdf.allowModifyOther()) << "\n"
|
|
|
|
<< "modify anything: " << show_bool(pdf.allowModifyAll()) << "\n";
|
2022-04-02 21:14:10 +00:00
|
|
|
if (V >= 4) {
|
2022-01-05 20:01:35 +00:00
|
|
|
cout << "stream encryption method: "
|
2022-06-05 17:30:42 +00:00
|
|
|
<< show_encryption_method(stream_method) << "\n"
|
2022-01-05 20:01:35 +00:00
|
|
|
<< "string encryption method: "
|
2022-06-05 17:30:42 +00:00
|
|
|
<< show_encryption_method(string_method) << "\n"
|
2022-01-05 20:01:35 +00:00
|
|
|
<< "file encryption method: "
|
2022-06-05 17:30:42 +00:00
|
|
|
<< show_encryption_method(file_method) << "\n";
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 17:16:16 +00:00
|
|
|
void
|
|
|
|
QPDFJob::doCheck(QPDF& pdf)
|
2022-01-03 15:16:28 +00:00
|
|
|
{
|
|
|
|
// Code below may set okay to false but not to true.
|
|
|
|
// We assume okay until we prove otherwise but may
|
|
|
|
// continue to perform additional checks after finding
|
|
|
|
// errors.
|
|
|
|
bool okay = true;
|
|
|
|
bool warnings = false;
|
2022-06-05 17:30:42 +00:00
|
|
|
auto& cout = *this->m->log->getInfo();
|
|
|
|
cout << "checking " << m->infilename.get() << "\n";
|
2022-04-02 21:14:10 +00:00
|
|
|
try {
|
2022-01-03 15:16:28 +00:00
|
|
|
int extension_level = pdf.getExtensionLevel();
|
2022-01-05 20:01:35 +00:00
|
|
|
cout << "PDF Version: " << pdf.getPDFVersion();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (extension_level > 0) {
|
2022-01-05 20:01:35 +00:00
|
|
|
cout << " extension level " << pdf.getExtensionLevel();
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-06-05 17:30:42 +00:00
|
|
|
cout << "\n";
|
2022-01-03 17:16:16 +00:00
|
|
|
showEncryption(pdf);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (pdf.isLinearized()) {
|
2022-01-05 20:01:35 +00:00
|
|
|
cout << "File is linearized\n";
|
2022-01-03 15:16:28 +00:00
|
|
|
// any errors or warnings are reported by
|
|
|
|
// checkLinearization(). We treat all issues reported here
|
|
|
|
// as warnings.
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!pdf.checkLinearization()) {
|
2022-01-03 15:16:28 +00:00
|
|
|
warnings = true;
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-01-05 20:01:35 +00:00
|
|
|
cout << "File is not linearized\n";
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
|
2022-01-06 20:33:29 +00:00
|
|
|
// Write the file to nowhere, uncompressing
|
2022-01-03 15:16:28 +00:00
|
|
|
// streams. This causes full file traversal and
|
|
|
|
// decoding of all streams we can decode.
|
|
|
|
QPDFWriter w(pdf);
|
|
|
|
Pl_Discard discard;
|
|
|
|
w.setOutputPipeline(&discard);
|
|
|
|
w.setDecodeLevel(qpdf_dl_all);
|
|
|
|
w.write();
|
|
|
|
|
|
|
|
// Parse all content streams
|
|
|
|
DiscardContents discard_contents;
|
|
|
|
int pageno = 0;
|
2022-05-21 14:18:15 +00:00
|
|
|
for (auto& page: QPDFPageDocumentHelper(pdf).getAllPages()) {
|
2022-01-03 15:16:28 +00:00
|
|
|
++pageno;
|
2022-04-02 21:14:10 +00:00
|
|
|
try {
|
2022-01-03 15:16:28 +00:00
|
|
|
page.parseContents(&discard_contents);
|
2022-04-02 21:14:10 +00:00
|
|
|
} catch (QPDFExc& e) {
|
2022-01-03 15:16:28 +00:00
|
|
|
okay = false;
|
2022-06-05 17:30:42 +00:00
|
|
|
*this->m->log->getError()
|
|
|
|
<< "ERROR: page " << pageno << ": " << e.what() << "\n";
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} catch (std::exception& e) {
|
2022-06-05 17:30:42 +00:00
|
|
|
*this->m->log->getError() << "ERROR: " << e.what() << "\n";
|
2022-01-03 15:16:28 +00:00
|
|
|
okay = false;
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!okay) {
|
2022-01-03 17:16:16 +00:00
|
|
|
throw std::runtime_error("errors detected");
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if ((!pdf.getWarnings().empty()) || warnings) {
|
2022-01-23 18:13:18 +00:00
|
|
|
this->m->warnings = true;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-06-05 17:30:42 +00:00
|
|
|
*this->m->log->getInfo() << "No syntax or stream encoding errors"
|
|
|
|
<< " found; the file may still contain\n"
|
|
|
|
<< "errors that qpdf cannot detect\n";
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-05 20:01:35 +00:00
|
|
|
void
|
|
|
|
QPDFJob::doShowObj(QPDF& pdf)
|
2022-01-03 15:16:28 +00:00
|
|
|
{
|
|
|
|
QPDFObjectHandle obj;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->show_trailer) {
|
2022-01-03 15:16:28 +00:00
|
|
|
obj = pdf.getTrailer();
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-01-26 19:56:24 +00:00
|
|
|
obj = pdf.getObjectByID(m->show_obj, m->show_gen);
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-01-03 17:16:16 +00:00
|
|
|
bool error = false;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (obj.isStream()) {
|
|
|
|
if (m->show_raw_stream_data || m->show_filtered_stream_data) {
|
2022-01-26 19:56:24 +00:00
|
|
|
bool filter = m->show_filtered_stream_data;
|
2022-07-26 11:37:50 +00:00
|
|
|
if (filter && (!obj.pipeStreamData(nullptr, 0, qpdf_dl_all))) {
|
2022-02-01 12:49:00 +00:00
|
|
|
QTC::TC("qpdf", "QPDFJob unable to filter");
|
2022-01-03 17:16:16 +00:00
|
|
|
obj.warnIfPossible("unable to filter stream data");
|
|
|
|
error = true;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-06-18 13:40:41 +00:00
|
|
|
// If anything has been written to standard output,
|
|
|
|
// this will fail.
|
2022-06-18 17:38:36 +00:00
|
|
|
this->m->log->saveToStandardOutput(true);
|
2022-01-03 15:16:28 +00:00
|
|
|
obj.pipeStreamData(
|
2022-06-18 13:40:41 +00:00
|
|
|
this->m->log->getSave().get(),
|
2022-01-26 19:56:24 +00:00
|
|
|
(filter && m->normalize) ? qpdf_ef_normalize : 0,
|
2022-01-03 15:16:28 +00:00
|
|
|
filter ? qpdf_dl_all : qpdf_dl_none);
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-06-05 17:30:42 +00:00
|
|
|
*this->m->log->getInfo() << "Object is stream. Dictionary:\n"
|
|
|
|
<< obj.getDict().unparseResolved() << "\n";
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-06-05 17:30:42 +00:00
|
|
|
*this->m->log->getInfo() << obj.unparseResolved() << "\n";
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (error) {
|
2022-01-03 17:16:16 +00:00
|
|
|
throw std::runtime_error(
|
2022-07-16 10:21:11 +00:00
|
|
|
"unable to get object " + obj.getObjGen().unparse(','));
|
2022-01-03 17:16:16 +00:00
|
|
|
}
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 20:01:35 +00:00
|
|
|
void
|
|
|
|
QPDFJob::doShowPages(QPDF& pdf)
|
2022-01-03 15:16:28 +00:00
|
|
|
{
|
|
|
|
int pageno = 0;
|
2022-06-05 17:30:42 +00:00
|
|
|
auto& cout = *this->m->log->getInfo();
|
2022-05-21 14:18:15 +00:00
|
|
|
for (auto& ph: QPDFPageDocumentHelper(pdf).getAllPages()) {
|
2022-01-03 15:16:28 +00:00
|
|
|
QPDFObjectHandle page = ph.getObjectHandle();
|
|
|
|
++pageno;
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
cout << "page " << pageno << ": " << page.getObjectID() << " "
|
2022-06-05 17:30:42 +00:00
|
|
|
<< page.getGeneration() << " R\n";
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->show_page_images) {
|
2022-01-03 15:16:28 +00:00
|
|
|
std::map<std::string, QPDFObjectHandle> images = ph.getImages();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!images.empty()) {
|
2022-06-05 17:30:42 +00:00
|
|
|
cout << " images:\n";
|
2022-04-30 13:43:07 +00:00
|
|
|
for (auto const& iter2: images) {
|
2022-01-03 15:16:28 +00:00
|
|
|
std::string const& name = iter2.first;
|
|
|
|
QPDFObjectHandle image = iter2.second;
|
|
|
|
QPDFObjectHandle dict = image.getDict();
|
2022-04-02 21:14:10 +00:00
|
|
|
int width = dict.getKey("/Width").getIntValueAsInt();
|
|
|
|
int height = dict.getKey("/Height").getIntValueAsInt();
|
|
|
|
cout << " " << name << ": " << image.unparse() << ", "
|
2022-06-05 17:30:42 +00:00
|
|
|
<< width << " x " << height << "\n";
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-05 17:30:42 +00:00
|
|
|
cout << " content:\n";
|
2022-05-21 14:18:15 +00:00
|
|
|
for (auto& iter2: ph.getPageContents()) {
|
2022-06-05 17:30:42 +00:00
|
|
|
cout << " " << iter2.unparse() << "\n";
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-05 20:01:35 +00:00
|
|
|
void
|
|
|
|
QPDFJob::doListAttachments(QPDF& pdf)
|
2022-01-03 15:16:28 +00:00
|
|
|
{
|
|
|
|
QPDFEmbeddedFileDocumentHelper efdh(pdf);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (efdh.hasEmbeddedFiles()) {
|
2022-04-30 13:43:07 +00:00
|
|
|
for (auto const& i: efdh.getEmbeddedFiles()) {
|
2022-01-03 15:16:28 +00:00
|
|
|
std::string const& key = i.first;
|
|
|
|
auto efoh = i.second;
|
2022-06-05 17:30:42 +00:00
|
|
|
*this->m->log->getInfo()
|
|
|
|
<< key << " -> "
|
2022-07-16 10:21:11 +00:00
|
|
|
<< efoh->getEmbeddedFileStream().getObjGen().unparse(',')
|
|
|
|
<< "\n";
|
2022-06-05 17:30:42 +00:00
|
|
|
doIfVerbose([&](Pipeline& v, std::string const& prefix) {
|
2022-01-03 15:16:28 +00:00
|
|
|
auto desc = efoh->getDescription();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!desc.empty()) {
|
2022-06-05 17:30:42 +00:00
|
|
|
v << " description: " << desc << "\n";
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-06-05 17:30:42 +00:00
|
|
|
v << " preferred name: " << efoh->getFilename() << "\n";
|
|
|
|
v << " all names:\n";
|
2022-04-30 13:43:07 +00:00
|
|
|
for (auto const& i2: efoh->getFilenames()) {
|
2022-06-05 17:30:42 +00:00
|
|
|
v << " " << i2.first << " -> " << i2.second << "\n";
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-06-05 17:30:42 +00:00
|
|
|
v << " all data streams:\n";
|
2022-04-30 13:43:07 +00:00
|
|
|
for (auto i2: efoh->getEmbeddedFileStreams().ditems()) {
|
2022-05-30 13:23:48 +00:00
|
|
|
auto efs = QPDFEFStreamObjectHelper(i2.second);
|
2022-06-05 17:30:42 +00:00
|
|
|
v << " " << i2.first << " -> "
|
2022-07-16 10:21:11 +00:00
|
|
|
<< efs.getObjectHandle().getObjGen().unparse(',') << "\n";
|
2022-06-05 17:30:42 +00:00
|
|
|
v << " creation date: " << efs.getCreationDate()
|
|
|
|
<< "\n"
|
|
|
|
<< " modification date: " << efs.getModDate() << "\n"
|
|
|
|
<< " mime type: " << efs.getSubtype() << "\n"
|
|
|
|
<< " checksum: "
|
|
|
|
<< QUtil::hex_encode(efs.getChecksum()) << "\n";
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-01-03 17:16:16 +00:00
|
|
|
});
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-06-05 17:30:42 +00:00
|
|
|
*this->m->log->getInfo()
|
|
|
|
<< m->infilename.get() << " has no embedded files\n";
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-05 21:13:43 +00:00
|
|
|
void
|
|
|
|
QPDFJob::doShowAttachment(QPDF& pdf)
|
2022-01-03 15:16:28 +00:00
|
|
|
{
|
|
|
|
QPDFEmbeddedFileDocumentHelper efdh(pdf);
|
2022-01-26 19:56:24 +00:00
|
|
|
auto fs = efdh.getEmbeddedFile(m->attachment_to_show);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!fs) {
|
2022-01-03 17:16:16 +00:00
|
|
|
throw std::runtime_error(
|
2022-01-26 19:56:24 +00:00
|
|
|
"attachment " + m->attachment_to_show + " not found");
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
auto efs = fs->getEmbeddedFileStream();
|
2022-06-18 13:40:41 +00:00
|
|
|
// saveToStandardOutput has already been called, but it's harmless
|
|
|
|
// to call it again, so do as defensive coding.
|
2022-06-18 17:38:36 +00:00
|
|
|
this->m->log->saveToStandardOutput(true);
|
2022-06-18 13:40:41 +00:00
|
|
|
efs.pipeStreamData(this->m->log->getSave().get(), 0, qpdf_dl_all);
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-04-02 21:14:10 +00:00
|
|
|
QPDFJob::parse_object_id(
|
|
|
|
std::string const& objspec, bool& trailer, int& obj, int& gen)
|
2022-01-03 15:16:28 +00:00
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (objspec == "trailer") {
|
2022-01-03 15:16:28 +00:00
|
|
|
trailer = true;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-01-03 15:16:28 +00:00
|
|
|
trailer = false;
|
|
|
|
obj = QUtil::string_to_int(objspec.c_str());
|
|
|
|
size_t comma = objspec.find(',');
|
2022-04-02 21:14:10 +00:00
|
|
|
if ((comma != std::string::npos) && (comma + 1 < objspec.length())) {
|
2022-01-03 15:16:28 +00:00
|
|
|
gen = QUtil::string_to_int(
|
|
|
|
objspec.substr(1 + comma, std::string::npos).c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-05 21:13:43 +00:00
|
|
|
std::set<QPDFObjGen>
|
|
|
|
QPDFJob::getWantedJSONObjects()
|
2022-01-03 15:16:28 +00:00
|
|
|
{
|
|
|
|
std::set<QPDFObjGen> wanted_og;
|
2022-04-30 13:43:07 +00:00
|
|
|
for (auto const& iter: m->json_objects) {
|
2022-01-03 15:16:28 +00:00
|
|
|
bool trailer;
|
|
|
|
int obj = 0;
|
|
|
|
int gen = 0;
|
2022-01-26 14:09:29 +00:00
|
|
|
parse_object_id(iter, trailer, obj, gen);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (obj) {
|
2022-01-03 15:16:28 +00:00
|
|
|
wanted_og.insert(QPDFObjGen(obj, gen));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return wanted_og;
|
|
|
|
}
|
|
|
|
|
2022-05-18 22:22:57 +00:00
|
|
|
void
|
|
|
|
QPDFJob::doJSONObject(
|
|
|
|
Pipeline* p, bool& first, std::string const& key, QPDFObjectHandle& obj)
|
|
|
|
{
|
|
|
|
if (this->m->json_version == 1) {
|
2022-07-30 17:32:59 +00:00
|
|
|
JSON::writeDictionaryItem(p, first, key, obj.getJSON(1, true), 2);
|
2022-05-18 22:22:57 +00:00
|
|
|
} else {
|
|
|
|
auto j = JSON::makeDictionary();
|
|
|
|
if (obj.isStream()) {
|
|
|
|
j.addDictionaryMember("stream", JSON::makeDictionary())
|
|
|
|
.addDictionaryMember(
|
|
|
|
"dict", obj.getDict().getJSON(this->m->json_version, true));
|
|
|
|
} else {
|
|
|
|
j.addDictionaryMember(
|
|
|
|
"value", obj.getJSON(this->m->json_version, true));
|
|
|
|
}
|
2022-07-30 17:32:59 +00:00
|
|
|
JSON::writeDictionaryItem(p, first, key, j, 2);
|
2022-05-18 22:22:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-05 21:13:43 +00:00
|
|
|
void
|
Top-level json: write incrementally
This commit just changes the order in which fields are written to the
json without changing their content. All the json files in the test
suite were modified with this script to ensure that we didn't get any
changes other than ordering.
----------
#!/usr/bin/env python3
import json
import sys
def json_dumps(data):
return json.dumps(data, ensure_ascii=False,
indent=2, separators=(',', ': '))
for filename in sys.argv[1:]:
with open(filename, 'r') as f:
data = json.loads(f.read())
newdata = {}
for i in ('version', 'parameters', 'pages', 'pagelabels',
'acroform', 'attachments', 'encrypt', 'outlines',
'objects', 'objectinfo'):
if i in data:
newdata[i] = data[i]
print(json_dumps(newdata))
----------
2022-05-06 22:25:59 +00:00
|
|
|
QPDFJob::doJSONObjects(Pipeline* p, bool& first, QPDF& pdf)
|
2022-01-03 15:16:28 +00:00
|
|
|
{
|
2022-07-31 14:34:05 +00:00
|
|
|
if (m->json_version == 1) {
|
|
|
|
JSON::writeDictionaryKey(p, first, "objects", 1);
|
|
|
|
bool first_object = true;
|
|
|
|
JSON::writeDictionaryOpen(p, first_object, 1);
|
|
|
|
bool all_objects = m->json_objects.empty();
|
|
|
|
std::set<QPDFObjGen> wanted_og = getWantedJSONObjects();
|
|
|
|
for (auto& obj: pdf.getAllObjects()) {
|
|
|
|
std::string key = obj.unparse();
|
|
|
|
if (this->m->json_version > 1) {
|
|
|
|
key = "obj:" + key;
|
|
|
|
}
|
|
|
|
if (all_objects || wanted_og.count(obj.getObjGen())) {
|
|
|
|
doJSONObject(p, first_object, key, obj);
|
|
|
|
}
|
2022-05-18 22:22:57 +00:00
|
|
|
}
|
2022-07-31 14:34:05 +00:00
|
|
|
if (all_objects || m->json_objects.count("trailer")) {
|
|
|
|
auto trailer = pdf.getTrailer();
|
|
|
|
doJSONObject(p, first_object, "trailer", trailer);
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-07-31 14:34:05 +00:00
|
|
|
JSON::writeDictionaryClose(p, first_object, 1);
|
|
|
|
} else {
|
|
|
|
std::set<std::string> json_objects;
|
|
|
|
if (this->m->json_objects.count("trailer")) {
|
|
|
|
json_objects.insert("trailer");
|
|
|
|
}
|
|
|
|
auto wanted = getWantedJSONObjects();
|
|
|
|
for (auto const& og: wanted) {
|
|
|
|
std::ostringstream s;
|
|
|
|
s << "obj:" << og.unparse(' ') << " R";
|
|
|
|
json_objects.insert(s.str());
|
|
|
|
}
|
|
|
|
pdf.writeJSON(
|
|
|
|
this->m->json_version,
|
|
|
|
p,
|
|
|
|
false,
|
|
|
|
first,
|
|
|
|
this->m->decode_level,
|
|
|
|
this->m->json_stream_data,
|
|
|
|
this->m->json_stream_prefix,
|
|
|
|
json_objects);
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-05 21:13:43 +00:00
|
|
|
void
|
Top-level json: write incrementally
This commit just changes the order in which fields are written to the
json without changing their content. All the json files in the test
suite were modified with this script to ensure that we didn't get any
changes other than ordering.
----------
#!/usr/bin/env python3
import json
import sys
def json_dumps(data):
return json.dumps(data, ensure_ascii=False,
indent=2, separators=(',', ': '))
for filename in sys.argv[1:]:
with open(filename, 'r') as f:
data = json.loads(f.read())
newdata = {}
for i in ('version', 'parameters', 'pages', 'pagelabels',
'acroform', 'attachments', 'encrypt', 'outlines',
'objects', 'objectinfo'):
if i in data:
newdata[i] = data[i]
print(json_dumps(newdata))
----------
2022-05-06 22:25:59 +00:00
|
|
|
QPDFJob::doJSONObjectinfo(Pipeline* p, bool& first, QPDF& pdf)
|
2022-01-03 15:16:28 +00:00
|
|
|
{
|
2022-07-30 17:32:59 +00:00
|
|
|
JSON::writeDictionaryKey(p, first, "objectinfo", 1);
|
Objectinfo json: write incrementally and in numeric order
This script was used on test data:
----------
#!/usr/bin/env python3
import json
import sys
import re
def json_dumps(data):
return json.dumps(data, ensure_ascii=False,
indent=2, separators=(',', ': '))
for filename in sys.argv[1:]:
with open(filename, 'r') as f:
data = json.loads(f.read())
if 'objectinfo' not in data:
continue
trailer = None
to_sort = []
for k, v in data['objectinfo'].items():
if k == 'trailer':
trailer = v
else:
m = re.match(r'^(\d+) \d+ R', k)
if m:
to_sort.append([int(m.group(1)), k, v])
newobjectinfo = {x[1]: x[2] for x in sorted(to_sort)}
if trailer is not None:
newobjectinfo['trailer'] = trailer
data['objectinfo'] = newobjectinfo
print(json_dumps(data))
----------
2022-05-06 23:08:31 +00:00
|
|
|
bool first_object = true;
|
|
|
|
JSON::writeDictionaryOpen(p, first_object, 1);
|
2022-01-26 19:56:24 +00:00
|
|
|
bool all_objects = m->json_objects.empty();
|
2022-01-05 21:13:43 +00:00
|
|
|
std::set<QPDFObjGen> wanted_og = getWantedJSONObjects();
|
2022-04-30 13:43:07 +00:00
|
|
|
for (auto& obj: pdf.getAllObjects()) {
|
2022-04-02 21:14:10 +00:00
|
|
|
if (all_objects || wanted_og.count(obj.getObjGen())) {
|
Objectinfo json: write incrementally and in numeric order
This script was used on test data:
----------
#!/usr/bin/env python3
import json
import sys
import re
def json_dumps(data):
return json.dumps(data, ensure_ascii=False,
indent=2, separators=(',', ': '))
for filename in sys.argv[1:]:
with open(filename, 'r') as f:
data = json.loads(f.read())
if 'objectinfo' not in data:
continue
trailer = None
to_sort = []
for k, v in data['objectinfo'].items():
if k == 'trailer':
trailer = v
else:
m = re.match(r'^(\d+) \d+ R', k)
if m:
to_sort.append([int(m.group(1)), k, v])
newobjectinfo = {x[1]: x[2] for x in sorted(to_sort)}
if trailer is not None:
newobjectinfo['trailer'] = trailer
data['objectinfo'] = newobjectinfo
print(json_dumps(data))
----------
2022-05-06 23:08:31 +00:00
|
|
|
auto j_details = JSON::makeDictionary();
|
2022-04-02 21:14:10 +00:00
|
|
|
auto j_stream =
|
|
|
|
j_details.addDictionaryMember("stream", JSON::makeDictionary());
|
2022-01-03 15:16:28 +00:00
|
|
|
bool is_stream = obj.isStream();
|
2022-04-02 21:14:10 +00:00
|
|
|
j_stream.addDictionaryMember("is", JSON::makeBool(is_stream));
|
2022-01-03 15:16:28 +00:00
|
|
|
j_stream.addDictionaryMember(
|
|
|
|
"length",
|
2022-05-07 11:53:45 +00:00
|
|
|
(is_stream ? obj.getDict().getKey("/Length").getJSON(
|
|
|
|
this->m->json_version, true)
|
2022-04-02 21:14:10 +00:00
|
|
|
: JSON::makeNull()));
|
2022-01-03 15:16:28 +00:00
|
|
|
j_stream.addDictionaryMember(
|
|
|
|
"filter",
|
2022-05-07 11:53:45 +00:00
|
|
|
(is_stream ? obj.getDict().getKey("/Filter").getJSON(
|
|
|
|
this->m->json_version, true)
|
2022-04-02 21:14:10 +00:00
|
|
|
: JSON::makeNull()));
|
Objectinfo json: write incrementally and in numeric order
This script was used on test data:
----------
#!/usr/bin/env python3
import json
import sys
import re
def json_dumps(data):
return json.dumps(data, ensure_ascii=False,
indent=2, separators=(',', ': '))
for filename in sys.argv[1:]:
with open(filename, 'r') as f:
data = json.loads(f.read())
if 'objectinfo' not in data:
continue
trailer = None
to_sort = []
for k, v in data['objectinfo'].items():
if k == 'trailer':
trailer = v
else:
m = re.match(r'^(\d+) \d+ R', k)
if m:
to_sort.append([int(m.group(1)), k, v])
newobjectinfo = {x[1]: x[2] for x in sorted(to_sort)}
if trailer is not None:
newobjectinfo['trailer'] = trailer
data['objectinfo'] = newobjectinfo
print(json_dumps(data))
----------
2022-05-06 23:08:31 +00:00
|
|
|
JSON::writeDictionaryItem(
|
2022-07-30 17:32:59 +00:00
|
|
|
p, first_object, obj.unparse(), j_details, 2);
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
}
|
Objectinfo json: write incrementally and in numeric order
This script was used on test data:
----------
#!/usr/bin/env python3
import json
import sys
import re
def json_dumps(data):
return json.dumps(data, ensure_ascii=False,
indent=2, separators=(',', ': '))
for filename in sys.argv[1:]:
with open(filename, 'r') as f:
data = json.loads(f.read())
if 'objectinfo' not in data:
continue
trailer = None
to_sort = []
for k, v in data['objectinfo'].items():
if k == 'trailer':
trailer = v
else:
m = re.match(r'^(\d+) \d+ R', k)
if m:
to_sort.append([int(m.group(1)), k, v])
newobjectinfo = {x[1]: x[2] for x in sorted(to_sort)}
if trailer is not None:
newobjectinfo['trailer'] = trailer
data['objectinfo'] = newobjectinfo
print(json_dumps(data))
----------
2022-05-06 23:08:31 +00:00
|
|
|
JSON::writeDictionaryClose(p, first_object, 1);
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 21:13:43 +00:00
|
|
|
void
|
Top-level json: write incrementally
This commit just changes the order in which fields are written to the
json without changing their content. All the json files in the test
suite were modified with this script to ensure that we didn't get any
changes other than ordering.
----------
#!/usr/bin/env python3
import json
import sys
def json_dumps(data):
return json.dumps(data, ensure_ascii=False,
indent=2, separators=(',', ': '))
for filename in sys.argv[1:]:
with open(filename, 'r') as f:
data = json.loads(f.read())
newdata = {}
for i in ('version', 'parameters', 'pages', 'pagelabels',
'acroform', 'attachments', 'encrypt', 'outlines',
'objects', 'objectinfo'):
if i in data:
newdata[i] = data[i]
print(json_dumps(newdata))
----------
2022-05-06 22:25:59 +00:00
|
|
|
QPDFJob::doJSONPages(Pipeline* p, bool& first, QPDF& pdf)
|
2022-01-03 15:16:28 +00:00
|
|
|
{
|
2022-07-30 17:32:59 +00:00
|
|
|
JSON::writeDictionaryKey(p, first, "pages", 1);
|
2022-05-06 22:46:38 +00:00
|
|
|
bool first_page = true;
|
2022-07-30 17:32:59 +00:00
|
|
|
JSON::writeArrayOpen(p, first_page, 2);
|
2022-01-03 15:16:28 +00:00
|
|
|
QPDFPageLabelDocumentHelper pldh(pdf);
|
|
|
|
QPDFOutlineDocumentHelper odh(pdf);
|
2022-04-30 17:23:18 +00:00
|
|
|
int pageno = -1;
|
2022-05-21 14:18:15 +00:00
|
|
|
for (auto& ph: QPDFPageDocumentHelper(pdf).getAllPages()) {
|
2022-04-30 17:23:18 +00:00
|
|
|
++pageno;
|
2022-05-06 22:46:38 +00:00
|
|
|
JSON j_page = JSON::makeDictionary();
|
2022-01-03 15:16:28 +00:00
|
|
|
QPDFObjectHandle page = ph.getObjectHandle();
|
2022-05-07 11:53:45 +00:00
|
|
|
j_page.addDictionaryMember(
|
|
|
|
"object", page.getJSON(this->m->json_version));
|
2022-04-02 21:14:10 +00:00
|
|
|
JSON j_images = j_page.addDictionaryMember("images", JSON::makeArray());
|
2022-05-21 14:18:15 +00:00
|
|
|
for (auto const& iter2: ph.getImages()) {
|
2022-01-03 15:16:28 +00:00
|
|
|
JSON j_image = j_images.addArrayElement(JSON::makeDictionary());
|
2022-04-02 21:14:10 +00:00
|
|
|
j_image.addDictionaryMember("name", JSON::makeString(iter2.first));
|
2022-01-03 15:16:28 +00:00
|
|
|
QPDFObjectHandle image = iter2.second;
|
|
|
|
QPDFObjectHandle dict = image.getDict();
|
|
|
|
j_image.addDictionaryMember(
|
2022-05-07 11:53:45 +00:00
|
|
|
"object", image.getJSON(this->m->json_version));
|
|
|
|
j_image.addDictionaryMember(
|
|
|
|
"width", dict.getKey("/Width").getJSON(this->m->json_version));
|
2022-01-03 15:16:28 +00:00
|
|
|
j_image.addDictionaryMember(
|
2022-05-07 11:53:45 +00:00
|
|
|
"height",
|
|
|
|
dict.getKey("/Height").getJSON(this->m->json_version));
|
2022-01-03 15:16:28 +00:00
|
|
|
j_image.addDictionaryMember(
|
2022-05-07 11:53:45 +00:00
|
|
|
"colorspace",
|
|
|
|
dict.getKey("/ColorSpace").getJSON(this->m->json_version));
|
2022-01-03 15:16:28 +00:00
|
|
|
j_image.addDictionaryMember(
|
2022-05-07 11:53:45 +00:00
|
|
|
"bitspercomponent",
|
|
|
|
dict.getKey("/BitsPerComponent")
|
|
|
|
.getJSON(this->m->json_version));
|
2022-01-03 15:16:28 +00:00
|
|
|
QPDFObjectHandle filters = dict.getKey("/Filter").wrapInArray();
|
2022-05-07 11:53:45 +00:00
|
|
|
j_image.addDictionaryMember(
|
|
|
|
"filter", filters.getJSON(this->m->json_version));
|
2022-01-03 15:16:28 +00:00
|
|
|
QPDFObjectHandle decode_parms = dict.getKey("/DecodeParms");
|
|
|
|
QPDFObjectHandle dp_array;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (decode_parms.isArray()) {
|
2022-01-03 15:16:28 +00:00
|
|
|
dp_array = decode_parms;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-01-03 15:16:28 +00:00
|
|
|
dp_array = QPDFObjectHandle::newArray();
|
2022-04-02 21:14:10 +00:00
|
|
|
for (int i = 0; i < filters.getArrayNItems(); ++i) {
|
2022-01-03 15:16:28 +00:00
|
|
|
dp_array.appendItem(decode_parms);
|
|
|
|
}
|
|
|
|
}
|
2022-05-07 11:53:45 +00:00
|
|
|
j_image.addDictionaryMember(
|
|
|
|
"decodeparms", dp_array.getJSON(this->m->json_version));
|
2022-01-03 15:16:28 +00:00
|
|
|
j_image.addDictionaryMember(
|
|
|
|
"filterable",
|
|
|
|
JSON::makeBool(
|
2022-07-26 11:37:50 +00:00
|
|
|
image.pipeStreamData(nullptr, 0, m->decode_level, true)));
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
j_page.addDictionaryMember("images", j_images);
|
2022-04-02 21:14:10 +00:00
|
|
|
JSON j_contents =
|
|
|
|
j_page.addDictionaryMember("contents", JSON::makeArray());
|
2022-05-21 14:18:15 +00:00
|
|
|
for (auto& iter2: ph.getPageContents()) {
|
2022-05-07 11:53:45 +00:00
|
|
|
j_contents.addArrayElement(iter2.getJSON(this->m->json_version));
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
j_page.addDictionaryMember(
|
2022-05-07 11:53:45 +00:00
|
|
|
"label",
|
|
|
|
pldh.getLabelForPage(pageno).getJSON(this->m->json_version));
|
2022-04-02 21:14:10 +00:00
|
|
|
JSON j_outlines =
|
|
|
|
j_page.addDictionaryMember("outlines", JSON::makeArray());
|
2022-01-03 15:16:28 +00:00
|
|
|
std::vector<QPDFOutlineObjectHelper> outlines =
|
|
|
|
odh.getOutlinesForPage(page.getObjGen());
|
2022-04-30 17:23:18 +00:00
|
|
|
for (auto& oiter: outlines) {
|
2022-01-03 15:16:28 +00:00
|
|
|
JSON j_outline = j_outlines.addArrayElement(JSON::makeDictionary());
|
|
|
|
j_outline.addDictionaryMember(
|
2022-05-07 11:53:45 +00:00
|
|
|
"object",
|
|
|
|
oiter.getObjectHandle().getJSON(this->m->json_version));
|
2022-01-03 15:16:28 +00:00
|
|
|
j_outline.addDictionaryMember(
|
2022-04-30 17:23:18 +00:00
|
|
|
"title", JSON::makeString(oiter.getTitle()));
|
2022-01-03 15:16:28 +00:00
|
|
|
j_outline.addDictionaryMember(
|
2022-05-07 11:53:45 +00:00
|
|
|
"dest", oiter.getDest().getJSON(this->m->json_version, true));
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
j_page.addDictionaryMember("pageposfrom1", JSON::makeInt(1 + pageno));
|
2022-07-30 17:32:59 +00:00
|
|
|
JSON::writeArrayItem(p, first_page, j_page, 2);
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-05-06 22:46:38 +00:00
|
|
|
JSON::writeArrayClose(p, first_page, 1);
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 21:13:43 +00:00
|
|
|
void
|
Top-level json: write incrementally
This commit just changes the order in which fields are written to the
json without changing their content. All the json files in the test
suite were modified with this script to ensure that we didn't get any
changes other than ordering.
----------
#!/usr/bin/env python3
import json
import sys
def json_dumps(data):
return json.dumps(data, ensure_ascii=False,
indent=2, separators=(',', ': '))
for filename in sys.argv[1:]:
with open(filename, 'r') as f:
data = json.loads(f.read())
newdata = {}
for i in ('version', 'parameters', 'pages', 'pagelabels',
'acroform', 'attachments', 'encrypt', 'outlines',
'objects', 'objectinfo'):
if i in data:
newdata[i] = data[i]
print(json_dumps(newdata))
----------
2022-05-06 22:25:59 +00:00
|
|
|
QPDFJob::doJSONPageLabels(Pipeline* p, bool& first, QPDF& pdf)
|
2022-01-03 15:16:28 +00:00
|
|
|
{
|
Top-level json: write incrementally
This commit just changes the order in which fields are written to the
json without changing their content. All the json files in the test
suite were modified with this script to ensure that we didn't get any
changes other than ordering.
----------
#!/usr/bin/env python3
import json
import sys
def json_dumps(data):
return json.dumps(data, ensure_ascii=False,
indent=2, separators=(',', ': '))
for filename in sys.argv[1:]:
with open(filename, 'r') as f:
data = json.loads(f.read())
newdata = {}
for i in ('version', 'parameters', 'pages', 'pagelabels',
'acroform', 'attachments', 'encrypt', 'outlines',
'objects', 'objectinfo'):
if i in data:
newdata[i] = data[i]
print(json_dumps(newdata))
----------
2022-05-06 22:25:59 +00:00
|
|
|
JSON j_labels = JSON::makeArray();
|
2022-01-03 15:16:28 +00:00
|
|
|
QPDFPageLabelDocumentHelper pldh(pdf);
|
2022-06-25 12:29:07 +00:00
|
|
|
long long npages =
|
|
|
|
QIntC::to_longlong(QPDFPageDocumentHelper(pdf).getAllPages().size());
|
2022-04-02 21:14:10 +00:00
|
|
|
if (pldh.hasPageLabels()) {
|
2022-01-03 15:16:28 +00:00
|
|
|
std::vector<QPDFObjectHandle> labels;
|
2022-06-23 09:32:32 +00:00
|
|
|
pldh.getLabelsForPageRange(0, npages - 1, 0, labels);
|
2022-04-30 17:23:18 +00:00
|
|
|
for (auto iter = labels.begin(); iter != labels.end(); ++iter) {
|
2022-06-23 09:32:32 +00:00
|
|
|
if ((iter + 1) == labels.end()) {
|
2022-01-03 15:16:28 +00:00
|
|
|
// This can't happen, so ignore it. This could only
|
|
|
|
// happen if getLabelsForPageRange somehow returned an
|
|
|
|
// odd number of items.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
JSON j_label = j_labels.addArrayElement(JSON::makeDictionary());
|
2022-05-07 11:53:45 +00:00
|
|
|
j_label.addDictionaryMember(
|
|
|
|
"index", (*iter).getJSON(this->m->json_version));
|
2022-01-03 15:16:28 +00:00
|
|
|
++iter;
|
2022-05-07 11:53:45 +00:00
|
|
|
j_label.addDictionaryMember(
|
|
|
|
"label", (*iter).getJSON(this->m->json_version));
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-30 17:32:59 +00:00
|
|
|
JSON::writeDictionaryItem(p, first, "pagelabels", j_labels, 1);
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
|
2022-05-07 11:53:45 +00:00
|
|
|
void
|
|
|
|
QPDFJob::addOutlinesToJson(
|
2022-04-02 21:14:10 +00:00
|
|
|
std::vector<QPDFOutlineObjectHelper> outlines,
|
|
|
|
JSON& j,
|
2022-01-03 15:16:28 +00:00
|
|
|
std::map<QPDFObjGen, int>& page_numbers)
|
|
|
|
{
|
2022-04-30 17:23:18 +00:00
|
|
|
for (auto& ol: outlines) {
|
2022-01-03 15:16:28 +00:00
|
|
|
JSON jo = j.addArrayElement(JSON::makeDictionary());
|
2022-05-07 11:53:45 +00:00
|
|
|
jo.addDictionaryMember(
|
|
|
|
"object", ol.getObjectHandle().getJSON(this->m->json_version));
|
2022-01-03 15:16:28 +00:00
|
|
|
jo.addDictionaryMember("title", JSON::makeString(ol.getTitle()));
|
2022-05-07 11:53:45 +00:00
|
|
|
jo.addDictionaryMember(
|
|
|
|
"dest", ol.getDest().getJSON(this->m->json_version, true));
|
2022-01-03 15:16:28 +00:00
|
|
|
jo.addDictionaryMember("open", JSON::makeBool(ol.getCount() >= 0));
|
|
|
|
QPDFObjectHandle page = ol.getDestPage();
|
|
|
|
JSON j_destpage = JSON::makeNull();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (page.isIndirect()) {
|
2022-01-03 15:16:28 +00:00
|
|
|
QPDFObjGen og = page.getObjGen();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (page_numbers.count(og)) {
|
2022-01-03 15:16:28 +00:00
|
|
|
j_destpage = JSON::makeInt(page_numbers[og]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
jo.addDictionaryMember("destpageposfrom1", j_destpage);
|
|
|
|
JSON j_kids = jo.addDictionaryMember("kids", JSON::makeArray());
|
2022-05-07 11:53:45 +00:00
|
|
|
addOutlinesToJson(ol.getKids(), j_kids, page_numbers);
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-05 21:13:43 +00:00
|
|
|
void
|
Top-level json: write incrementally
This commit just changes the order in which fields are written to the
json without changing their content. All the json files in the test
suite were modified with this script to ensure that we didn't get any
changes other than ordering.
----------
#!/usr/bin/env python3
import json
import sys
def json_dumps(data):
return json.dumps(data, ensure_ascii=False,
indent=2, separators=(',', ': '))
for filename in sys.argv[1:]:
with open(filename, 'r') as f:
data = json.loads(f.read())
newdata = {}
for i in ('version', 'parameters', 'pages', 'pagelabels',
'acroform', 'attachments', 'encrypt', 'outlines',
'objects', 'objectinfo'):
if i in data:
newdata[i] = data[i]
print(json_dumps(newdata))
----------
2022-05-06 22:25:59 +00:00
|
|
|
QPDFJob::doJSONOutlines(Pipeline* p, bool& first, QPDF& pdf)
|
2022-01-03 15:16:28 +00:00
|
|
|
{
|
|
|
|
std::map<QPDFObjGen, int> page_numbers;
|
|
|
|
int n = 0;
|
2022-05-21 14:18:15 +00:00
|
|
|
for (auto const& ph: QPDFPageDocumentHelper(pdf).getAllPages()) {
|
2022-04-30 17:23:18 +00:00
|
|
|
QPDFObjectHandle oh = ph.getObjectHandle();
|
2022-01-03 15:16:28 +00:00
|
|
|
page_numbers[oh.getObjGen()] = ++n;
|
|
|
|
}
|
|
|
|
|
Top-level json: write incrementally
This commit just changes the order in which fields are written to the
json without changing their content. All the json files in the test
suite were modified with this script to ensure that we didn't get any
changes other than ordering.
----------
#!/usr/bin/env python3
import json
import sys
def json_dumps(data):
return json.dumps(data, ensure_ascii=False,
indent=2, separators=(',', ': '))
for filename in sys.argv[1:]:
with open(filename, 'r') as f:
data = json.loads(f.read())
newdata = {}
for i in ('version', 'parameters', 'pages', 'pagelabels',
'acroform', 'attachments', 'encrypt', 'outlines',
'objects', 'objectinfo'):
if i in data:
newdata[i] = data[i]
print(json_dumps(newdata))
----------
2022-05-06 22:25:59 +00:00
|
|
|
JSON j_outlines = JSON::makeArray();
|
2022-01-03 15:16:28 +00:00
|
|
|
QPDFOutlineDocumentHelper odh(pdf);
|
2022-05-07 11:53:45 +00:00
|
|
|
addOutlinesToJson(odh.getTopLevelOutlines(), j_outlines, page_numbers);
|
2022-07-30 17:32:59 +00:00
|
|
|
JSON::writeDictionaryItem(p, first, "outlines", j_outlines, 1);
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 21:13:43 +00:00
|
|
|
void
|
Top-level json: write incrementally
This commit just changes the order in which fields are written to the
json without changing their content. All the json files in the test
suite were modified with this script to ensure that we didn't get any
changes other than ordering.
----------
#!/usr/bin/env python3
import json
import sys
def json_dumps(data):
return json.dumps(data, ensure_ascii=False,
indent=2, separators=(',', ': '))
for filename in sys.argv[1:]:
with open(filename, 'r') as f:
data = json.loads(f.read())
newdata = {}
for i in ('version', 'parameters', 'pages', 'pagelabels',
'acroform', 'attachments', 'encrypt', 'outlines',
'objects', 'objectinfo'):
if i in data:
newdata[i] = data[i]
print(json_dumps(newdata))
----------
2022-05-06 22:25:59 +00:00
|
|
|
QPDFJob::doJSONAcroform(Pipeline* p, bool& first, QPDF& pdf)
|
2022-01-03 15:16:28 +00:00
|
|
|
{
|
Top-level json: write incrementally
This commit just changes the order in which fields are written to the
json without changing their content. All the json files in the test
suite were modified with this script to ensure that we didn't get any
changes other than ordering.
----------
#!/usr/bin/env python3
import json
import sys
def json_dumps(data):
return json.dumps(data, ensure_ascii=False,
indent=2, separators=(',', ': '))
for filename in sys.argv[1:]:
with open(filename, 'r') as f:
data = json.loads(f.read())
newdata = {}
for i in ('version', 'parameters', 'pages', 'pagelabels',
'acroform', 'attachments', 'encrypt', 'outlines',
'objects', 'objectinfo'):
if i in data:
newdata[i] = data[i]
print(json_dumps(newdata))
----------
2022-05-06 22:25:59 +00:00
|
|
|
JSON j_acroform = JSON::makeDictionary();
|
2022-01-03 15:16:28 +00:00
|
|
|
QPDFAcroFormDocumentHelper afdh(pdf);
|
|
|
|
j_acroform.addDictionaryMember(
|
2022-04-02 21:14:10 +00:00
|
|
|
"hasacroform", JSON::makeBool(afdh.hasAcroForm()));
|
2022-01-03 15:16:28 +00:00
|
|
|
j_acroform.addDictionaryMember(
|
2022-04-02 21:14:10 +00:00
|
|
|
"needappearances", JSON::makeBool(afdh.getNeedAppearances()));
|
|
|
|
JSON j_fields = j_acroform.addDictionaryMember("fields", JSON::makeArray());
|
2022-01-03 15:16:28 +00:00
|
|
|
int pagepos1 = 0;
|
2022-05-21 14:18:15 +00:00
|
|
|
for (auto const& page: QPDFPageDocumentHelper(pdf).getAllPages()) {
|
2022-01-03 15:16:28 +00:00
|
|
|
++pagepos1;
|
2022-05-21 14:18:15 +00:00
|
|
|
for (auto& aoh: afdh.getWidgetAnnotationsForPage(page)) {
|
2022-04-02 21:14:10 +00:00
|
|
|
QPDFFormFieldObjectHelper ffh = afdh.getFieldForAnnotation(aoh);
|
|
|
|
JSON j_field = j_fields.addArrayElement(JSON::makeDictionary());
|
2022-01-03 15:16:28 +00:00
|
|
|
j_field.addDictionaryMember(
|
2022-05-07 11:53:45 +00:00
|
|
|
"object", ffh.getObjectHandle().getJSON(this->m->json_version));
|
2022-01-03 15:16:28 +00:00
|
|
|
j_field.addDictionaryMember(
|
2022-05-07 11:53:45 +00:00
|
|
|
"parent",
|
|
|
|
ffh.getObjectHandle().getKey("/Parent").getJSON(
|
|
|
|
this->m->json_version));
|
2022-01-03 15:16:28 +00:00
|
|
|
j_field.addDictionaryMember(
|
2022-04-02 21:14:10 +00:00
|
|
|
"pageposfrom1", JSON::makeInt(pagepos1));
|
2022-01-03 15:16:28 +00:00
|
|
|
j_field.addDictionaryMember(
|
2022-04-02 21:14:10 +00:00
|
|
|
"fieldtype", JSON::makeString(ffh.getFieldType()));
|
2022-01-03 15:16:28 +00:00
|
|
|
j_field.addDictionaryMember(
|
2022-04-02 21:14:10 +00:00
|
|
|
"fieldflags", JSON::makeInt(ffh.getFlags()));
|
2022-01-03 15:16:28 +00:00
|
|
|
j_field.addDictionaryMember(
|
2022-04-02 21:14:10 +00:00
|
|
|
"fullname", JSON::makeString(ffh.getFullyQualifiedName()));
|
2022-01-03 15:16:28 +00:00
|
|
|
j_field.addDictionaryMember(
|
2022-04-02 21:14:10 +00:00
|
|
|
"partialname", JSON::makeString(ffh.getPartialName()));
|
2022-01-03 15:16:28 +00:00
|
|
|
j_field.addDictionaryMember(
|
2022-04-02 21:14:10 +00:00
|
|
|
"alternativename", JSON::makeString(ffh.getAlternativeName()));
|
2022-01-03 15:16:28 +00:00
|
|
|
j_field.addDictionaryMember(
|
2022-04-02 21:14:10 +00:00
|
|
|
"mappingname", JSON::makeString(ffh.getMappingName()));
|
2022-01-03 15:16:28 +00:00
|
|
|
j_field.addDictionaryMember(
|
2022-05-07 11:53:45 +00:00
|
|
|
"value", ffh.getValue().getJSON(this->m->json_version));
|
|
|
|
j_field.addDictionaryMember(
|
|
|
|
"defaultvalue",
|
|
|
|
ffh.getDefaultValue().getJSON(this->m->json_version));
|
2022-01-03 15:16:28 +00:00
|
|
|
j_field.addDictionaryMember(
|
2022-04-02 21:14:10 +00:00
|
|
|
"quadding", JSON::makeInt(ffh.getQuadding()));
|
2022-01-03 15:16:28 +00:00
|
|
|
j_field.addDictionaryMember(
|
2022-04-02 21:14:10 +00:00
|
|
|
"ischeckbox", JSON::makeBool(ffh.isCheckbox()));
|
2022-01-03 15:16:28 +00:00
|
|
|
j_field.addDictionaryMember(
|
2022-04-02 21:14:10 +00:00
|
|
|
"isradiobutton", JSON::makeBool(ffh.isRadioButton()));
|
2022-01-03 15:16:28 +00:00
|
|
|
j_field.addDictionaryMember(
|
2022-04-02 21:14:10 +00:00
|
|
|
"ischoice", JSON::makeBool(ffh.isChoice()));
|
|
|
|
j_field.addDictionaryMember("istext", JSON::makeBool(ffh.isText()));
|
|
|
|
JSON j_choices =
|
|
|
|
j_field.addDictionaryMember("choices", JSON::makeArray());
|
2022-05-21 14:18:15 +00:00
|
|
|
for (auto const& choice: ffh.getChoices()) {
|
2022-04-30 17:23:18 +00:00
|
|
|
j_choices.addArrayElement(JSON::makeString(choice));
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
JSON j_annot = j_field.addDictionaryMember(
|
|
|
|
"annotation", JSON::makeDictionary());
|
|
|
|
j_annot.addDictionaryMember(
|
2022-05-07 11:53:45 +00:00
|
|
|
"object", aoh.getObjectHandle().getJSON(this->m->json_version));
|
2022-01-03 15:16:28 +00:00
|
|
|
j_annot.addDictionaryMember(
|
2022-04-02 21:14:10 +00:00
|
|
|
"appearancestate", JSON::makeString(aoh.getAppearanceState()));
|
2022-01-03 15:16:28 +00:00
|
|
|
j_annot.addDictionaryMember(
|
2022-04-02 21:14:10 +00:00
|
|
|
"annotationflags", JSON::makeInt(aoh.getFlags()));
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-30 17:32:59 +00:00
|
|
|
JSON::writeDictionaryItem(p, first, "acroform", j_acroform, 1);
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 21:13:43 +00:00
|
|
|
void
|
Top-level json: write incrementally
This commit just changes the order in which fields are written to the
json without changing their content. All the json files in the test
suite were modified with this script to ensure that we didn't get any
changes other than ordering.
----------
#!/usr/bin/env python3
import json
import sys
def json_dumps(data):
return json.dumps(data, ensure_ascii=False,
indent=2, separators=(',', ': '))
for filename in sys.argv[1:]:
with open(filename, 'r') as f:
data = json.loads(f.read())
newdata = {}
for i in ('version', 'parameters', 'pages', 'pagelabels',
'acroform', 'attachments', 'encrypt', 'outlines',
'objects', 'objectinfo'):
if i in data:
newdata[i] = data[i]
print(json_dumps(newdata))
----------
2022-05-06 22:25:59 +00:00
|
|
|
QPDFJob::doJSONEncrypt(Pipeline* p, bool& first, QPDF& pdf)
|
2022-01-03 15:16:28 +00:00
|
|
|
{
|
|
|
|
int R = 0;
|
|
|
|
int P = 0;
|
|
|
|
int V = 0;
|
|
|
|
QPDF::encryption_method_e stream_method = QPDF::e_none;
|
|
|
|
QPDF::encryption_method_e string_method = QPDF::e_none;
|
|
|
|
QPDF::encryption_method_e file_method = QPDF::e_none;
|
2022-04-02 21:14:10 +00:00
|
|
|
bool is_encrypted =
|
|
|
|
pdf.isEncrypted(R, P, V, stream_method, string_method, file_method);
|
Top-level json: write incrementally
This commit just changes the order in which fields are written to the
json without changing their content. All the json files in the test
suite were modified with this script to ensure that we didn't get any
changes other than ordering.
----------
#!/usr/bin/env python3
import json
import sys
def json_dumps(data):
return json.dumps(data, ensure_ascii=False,
indent=2, separators=(',', ': '))
for filename in sys.argv[1:]:
with open(filename, 'r') as f:
data = json.loads(f.read())
newdata = {}
for i in ('version', 'parameters', 'pages', 'pagelabels',
'acroform', 'attachments', 'encrypt', 'outlines',
'objects', 'objectinfo'):
if i in data:
newdata[i] = data[i]
print(json_dumps(newdata))
----------
2022-05-06 22:25:59 +00:00
|
|
|
JSON j_encrypt = JSON::makeDictionary();
|
2022-04-02 21:14:10 +00:00
|
|
|
j_encrypt.addDictionaryMember("encrypted", JSON::makeBool(is_encrypted));
|
2022-01-03 15:16:28 +00:00
|
|
|
j_encrypt.addDictionaryMember(
|
|
|
|
"userpasswordmatched",
|
|
|
|
JSON::makeBool(is_encrypted && pdf.userPasswordMatched()));
|
|
|
|
j_encrypt.addDictionaryMember(
|
|
|
|
"ownerpasswordmatched",
|
|
|
|
JSON::makeBool(is_encrypted && pdf.ownerPasswordMatched()));
|
2022-05-30 14:55:07 +00:00
|
|
|
if (is_encrypted && (V < 5) && pdf.ownerPasswordMatched() &&
|
|
|
|
(!pdf.userPasswordMatched())) {
|
|
|
|
std::string user_password = pdf.getTrimmedUserPassword();
|
|
|
|
j_encrypt.addDictionaryMember(
|
|
|
|
"recovereduserpassword", JSON::makeString(user_password));
|
|
|
|
} else {
|
|
|
|
j_encrypt.addDictionaryMember(
|
|
|
|
"recovereduserpassword", JSON::makeNull());
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
JSON j_capabilities =
|
|
|
|
j_encrypt.addDictionaryMember("capabilities", JSON::makeDictionary());
|
2022-01-03 15:16:28 +00:00
|
|
|
j_capabilities.addDictionaryMember(
|
2022-04-02 21:14:10 +00:00
|
|
|
"accessibility", JSON::makeBool(pdf.allowAccessibility()));
|
2022-01-03 15:16:28 +00:00
|
|
|
j_capabilities.addDictionaryMember(
|
2022-04-02 21:14:10 +00:00
|
|
|
"extract", JSON::makeBool(pdf.allowExtractAll()));
|
2022-01-03 15:16:28 +00:00
|
|
|
j_capabilities.addDictionaryMember(
|
2022-04-02 21:14:10 +00:00
|
|
|
"printlow", JSON::makeBool(pdf.allowPrintLowRes()));
|
2022-01-03 15:16:28 +00:00
|
|
|
j_capabilities.addDictionaryMember(
|
2022-04-02 21:14:10 +00:00
|
|
|
"printhigh", JSON::makeBool(pdf.allowPrintHighRes()));
|
2022-01-03 15:16:28 +00:00
|
|
|
j_capabilities.addDictionaryMember(
|
2022-04-02 21:14:10 +00:00
|
|
|
"modifyassembly", JSON::makeBool(pdf.allowModifyAssembly()));
|
2022-01-03 15:16:28 +00:00
|
|
|
j_capabilities.addDictionaryMember(
|
2022-04-02 21:14:10 +00:00
|
|
|
"modifyforms", JSON::makeBool(pdf.allowModifyForm()));
|
2022-05-01 15:48:41 +00:00
|
|
|
/* cSpell:ignore moddifyannotations */
|
2022-05-07 15:38:04 +00:00
|
|
|
std::string MODIFY_ANNOTATIONS =
|
|
|
|
(this->m->json_version == 1 ? "moddifyannotations"
|
|
|
|
: "modifyannotations");
|
2022-01-03 15:16:28 +00:00
|
|
|
j_capabilities.addDictionaryMember(
|
2022-05-07 15:38:04 +00:00
|
|
|
MODIFY_ANNOTATIONS, JSON::makeBool(pdf.allowModifyAnnotation()));
|
2022-01-03 15:16:28 +00:00
|
|
|
j_capabilities.addDictionaryMember(
|
2022-04-02 21:14:10 +00:00
|
|
|
"modifyother", JSON::makeBool(pdf.allowModifyOther()));
|
2022-01-03 15:16:28 +00:00
|
|
|
j_capabilities.addDictionaryMember(
|
2022-04-02 21:14:10 +00:00
|
|
|
"modify", JSON::makeBool(pdf.allowModifyAll()));
|
|
|
|
JSON j_parameters =
|
|
|
|
j_encrypt.addDictionaryMember("parameters", JSON::makeDictionary());
|
2022-01-03 15:16:28 +00:00
|
|
|
j_parameters.addDictionaryMember("R", JSON::makeInt(R));
|
|
|
|
j_parameters.addDictionaryMember("V", JSON::makeInt(V));
|
|
|
|
j_parameters.addDictionaryMember("P", JSON::makeInt(P));
|
|
|
|
int bits = 0;
|
|
|
|
JSON key = JSON::makeNull();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (is_encrypted) {
|
2022-01-03 15:16:28 +00:00
|
|
|
std::string encryption_key = pdf.getEncryptionKey();
|
|
|
|
bits = QIntC::to_int(encryption_key.length() * 8);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->show_encryption_key) {
|
2022-01-03 15:16:28 +00:00
|
|
|
key = JSON::makeString(QUtil::hex_encode(encryption_key));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
j_parameters.addDictionaryMember("bits", JSON::makeInt(bits));
|
|
|
|
j_parameters.addDictionaryMember("key", key);
|
|
|
|
auto fix_method = [is_encrypted](QPDF::encryption_method_e& m) {
|
2022-04-02 21:14:10 +00:00
|
|
|
if (is_encrypted && m == QPDF::e_none) {
|
2022-01-26 19:56:24 +00:00
|
|
|
m = QPDF::e_rc4;
|
|
|
|
}
|
|
|
|
};
|
2022-01-03 15:16:28 +00:00
|
|
|
fix_method(stream_method);
|
|
|
|
fix_method(string_method);
|
|
|
|
fix_method(file_method);
|
|
|
|
std::string s_stream_method = show_encryption_method(stream_method);
|
|
|
|
std::string s_string_method = show_encryption_method(string_method);
|
|
|
|
std::string s_file_method = show_encryption_method(file_method);
|
|
|
|
std::string s_overall_method;
|
2022-04-02 21:14:10 +00:00
|
|
|
if ((stream_method == string_method) && (stream_method == file_method)) {
|
2022-01-03 15:16:28 +00:00
|
|
|
s_overall_method = s_stream_method;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-01-03 15:16:28 +00:00
|
|
|
s_overall_method = "mixed";
|
|
|
|
}
|
|
|
|
j_parameters.addDictionaryMember(
|
|
|
|
"method", JSON::makeString(s_overall_method));
|
|
|
|
j_parameters.addDictionaryMember(
|
|
|
|
"streammethod", JSON::makeString(s_stream_method));
|
|
|
|
j_parameters.addDictionaryMember(
|
|
|
|
"stringmethod", JSON::makeString(s_string_method));
|
|
|
|
j_parameters.addDictionaryMember(
|
|
|
|
"filemethod", JSON::makeString(s_file_method));
|
2022-07-30 17:32:59 +00:00
|
|
|
JSON::writeDictionaryItem(p, first, "encrypt", j_encrypt, 1);
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 21:13:43 +00:00
|
|
|
void
|
Top-level json: write incrementally
This commit just changes the order in which fields are written to the
json without changing their content. All the json files in the test
suite were modified with this script to ensure that we didn't get any
changes other than ordering.
----------
#!/usr/bin/env python3
import json
import sys
def json_dumps(data):
return json.dumps(data, ensure_ascii=False,
indent=2, separators=(',', ': '))
for filename in sys.argv[1:]:
with open(filename, 'r') as f:
data = json.loads(f.read())
newdata = {}
for i in ('version', 'parameters', 'pages', 'pagelabels',
'acroform', 'attachments', 'encrypt', 'outlines',
'objects', 'objectinfo'):
if i in data:
newdata[i] = data[i]
print(json_dumps(newdata))
----------
2022-05-06 22:25:59 +00:00
|
|
|
QPDFJob::doJSONAttachments(Pipeline* p, bool& first, QPDF& pdf)
|
2022-01-03 15:16:28 +00:00
|
|
|
{
|
2022-05-30 13:23:48 +00:00
|
|
|
auto to_iso8601 = [](std::string const& d) {
|
|
|
|
// Convert PDF date to iso8601 if not empty; if empty, return
|
|
|
|
// empty.
|
|
|
|
std::string iso8601;
|
|
|
|
QUtil::pdf_time_to_iso8601(d, iso8601);
|
|
|
|
return iso8601;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto null_or_string = [](std::string const& s) {
|
|
|
|
if (s.empty()) {
|
|
|
|
return JSON::makeNull();
|
|
|
|
} else {
|
|
|
|
return JSON::makeString(s);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
Top-level json: write incrementally
This commit just changes the order in which fields are written to the
json without changing their content. All the json files in the test
suite were modified with this script to ensure that we didn't get any
changes other than ordering.
----------
#!/usr/bin/env python3
import json
import sys
def json_dumps(data):
return json.dumps(data, ensure_ascii=False,
indent=2, separators=(',', ': '))
for filename in sys.argv[1:]:
with open(filename, 'r') as f:
data = json.loads(f.read())
newdata = {}
for i in ('version', 'parameters', 'pages', 'pagelabels',
'acroform', 'attachments', 'encrypt', 'outlines',
'objects', 'objectinfo'):
if i in data:
newdata[i] = data[i]
print(json_dumps(newdata))
----------
2022-05-06 22:25:59 +00:00
|
|
|
JSON j_attachments = JSON::makeDictionary();
|
2022-01-03 15:16:28 +00:00
|
|
|
QPDFEmbeddedFileDocumentHelper efdh(pdf);
|
2022-04-30 13:43:07 +00:00
|
|
|
for (auto const& iter: efdh.getEmbeddedFiles()) {
|
2022-01-03 15:16:28 +00:00
|
|
|
std::string const& key = iter.first;
|
|
|
|
auto fsoh = iter.second;
|
2022-04-02 21:14:10 +00:00
|
|
|
auto j_details =
|
|
|
|
j_attachments.addDictionaryMember(key, JSON::makeDictionary());
|
2022-01-03 15:16:28 +00:00
|
|
|
j_details.addDictionaryMember(
|
2022-04-02 21:14:10 +00:00
|
|
|
"filespec", JSON::makeString(fsoh->getObjectHandle().unparse()));
|
2022-01-03 15:16:28 +00:00
|
|
|
j_details.addDictionaryMember(
|
|
|
|
"preferredname", JSON::makeString(fsoh->getFilename()));
|
|
|
|
j_details.addDictionaryMember(
|
|
|
|
"preferredcontents",
|
|
|
|
JSON::makeString(fsoh->getEmbeddedFileStream().unparse()));
|
2022-05-30 13:23:48 +00:00
|
|
|
j_details.addDictionaryMember(
|
|
|
|
"description", null_or_string(fsoh->getDescription()));
|
|
|
|
auto j_names =
|
|
|
|
j_details.addDictionaryMember("names", JSON::makeDictionary());
|
|
|
|
for (auto const& i2: fsoh->getFilenames()) {
|
|
|
|
j_names.addDictionaryMember(i2.first, JSON::makeString(i2.second));
|
|
|
|
}
|
|
|
|
auto j_streams =
|
|
|
|
j_details.addDictionaryMember("streams", JSON::makeDictionary());
|
|
|
|
for (auto i2: fsoh->getEmbeddedFileStreams().ditems()) {
|
|
|
|
auto efs = QPDFEFStreamObjectHelper(i2.second);
|
|
|
|
auto j_stream =
|
|
|
|
j_streams.addDictionaryMember(i2.first, JSON::makeDictionary());
|
|
|
|
j_stream.addDictionaryMember(
|
|
|
|
"creationdate",
|
|
|
|
null_or_string(to_iso8601(efs.getCreationDate())));
|
|
|
|
j_stream.addDictionaryMember(
|
|
|
|
"modificationdate",
|
|
|
|
null_or_string(to_iso8601(efs.getCreationDate())));
|
|
|
|
j_stream.addDictionaryMember(
|
|
|
|
"mimetype", null_or_string(efs.getSubtype()));
|
|
|
|
j_stream.addDictionaryMember(
|
|
|
|
"checksum",
|
|
|
|
null_or_string(QUtil::hex_encode(efs.getChecksum())));
|
|
|
|
}
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-07-30 17:32:59 +00:00
|
|
|
JSON::writeDictionaryItem(p, first, "attachments", j_attachments, 1);
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSON
|
2022-05-07 15:38:04 +00:00
|
|
|
QPDFJob::json_schema(int json_version, std::set<std::string>* keys)
|
2022-01-03 15:16:28 +00:00
|
|
|
{
|
|
|
|
// Style: use all lower-case keys with no dashes or underscores.
|
|
|
|
// Choose array or dictionary based on indexing. For example, we
|
|
|
|
// use a dictionary for objects because we want to index by object
|
|
|
|
// ID and an array for pages because we want to index by position.
|
|
|
|
// The pages in the pages array contain references back to the
|
|
|
|
// original object, which can be resolved in the objects
|
|
|
|
// dictionary. When a PDF construct that maps back to an original
|
|
|
|
// object is represented separately, use "object" as the key that
|
|
|
|
// references the original object.
|
|
|
|
|
|
|
|
// This JSON object doubles as a schema and as documentation for
|
|
|
|
// our JSON output. Any schema mismatch is a bug in qpdf. This
|
|
|
|
// helps to enforce our policy of consistently providing a known
|
|
|
|
// structure where every documented key will always be present,
|
|
|
|
// which makes it easier to consume our JSON. This is discussed in
|
|
|
|
// more depth in the manual.
|
|
|
|
JSON schema = JSON::makeDictionary();
|
|
|
|
schema.addDictionaryMember(
|
2022-04-02 21:14:10 +00:00
|
|
|
"version",
|
|
|
|
JSON::makeString(
|
2022-01-03 15:16:28 +00:00
|
|
|
"JSON format serial number; increased for non-compatible changes"));
|
2022-04-02 21:14:10 +00:00
|
|
|
JSON j_params = schema.addDictionaryMember("parameters", JSON::parse(R"({
|
2022-01-19 16:21:48 +00:00
|
|
|
"decodelevel": "decode level used to determine stream filterability"
|
|
|
|
})"));
|
2022-01-03 15:16:28 +00:00
|
|
|
|
2022-07-26 11:37:50 +00:00
|
|
|
bool all_keys = ((keys == nullptr) || keys->empty());
|
2022-01-03 15:16:28 +00:00
|
|
|
|
2022-01-07 22:15:15 +00:00
|
|
|
// The list of selectable top-level keys id duplicated in the
|
|
|
|
// following places: job.yml, QPDFJob::json_schema, and
|
|
|
|
// QPDFJob::doJSON.
|
2022-07-31 00:53:30 +00:00
|
|
|
if (json_version == 1) {
|
|
|
|
if (all_keys || keys->count("objects")) {
|
|
|
|
schema.addDictionaryMember("objects", JSON::parse(R"({
|
2022-01-19 16:41:23 +00:00
|
|
|
"<n n R|trailer>": "json representation of object"
|
|
|
|
})"));
|
2022-07-31 00:53:30 +00:00
|
|
|
}
|
2022-05-07 17:33:45 +00:00
|
|
|
if (all_keys || keys->count("objectinfo")) {
|
|
|
|
JSON objectinfo =
|
|
|
|
schema.addDictionaryMember("objectinfo", JSON::parse(R"({
|
2022-01-19 16:21:48 +00:00
|
|
|
"<object-id>": {
|
|
|
|
"stream": {
|
|
|
|
"filter": "if stream, its filters, otherwise null",
|
|
|
|
"is": "whether the object is a stream",
|
|
|
|
"length": "if stream, its length, otherwise null"
|
|
|
|
}
|
|
|
|
}
|
2022-05-07 17:33:45 +00:00
|
|
|
})"));
|
|
|
|
}
|
2022-07-31 00:53:30 +00:00
|
|
|
} else {
|
|
|
|
if (all_keys || keys->count("qpdf")) {
|
|
|
|
schema.addDictionaryMember("qpdf", JSON::parse(R"([{
|
|
|
|
"jsonversion": "numeric JSON version",
|
|
|
|
"pdfversion": "PDF version as x.y",
|
|
|
|
"pushedinheritedpageresources": "whether inherited attributes were pushed to the page level",
|
|
|
|
"calledgetallpages": "whether getAllPages was called",
|
|
|
|
"maxobjectid": "highest object ID in output, ignored on input"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"<obj:n n R|trailer>": "json representation of object"
|
|
|
|
}])"));
|
|
|
|
}
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (all_keys || keys->count("pages")) {
|
2022-01-19 16:21:48 +00:00
|
|
|
JSON page = schema.addDictionaryMember("pages", JSON::parse(R"([
|
|
|
|
{
|
|
|
|
"contents": [
|
|
|
|
"reference to each content stream"
|
|
|
|
],
|
|
|
|
"images": [
|
|
|
|
{
|
|
|
|
"bitspercomponent": "bits per component",
|
|
|
|
"colorspace": "color space",
|
|
|
|
"decodeparms": [
|
|
|
|
"decode parameters for image data"
|
|
|
|
],
|
|
|
|
"filter": [
|
|
|
|
"filters applied to image data"
|
|
|
|
],
|
|
|
|
"filterable": "whether image data can be decoded using the decode level qpdf was invoked with",
|
|
|
|
"height": "image height",
|
|
|
|
"name": "name of image in XObject table",
|
|
|
|
"object": "reference to image stream",
|
|
|
|
"width": "image width"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"label": "page label dictionary, or null if none",
|
|
|
|
"object": "reference to original page object",
|
|
|
|
"outlines": [
|
|
|
|
{
|
|
|
|
"dest": "outline destination dictionary",
|
|
|
|
"object": "reference to outline that targets this page",
|
|
|
|
"title": "outline title"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"pageposfrom1": "position of page in document numbering from 1"
|
|
|
|
}
|
|
|
|
])"));
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (all_keys || keys->count("pagelabels")) {
|
|
|
|
JSON labels = schema.addDictionaryMember("pagelabels", JSON::parse(R"([
|
2022-01-19 16:21:48 +00:00
|
|
|
{
|
|
|
|
"index": "starting page position starting from zero",
|
|
|
|
"label": "page label dictionary"
|
|
|
|
}
|
|
|
|
])"));
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (all_keys || keys->count("outlines")) {
|
|
|
|
JSON outlines = schema.addDictionaryMember("outlines", JSON::parse(R"([
|
2022-01-19 16:21:48 +00:00
|
|
|
{
|
|
|
|
"dest": "outline destination dictionary",
|
|
|
|
"destpageposfrom1": "position of destination page in document numbered from 1; null if not known",
|
|
|
|
"kids": "array of descendent outlines",
|
|
|
|
"object": "reference to this outline",
|
|
|
|
"open": "whether the outline is displayed expanded",
|
|
|
|
"title": "outline title"
|
|
|
|
}
|
|
|
|
])"));
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (all_keys || keys->count("acroform")) {
|
|
|
|
JSON acroform = schema.addDictionaryMember("acroform", JSON::parse(R"({
|
2022-01-19 16:21:48 +00:00
|
|
|
"fields": [
|
|
|
|
{
|
|
|
|
"alternativename": "alternative name of field -- this is the one usually shown to users",
|
|
|
|
"annotation": {
|
|
|
|
"annotationflags": "annotation flags from /F -- see pdf_annotation_flag_e in qpdf/Constants.h",
|
|
|
|
"appearancestate": "appearance state -- can be used to determine value for checkboxes and radio buttons",
|
|
|
|
"object": "reference to the annotation object"
|
|
|
|
},
|
|
|
|
"choices": "for choices fields, the list of choices presented to the user",
|
|
|
|
"defaultvalue": "default value of field",
|
|
|
|
"fieldflags": "form field flags from /Ff -- see pdf_form_field_flag_e in qpdf/Constants.h",
|
|
|
|
"fieldtype": "field type",
|
|
|
|
"fullname": "full name of field",
|
|
|
|
"ischeckbox": "whether field is a checkbox",
|
|
|
|
"ischoice": "whether field is a list, combo, or dropdown",
|
|
|
|
"isradiobutton": "whether field is a radio button -- buttons in a single group share a parent",
|
|
|
|
"istext": "whether field is a text field",
|
|
|
|
"mappingname": "mapping name of field",
|
|
|
|
"object": "reference to this form field",
|
|
|
|
"pageposfrom1": "position of containing page numbered from 1",
|
|
|
|
"parent": "reference to this field's parent",
|
|
|
|
"partialname": "partial name of field",
|
|
|
|
"quadding": "field quadding -- number indicating left, center, or right",
|
|
|
|
"value": "value of field"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"hasacroform": "whether the document has interactive forms",
|
|
|
|
"needappearances": "whether the form fields' appearance streams need to be regenerated"
|
|
|
|
})"));
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-05-07 15:38:04 +00:00
|
|
|
std::string MODIFY_ANNOTATIONS =
|
|
|
|
(json_version == 1 ? "moddifyannotations" : "modifyannotations");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (all_keys || keys->count("encrypt")) {
|
|
|
|
JSON encrypt = schema.addDictionaryMember("encrypt", JSON::parse(R"({
|
2022-01-19 16:21:48 +00:00
|
|
|
"capabilities": {
|
|
|
|
"accessibility": "allow extraction for accessibility?",
|
|
|
|
"extract": "allow extraction?",
|
2022-05-07 15:38:04 +00:00
|
|
|
")" + MODIFY_ANNOTATIONS + R"(": "allow modifying annotations?",
|
2022-01-19 16:21:48 +00:00
|
|
|
"modify": "allow all modifications?",
|
|
|
|
"modifyassembly": "allow modifying document assembly?",
|
|
|
|
"modifyforms": "allow modifying forms?",
|
|
|
|
"modifyother": "allow other modifications?",
|
|
|
|
"printhigh": "allow high resolution printing?",
|
|
|
|
"printlow": "allow low resolution printing?"
|
|
|
|
},
|
|
|
|
"encrypted": "whether the document is encrypted",
|
|
|
|
"ownerpasswordmatched": "whether supplied password matched owner password; always false for non-encrypted files",
|
2022-05-30 14:55:07 +00:00
|
|
|
"recovereduserpassword": "If the owner password was used to recover the user password, reveal user password; otherwise null",
|
2022-01-19 16:21:48 +00:00
|
|
|
"parameters": {
|
|
|
|
"P": "P value from Encrypt dictionary",
|
|
|
|
"R": "R value from Encrypt dictionary",
|
|
|
|
"V": "V value from Encrypt dictionary",
|
|
|
|
"bits": "encryption key bit length",
|
|
|
|
"filemethod": "encryption method for attachments",
|
|
|
|
"key": "encryption key; will be null unless --show-encryption-key was specified",
|
|
|
|
"method": "overall encryption method: none, mixed, RC4, AESv2, AESv3",
|
|
|
|
"streammethod": "encryption method for streams",
|
|
|
|
"stringmethod": "encryption method for string"
|
|
|
|
},
|
|
|
|
"userpasswordmatched": "whether supplied password matched user password; always false for non-encrypted files"
|
|
|
|
})"));
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (all_keys || keys->count("attachments")) {
|
|
|
|
JSON attachments =
|
|
|
|
schema.addDictionaryMember("attachments", JSON::parse(R"({
|
2022-01-19 16:21:48 +00:00
|
|
|
"<attachment-key>": {
|
|
|
|
"filespec": "object containing the file spec",
|
|
|
|
"preferredcontents": "most preferred embedded file stream",
|
2022-05-30 13:23:48 +00:00
|
|
|
"preferredname": "most preferred file name",
|
|
|
|
"description": "description of attachment",
|
|
|
|
"names": {
|
|
|
|
"<name-key>": "file name for key"
|
|
|
|
},
|
|
|
|
"streams": {
|
|
|
|
"<stream-key>": {
|
|
|
|
"creationdate": "ISO-8601 creation date or null",
|
|
|
|
"modificationdate": "ISO-8601 modification date or null",
|
|
|
|
"mimetype": "mime type or null",
|
|
|
|
"checksum": "MD5 checksum or null"
|
|
|
|
}
|
|
|
|
}
|
2022-01-19 16:21:48 +00:00
|
|
|
}
|
|
|
|
})"));
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
return schema;
|
|
|
|
}
|
|
|
|
|
2022-07-31 12:03:18 +00:00
|
|
|
std::string
|
|
|
|
QPDFJob::json_out_schema(int version)
|
|
|
|
{
|
|
|
|
return json_schema(version).unparse();
|
|
|
|
}
|
|
|
|
|
2022-01-26 21:40:14 +00:00
|
|
|
std::string
|
|
|
|
QPDFJob::json_out_schema_v1()
|
|
|
|
{
|
2022-05-07 15:38:04 +00:00
|
|
|
return json_schema(1).unparse();
|
2022-01-26 21:40:14 +00:00
|
|
|
}
|
|
|
|
|
2022-01-03 17:16:16 +00:00
|
|
|
void
|
2022-05-06 20:37:37 +00:00
|
|
|
QPDFJob::doJSON(QPDF& pdf, Pipeline* p)
|
2022-01-03 15:16:28 +00:00
|
|
|
{
|
2022-07-30 23:29:55 +00:00
|
|
|
// qpdf guarantees that no new top-level keys whose names start
|
2022-07-31 14:34:05 +00:00
|
|
|
// with "x-" will be added. These are reserved for users.
|
2022-07-30 23:29:55 +00:00
|
|
|
|
2022-05-06 21:49:28 +00:00
|
|
|
std::string captured_json;
|
|
|
|
std::shared_ptr<Pl_String> pl_str;
|
|
|
|
if (this->m->test_json_schema) {
|
|
|
|
pl_str = std::make_shared<Pl_String>("capture json", p, captured_json);
|
|
|
|
p = pl_str.get();
|
|
|
|
}
|
|
|
|
|
Top-level json: write incrementally
This commit just changes the order in which fields are written to the
json without changing their content. All the json files in the test
suite were modified with this script to ensure that we didn't get any
changes other than ordering.
----------
#!/usr/bin/env python3
import json
import sys
def json_dumps(data):
return json.dumps(data, ensure_ascii=False,
indent=2, separators=(',', ': '))
for filename in sys.argv[1:]:
with open(filename, 'r') as f:
data = json.loads(f.read())
newdata = {}
for i in ('version', 'parameters', 'pages', 'pagelabels',
'acroform', 'attachments', 'encrypt', 'outlines',
'objects', 'objectinfo'):
if i in data:
newdata[i] = data[i]
print(json_dumps(newdata))
----------
2022-05-06 22:25:59 +00:00
|
|
|
bool first = true;
|
|
|
|
JSON::writeDictionaryOpen(p, first, 0);
|
2022-01-03 15:16:28 +00:00
|
|
|
|
2022-07-31 14:34:05 +00:00
|
|
|
if (m->json_output) {
|
|
|
|
// Exclude version and parameters to keep the output file
|
|
|
|
// minimal. The JSON version is inside the "qpdf" key for
|
|
|
|
// version 2.
|
|
|
|
} else {
|
|
|
|
// This version is updated every time a non-backward-compatible
|
|
|
|
// change is made to the JSON format. Clients of the JSON are to
|
|
|
|
// ignore unrecognized keys, so we only update the version of a
|
|
|
|
// key disappears or if its value changes meaning.
|
|
|
|
JSON::writeDictionaryItem(
|
|
|
|
p, first, "version", JSON::makeInt(this->m->json_version), 1);
|
|
|
|
JSON j_params = JSON::makeDictionary();
|
|
|
|
std::string decode_level_str;
|
|
|
|
switch (m->decode_level) {
|
|
|
|
case qpdf_dl_none:
|
|
|
|
decode_level_str = "none";
|
|
|
|
break;
|
|
|
|
case qpdf_dl_generalized:
|
|
|
|
decode_level_str = "generalized";
|
|
|
|
break;
|
|
|
|
case qpdf_dl_specialized:
|
|
|
|
decode_level_str = "specialized";
|
|
|
|
break;
|
|
|
|
case qpdf_dl_all:
|
|
|
|
decode_level_str = "all";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
j_params.addDictionaryMember(
|
|
|
|
"decodelevel", JSON::makeString(decode_level_str));
|
|
|
|
JSON::writeDictionaryItem(p, first, "parameters", j_params, 1);
|
|
|
|
}
|
2022-01-26 19:56:24 +00:00
|
|
|
bool all_keys = m->json_keys.empty();
|
2022-01-07 22:15:15 +00:00
|
|
|
// The list of selectable top-level keys id duplicated in the
|
|
|
|
// following places: job.yml, QPDFJob::json_schema, and
|
2022-05-04 11:32:30 +00:00
|
|
|
// QPDFJob::doJSON.
|
Top-level json: write incrementally
This commit just changes the order in which fields are written to the
json without changing their content. All the json files in the test
suite were modified with this script to ensure that we didn't get any
changes other than ordering.
----------
#!/usr/bin/env python3
import json
import sys
def json_dumps(data):
return json.dumps(data, ensure_ascii=False,
indent=2, separators=(',', ': '))
for filename in sys.argv[1:]:
with open(filename, 'r') as f:
data = json.loads(f.read())
newdata = {}
for i in ('version', 'parameters', 'pages', 'pagelabels',
'acroform', 'attachments', 'encrypt', 'outlines',
'objects', 'objectinfo'):
if i in data:
newdata[i] = data[i]
print(json_dumps(newdata))
----------
2022-05-06 22:25:59 +00:00
|
|
|
|
|
|
|
// We do pages and pagelabels first since they have the side
|
|
|
|
// effect of repairing the pages tree, which could potentially
|
|
|
|
// impact object references in remaining items.
|
2022-04-02 21:14:10 +00:00
|
|
|
if (all_keys || m->json_keys.count("pages")) {
|
Top-level json: write incrementally
This commit just changes the order in which fields are written to the
json without changing their content. All the json files in the test
suite were modified with this script to ensure that we didn't get any
changes other than ordering.
----------
#!/usr/bin/env python3
import json
import sys
def json_dumps(data):
return json.dumps(data, ensure_ascii=False,
indent=2, separators=(',', ': '))
for filename in sys.argv[1:]:
with open(filename, 'r') as f:
data = json.loads(f.read())
newdata = {}
for i in ('version', 'parameters', 'pages', 'pagelabels',
'acroform', 'attachments', 'encrypt', 'outlines',
'objects', 'objectinfo'):
if i in data:
newdata[i] = data[i]
print(json_dumps(newdata))
----------
2022-05-06 22:25:59 +00:00
|
|
|
doJSONPages(p, first, pdf);
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (all_keys || m->json_keys.count("pagelabels")) {
|
Top-level json: write incrementally
This commit just changes the order in which fields are written to the
json without changing their content. All the json files in the test
suite were modified with this script to ensure that we didn't get any
changes other than ordering.
----------
#!/usr/bin/env python3
import json
import sys
def json_dumps(data):
return json.dumps(data, ensure_ascii=False,
indent=2, separators=(',', ': '))
for filename in sys.argv[1:]:
with open(filename, 'r') as f:
data = json.loads(f.read())
newdata = {}
for i in ('version', 'parameters', 'pages', 'pagelabels',
'acroform', 'attachments', 'encrypt', 'outlines',
'objects', 'objectinfo'):
if i in data:
newdata[i] = data[i]
print(json_dumps(newdata))
----------
2022-05-06 22:25:59 +00:00
|
|
|
doJSONPageLabels(p, first, pdf);
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
Top-level json: write incrementally
This commit just changes the order in which fields are written to the
json without changing their content. All the json files in the test
suite were modified with this script to ensure that we didn't get any
changes other than ordering.
----------
#!/usr/bin/env python3
import json
import sys
def json_dumps(data):
return json.dumps(data, ensure_ascii=False,
indent=2, separators=(',', ': '))
for filename in sys.argv[1:]:
with open(filename, 'r') as f:
data = json.loads(f.read())
newdata = {}
for i in ('version', 'parameters', 'pages', 'pagelabels',
'acroform', 'attachments', 'encrypt', 'outlines',
'objects', 'objectinfo'):
if i in data:
newdata[i] = data[i]
print(json_dumps(newdata))
----------
2022-05-06 22:25:59 +00:00
|
|
|
|
|
|
|
// The non-special keys are output in alphabetical order, but the
|
|
|
|
// order doesn't actually matter.
|
2022-04-02 21:14:10 +00:00
|
|
|
if (all_keys || m->json_keys.count("acroform")) {
|
Top-level json: write incrementally
This commit just changes the order in which fields are written to the
json without changing their content. All the json files in the test
suite were modified with this script to ensure that we didn't get any
changes other than ordering.
----------
#!/usr/bin/env python3
import json
import sys
def json_dumps(data):
return json.dumps(data, ensure_ascii=False,
indent=2, separators=(',', ': '))
for filename in sys.argv[1:]:
with open(filename, 'r') as f:
data = json.loads(f.read())
newdata = {}
for i in ('version', 'parameters', 'pages', 'pagelabels',
'acroform', 'attachments', 'encrypt', 'outlines',
'objects', 'objectinfo'):
if i in data:
newdata[i] = data[i]
print(json_dumps(newdata))
----------
2022-05-06 22:25:59 +00:00
|
|
|
doJSONAcroform(p, first, pdf);
|
|
|
|
}
|
|
|
|
if (all_keys || m->json_keys.count("attachments")) {
|
|
|
|
doJSONAttachments(p, first, pdf);
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (all_keys || m->json_keys.count("encrypt")) {
|
Top-level json: write incrementally
This commit just changes the order in which fields are written to the
json without changing their content. All the json files in the test
suite were modified with this script to ensure that we didn't get any
changes other than ordering.
----------
#!/usr/bin/env python3
import json
import sys
def json_dumps(data):
return json.dumps(data, ensure_ascii=False,
indent=2, separators=(',', ': '))
for filename in sys.argv[1:]:
with open(filename, 'r') as f:
data = json.loads(f.read())
newdata = {}
for i in ('version', 'parameters', 'pages', 'pagelabels',
'acroform', 'attachments', 'encrypt', 'outlines',
'objects', 'objectinfo'):
if i in data:
newdata[i] = data[i]
print(json_dumps(newdata))
----------
2022-05-06 22:25:59 +00:00
|
|
|
doJSONEncrypt(p, first, pdf);
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
Top-level json: write incrementally
This commit just changes the order in which fields are written to the
json without changing their content. All the json files in the test
suite were modified with this script to ensure that we didn't get any
changes other than ordering.
----------
#!/usr/bin/env python3
import json
import sys
def json_dumps(data):
return json.dumps(data, ensure_ascii=False,
indent=2, separators=(',', ': '))
for filename in sys.argv[1:]:
with open(filename, 'r') as f:
data = json.loads(f.read())
newdata = {}
for i in ('version', 'parameters', 'pages', 'pagelabels',
'acroform', 'attachments', 'encrypt', 'outlines',
'objects', 'objectinfo'):
if i in data:
newdata[i] = data[i]
print(json_dumps(newdata))
----------
2022-05-06 22:25:59 +00:00
|
|
|
if (all_keys || m->json_keys.count("outlines")) {
|
|
|
|
doJSONOutlines(p, first, pdf);
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
|
2022-05-07 17:33:45 +00:00
|
|
|
// We do objects last so their information is consistent with
|
|
|
|
// repairing the page tree. To see the original file with any page
|
|
|
|
// tree problems and the page tree not flattened, select
|
2022-07-31 00:53:30 +00:00
|
|
|
// qpdf/objects/objectinfo without other keys.
|
|
|
|
if (all_keys || m->json_keys.count("objects") ||
|
|
|
|
m->json_keys.count("qpdf")) {
|
2022-07-31 14:34:05 +00:00
|
|
|
doJSONObjects(p, first, pdf);
|
2022-05-18 22:22:57 +00:00
|
|
|
}
|
2022-05-07 17:33:45 +00:00
|
|
|
if (this->m->json_version == 1) {
|
2022-05-18 22:22:57 +00:00
|
|
|
// "objectinfo" is not needed for version >1 since you can
|
|
|
|
// tell streams from other objects in "objects".
|
2022-05-07 17:33:45 +00:00
|
|
|
if (all_keys || m->json_keys.count("objectinfo")) {
|
|
|
|
doJSONObjectinfo(p, first, pdf);
|
|
|
|
}
|
2022-05-04 11:32:30 +00:00
|
|
|
}
|
|
|
|
|
Top-level json: write incrementally
This commit just changes the order in which fields are written to the
json without changing their content. All the json files in the test
suite were modified with this script to ensure that we didn't get any
changes other than ordering.
----------
#!/usr/bin/env python3
import json
import sys
def json_dumps(data):
return json.dumps(data, ensure_ascii=False,
indent=2, separators=(',', ': '))
for filename in sys.argv[1:]:
with open(filename, 'r') as f:
data = json.loads(f.read())
newdata = {}
for i in ('version', 'parameters', 'pages', 'pagelabels',
'acroform', 'attachments', 'encrypt', 'outlines',
'objects', 'objectinfo'):
if i in data:
newdata[i] = data[i]
print(json_dumps(newdata))
----------
2022-05-06 22:25:59 +00:00
|
|
|
JSON::writeDictionaryClose(p, first, 0);
|
|
|
|
*p << "\n";
|
2022-01-03 15:16:28 +00:00
|
|
|
|
2022-05-06 21:49:28 +00:00
|
|
|
if (this->m->test_json_schema) {
|
|
|
|
// Check against schema
|
2022-05-07 15:38:04 +00:00
|
|
|
JSON schema = json_schema(this->m->json_version, &m->json_keys);
|
2022-05-06 21:49:28 +00:00
|
|
|
std::list<std::string> errors;
|
|
|
|
JSON captured = JSON::parse(captured_json);
|
|
|
|
if (!captured.checkSchema(schema, errors)) {
|
2022-06-05 17:30:42 +00:00
|
|
|
this->m->log->error("QPDFJob didn't create JSON that complies with "
|
|
|
|
"its own rules.\n");
|
2022-05-06 21:49:28 +00:00
|
|
|
for (auto const& error: errors) {
|
2022-06-05 17:30:42 +00:00
|
|
|
*this->m->log->getError() << error << "\n";
|
2022-05-06 21:49:28 +00:00
|
|
|
}
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 17:16:16 +00:00
|
|
|
void
|
|
|
|
QPDFJob::doInspection(QPDF& pdf)
|
2022-01-03 15:16:28 +00:00
|
|
|
{
|
2022-06-05 17:30:42 +00:00
|
|
|
auto& cout = *this->m->log->getInfo();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->check) {
|
2022-01-03 17:16:16 +00:00
|
|
|
doCheck(pdf);
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->show_npages) {
|
2022-02-01 12:49:00 +00:00
|
|
|
QTC::TC("qpdf", "QPDFJob npages");
|
2022-06-05 17:30:42 +00:00
|
|
|
cout << pdf.getRoot().getKey("/Pages").getKey("/Count").getIntValue()
|
|
|
|
<< "\n";
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->show_encryption) {
|
2022-01-03 17:16:16 +00:00
|
|
|
showEncryption(pdf);
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->check_linearization) {
|
|
|
|
if (!pdf.isLinearized()) {
|
2022-06-05 17:30:42 +00:00
|
|
|
cout << m->infilename.get() << " is not linearized\n";
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (pdf.checkLinearization()) {
|
2022-06-05 17:30:42 +00:00
|
|
|
cout << m->infilename.get() << ": no linearization errors\n";
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-01-05 21:50:18 +00:00
|
|
|
this->m->warnings = true;
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->show_linearization) {
|
|
|
|
if (pdf.isLinearized()) {
|
2022-01-05 21:50:18 +00:00
|
|
|
pdf.showLinearizationData();
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-06-05 17:30:42 +00:00
|
|
|
cout << m->infilename.get() << " is not linearized\n";
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->show_xref) {
|
2022-01-05 21:50:18 +00:00
|
|
|
pdf.showXRefTable();
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if ((m->show_obj > 0) || m->show_trailer) {
|
2022-01-05 21:50:18 +00:00
|
|
|
doShowObj(pdf);
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->show_pages) {
|
2022-01-05 21:50:18 +00:00
|
|
|
doShowPages(pdf);
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->list_attachments) {
|
2022-01-05 21:50:18 +00:00
|
|
|
doListAttachments(pdf);
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!m->attachment_to_show.empty()) {
|
2022-01-05 21:50:18 +00:00
|
|
|
doShowAttachment(pdf);
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!pdf.getWarnings().empty()) {
|
2022-01-05 21:50:18 +00:00
|
|
|
this->m->warnings = true;
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-22 22:37:51 +00:00
|
|
|
std::shared_ptr<QPDF>
|
2022-01-05 21:13:43 +00:00
|
|
|
QPDFJob::doProcessOnce(
|
2022-01-05 21:06:10 +00:00
|
|
|
std::function<void(QPDF*, char const*)> fn,
|
2022-04-02 21:14:10 +00:00
|
|
|
char const* password,
|
|
|
|
bool empty,
|
2022-05-18 22:22:57 +00:00
|
|
|
bool used_for_input,
|
|
|
|
bool main_input)
|
2022-01-03 15:16:28 +00:00
|
|
|
{
|
2022-01-22 22:37:51 +00:00
|
|
|
auto pdf = std::make_shared<QPDF>();
|
2022-01-05 21:13:43 +00:00
|
|
|
setQPDFOptions(*pdf);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (empty) {
|
2022-05-18 22:22:57 +00:00
|
|
|
pdf->emptyPDF();
|
|
|
|
} else if (main_input && this->m->json_input) {
|
|
|
|
pdf->createFromJSON(this->m->infilename.get());
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-01-22 22:37:51 +00:00
|
|
|
fn(pdf.get(), password);
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (used_for_input) {
|
2022-02-08 18:38:03 +00:00
|
|
|
this->m->max_input_version.updateIfGreater(
|
|
|
|
pdf->getVersionAsPDFVersion());
|
|
|
|
}
|
2022-01-03 15:16:28 +00:00
|
|
|
return pdf;
|
|
|
|
}
|
|
|
|
|
2022-01-22 22:37:51 +00:00
|
|
|
std::shared_ptr<QPDF>
|
2022-01-05 21:13:43 +00:00
|
|
|
QPDFJob::doProcess(
|
2022-01-05 21:06:10 +00:00
|
|
|
std::function<void(QPDF*, char const*)> fn,
|
2022-04-02 21:14:10 +00:00
|
|
|
char const* password,
|
|
|
|
bool empty,
|
2022-05-18 22:22:57 +00:00
|
|
|
bool used_for_input,
|
|
|
|
bool main_input)
|
2022-01-03 15:16:28 +00:00
|
|
|
{
|
|
|
|
// If a password has been specified but doesn't work, try other
|
|
|
|
// passwords that are equivalent in different character encodings.
|
|
|
|
// This makes it possible to open PDF files that were encrypted
|
|
|
|
// using incorrect string encodings. For example, if someone used
|
|
|
|
// a password encoded in PDF Doc encoding or Windows code page
|
|
|
|
// 1252 for an AES-encrypted file or a UTF-8-encoded password on
|
|
|
|
// an RC4-encrypted file, or if the password was properly encoded
|
2022-01-06 20:33:29 +00:00
|
|
|
// but the password given here was incorrectly encoded, there's a
|
2022-01-03 15:16:28 +00:00
|
|
|
// good chance we'd succeed here.
|
|
|
|
|
|
|
|
std::string ptemp;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (password && (!m->password_is_hex_key)) {
|
|
|
|
if (m->password_mode == QPDFJob::pm_hex_bytes) {
|
2022-01-03 15:16:28 +00:00
|
|
|
// Special case: handle --password-mode=hex-bytes for input
|
|
|
|
// password as well as output password
|
2022-02-01 12:49:00 +00:00
|
|
|
QTC::TC("qpdf", "QPDFJob input password hex-bytes");
|
2022-01-03 15:16:28 +00:00
|
|
|
ptemp = QUtil::hex_decode(password);
|
|
|
|
password = ptemp.c_str();
|
|
|
|
}
|
|
|
|
}
|
2022-07-26 11:37:50 +00:00
|
|
|
if ((password == nullptr) || empty || m->password_is_hex_key ||
|
2022-04-02 21:14:10 +00:00
|
|
|
m->suppress_password_recovery) {
|
2022-01-03 15:16:28 +00:00
|
|
|
// There is no password, or we're not doing recovery, so just
|
|
|
|
// do the normal processing with the supplied password.
|
2022-05-18 22:22:57 +00:00
|
|
|
return doProcessOnce(fn, password, empty, used_for_input, main_input);
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get a list of otherwise encoded strings. Keep in scope for this
|
|
|
|
// method.
|
|
|
|
std::vector<std::string> passwords_str =
|
|
|
|
QUtil::possible_repaired_encodings(password);
|
|
|
|
// Represent to char const*, as required by the QPDF class.
|
|
|
|
std::vector<char const*> passwords;
|
2022-04-30 17:23:18 +00:00
|
|
|
for (auto const& iter: passwords_str) {
|
|
|
|
passwords.push_back(iter.c_str());
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
// We always try the supplied password first because it is the
|
|
|
|
// first string returned by possible_repaired_encodings. If there
|
|
|
|
// is more than one option, go ahead and put the supplied password
|
|
|
|
// at the end so that it's that decoding attempt whose exception
|
|
|
|
// is thrown.
|
2022-04-02 21:14:10 +00:00
|
|
|
if (passwords.size() > 1) {
|
2022-01-03 15:16:28 +00:00
|
|
|
passwords.push_back(password);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try each password. If one works, return the resulting object.
|
|
|
|
// If they all fail, throw the exception thrown by the final
|
|
|
|
// attempt, which, like the first attempt, will be with the
|
|
|
|
// supplied password.
|
|
|
|
bool warned = false;
|
2022-04-30 17:23:18 +00:00
|
|
|
for (auto iter = passwords.begin(); iter != passwords.end(); ++iter) {
|
2022-04-02 21:14:10 +00:00
|
|
|
try {
|
2022-05-18 22:22:57 +00:00
|
|
|
return doProcessOnce(fn, *iter, empty, used_for_input, main_input);
|
2022-04-02 21:14:10 +00:00
|
|
|
} catch (QPDFExc& e) {
|
2022-04-30 17:23:18 +00:00
|
|
|
auto next = iter;
|
2022-01-03 15:16:28 +00:00
|
|
|
++next;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (next == passwords.end()) {
|
2022-01-03 15:16:28 +00:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!warned) {
|
2022-01-03 15:16:28 +00:00
|
|
|
warned = true;
|
2022-06-05 17:30:42 +00:00
|
|
|
doIfVerbose([&](Pipeline& v, std::string const& prefix) {
|
|
|
|
v << prefix << ": supplied password didn't work;"
|
|
|
|
<< " trying other passwords based on interpreting"
|
|
|
|
<< " password with different string encodings\n";
|
2022-01-03 17:16:16 +00:00
|
|
|
});
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Should not be reachable
|
|
|
|
throw std::logic_error("do_process returned");
|
|
|
|
}
|
|
|
|
|
2022-01-22 22:37:51 +00:00
|
|
|
std::shared_ptr<QPDF>
|
2022-04-02 21:14:10 +00:00
|
|
|
QPDFJob::processFile(
|
2022-05-18 22:22:57 +00:00
|
|
|
char const* filename,
|
|
|
|
char const* password,
|
|
|
|
bool used_for_input,
|
|
|
|
bool main_input)
|
2022-01-03 15:16:28 +00:00
|
|
|
{
|
2022-01-05 21:06:10 +00:00
|
|
|
auto f1 = std::mem_fn<void(char const*, char const*)>(&QPDF::processFile);
|
2022-04-02 21:14:10 +00:00
|
|
|
auto fn =
|
|
|
|
std::bind(f1, std::placeholders::_1, filename, std::placeholders::_2);
|
2022-05-18 22:22:57 +00:00
|
|
|
return doProcess(
|
|
|
|
fn, password, strcmp(filename, "") == 0, used_for_input, main_input);
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
|
2022-01-22 22:37:51 +00:00
|
|
|
std::shared_ptr<QPDF>
|
2022-01-05 21:13:43 +00:00
|
|
|
QPDFJob::processInputSource(
|
2022-04-09 18:35:56 +00:00
|
|
|
std::shared_ptr<InputSource> is, char const* password, bool used_for_input)
|
2022-01-03 15:16:28 +00:00
|
|
|
{
|
2022-01-05 21:06:10 +00:00
|
|
|
auto f1 = std::mem_fn(&QPDF::processInputSource);
|
2022-04-02 21:14:10 +00:00
|
|
|
auto fn = std::bind(f1, std::placeholders::_1, is, std::placeholders::_2);
|
2022-05-18 22:22:57 +00:00
|
|
|
return doProcess(fn, password, false, used_for_input, false);
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-01-26 14:09:29 +00:00
|
|
|
QPDFJob::validateUnderOverlay(QPDF& pdf, UnderOverlay* uo)
|
2022-01-03 15:16:28 +00:00
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (uo->filename.empty()) {
|
2022-01-03 15:16:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
QPDFPageDocumentHelper main_pdh(pdf);
|
|
|
|
int main_npages = QIntC::to_int(main_pdh.getAllPages().size());
|
2022-05-18 22:22:57 +00:00
|
|
|
uo->pdf =
|
|
|
|
processFile(uo->filename.c_str(), uo->password.get(), true, false);
|
2022-01-03 15:16:28 +00:00
|
|
|
QPDFPageDocumentHelper uo_pdh(*(uo->pdf));
|
|
|
|
int uo_npages = QIntC::to_int(uo_pdh.getAllPages().size());
|
2022-04-02 21:14:10 +00:00
|
|
|
try {
|
2022-01-22 23:46:46 +00:00
|
|
|
uo->to_pagenos = QUtil::parse_numrange(uo->to_nr.c_str(), main_npages);
|
2022-04-02 21:14:10 +00:00
|
|
|
} catch (std::runtime_error& e) {
|
2022-01-03 15:16:28 +00:00
|
|
|
throw std::runtime_error(
|
|
|
|
"parsing numeric range for " + uo->which +
|
|
|
|
" \"to\" pages: " + e.what());
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
try {
|
|
|
|
if (uo->from_nr.empty()) {
|
2022-02-01 12:49:00 +00:00
|
|
|
QTC::TC("qpdf", "QPDFJob from_nr from repeat_nr");
|
2022-01-03 15:16:28 +00:00
|
|
|
uo->from_nr = uo->repeat_nr;
|
|
|
|
}
|
2022-01-22 23:46:46 +00:00
|
|
|
uo->from_pagenos =
|
|
|
|
QUtil::parse_numrange(uo->from_nr.c_str(), uo_npages);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!uo->repeat_nr.empty()) {
|
2022-01-03 15:16:28 +00:00
|
|
|
uo->repeat_pagenos =
|
2022-01-22 23:46:46 +00:00
|
|
|
QUtil::parse_numrange(uo->repeat_nr.c_str(), uo_npages);
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} catch (std::runtime_error& e) {
|
2022-01-03 15:16:28 +00:00
|
|
|
throw std::runtime_error(
|
2022-04-02 21:14:10 +00:00
|
|
|
"parsing numeric range for " + uo->which + " file " + uo->filename +
|
|
|
|
": " + e.what());
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
static QPDFAcroFormDocumentHelper*
|
|
|
|
get_afdh_for_qpdf(
|
|
|
|
std::map<unsigned long long, std::shared_ptr<QPDFAcroFormDocumentHelper>>&
|
|
|
|
afdh_map,
|
2022-01-03 15:16:28 +00:00
|
|
|
QPDF* q)
|
|
|
|
{
|
|
|
|
auto uid = q->getUniqueId();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!afdh_map.count(uid)) {
|
2022-01-22 22:37:51 +00:00
|
|
|
afdh_map[uid] = std::make_shared<QPDFAcroFormDocumentHelper>(*q);
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-01-22 22:37:51 +00:00
|
|
|
return afdh_map[uid].get();
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 21:13:43 +00:00
|
|
|
void
|
|
|
|
QPDFJob::doUnderOverlayForPage(
|
2022-01-03 15:16:28 +00:00
|
|
|
QPDF& pdf,
|
2022-01-26 14:09:29 +00:00
|
|
|
UnderOverlay& uo,
|
2022-04-02 21:14:10 +00:00
|
|
|
std::map<int, std::vector<int>>& pagenos,
|
2022-01-03 15:16:28 +00:00
|
|
|
size_t page_idx,
|
|
|
|
std::map<int, QPDFObjectHandle>& fo,
|
|
|
|
std::vector<QPDFPageObjectHelper>& pages,
|
|
|
|
QPDFPageObjectHelper& dest_page,
|
|
|
|
bool before)
|
|
|
|
{
|
|
|
|
int pageno = 1 + QIntC::to_int(page_idx);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!pagenos.count(pageno)) {
|
2022-01-03 15:16:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
std::map<unsigned long long, std::shared_ptr<QPDFAcroFormDocumentHelper>>
|
|
|
|
afdh;
|
2022-01-03 15:16:28 +00:00
|
|
|
auto make_afdh = [&](QPDFPageObjectHelper& ph) {
|
|
|
|
QPDF* q = ph.getObjectHandle().getOwningQPDF();
|
|
|
|
return get_afdh_for_qpdf(afdh, q);
|
|
|
|
};
|
|
|
|
auto dest_afdh = make_afdh(dest_page);
|
|
|
|
|
|
|
|
std::string content;
|
|
|
|
int min_suffix = 1;
|
|
|
|
QPDFObjectHandle resources = dest_page.getAttribute("/Resources", true);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!resources.isDictionary()) {
|
2022-02-01 12:49:00 +00:00
|
|
|
QTC::TC("qpdf", "QPDFJob overlay page with no resources");
|
2022-07-24 19:42:23 +00:00
|
|
|
resources = dest_page.getObjectHandle().replaceKeyAndGetNew(
|
2022-04-30 00:21:07 +00:00
|
|
|
"/Resources", QPDFObjectHandle::newDictionary());
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-04-30 17:23:18 +00:00
|
|
|
for (int from_pageno: pagenos[pageno]) {
|
2022-06-05 17:30:42 +00:00
|
|
|
doIfVerbose([&](Pipeline& v, std::string const& prefix) {
|
|
|
|
v << " " << uo.which << " " << from_pageno << "\n";
|
2022-01-03 17:16:16 +00:00
|
|
|
});
|
2022-01-03 15:16:28 +00:00
|
|
|
auto from_page = pages.at(QIntC::to_size(from_pageno - 1));
|
2022-04-02 21:14:10 +00:00
|
|
|
if (0 == fo.count(from_pageno)) {
|
2022-01-03 15:16:28 +00:00
|
|
|
fo[from_pageno] =
|
2022-04-02 21:14:10 +00:00
|
|
|
pdf.copyForeignObject(from_page.getFormXObjectForPage());
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the same page is overlaid or underlaid multiple times,
|
|
|
|
// we'll generate multiple names for it, but that's harmless
|
|
|
|
// and also a pretty goofy case that's not worth coding
|
|
|
|
// around.
|
|
|
|
std::string name = resources.getUniqueResourceName("/Fx", min_suffix);
|
|
|
|
QPDFMatrix cm;
|
|
|
|
std::string new_content = dest_page.placeFormXObject(
|
2022-04-02 21:14:10 +00:00
|
|
|
fo[from_pageno],
|
|
|
|
name,
|
|
|
|
dest_page.getTrimBox().getArrayAsRectangle(),
|
|
|
|
cm);
|
2022-01-03 15:16:28 +00:00
|
|
|
dest_page.copyAnnotations(
|
|
|
|
from_page, cm, dest_afdh, make_afdh(from_page));
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!new_content.empty()) {
|
2022-02-05 14:18:58 +00:00
|
|
|
resources.mergeResources("<< /XObject << >> >>"_qpdf);
|
2022-01-03 15:16:28 +00:00
|
|
|
auto xobject = resources.getKey("/XObject");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (xobject.isDictionary()) {
|
2022-01-03 15:16:28 +00:00
|
|
|
xobject.replaceKey(name, fo[from_pageno]);
|
|
|
|
}
|
|
|
|
++min_suffix;
|
|
|
|
content += new_content;
|
|
|
|
}
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!content.empty()) {
|
|
|
|
if (before) {
|
2022-01-03 15:16:28 +00:00
|
|
|
dest_page.addPageContents(
|
|
|
|
QPDFObjectHandle::newStream(&pdf, content), true);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-01-03 15:16:28 +00:00
|
|
|
dest_page.addPageContents(
|
|
|
|
QPDFObjectHandle::newStream(&pdf, "q\n"), true);
|
|
|
|
dest_page.addPageContents(
|
|
|
|
QPDFObjectHandle::newStream(&pdf, "\nQ\n" + content), false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-26 21:40:14 +00:00
|
|
|
void
|
2022-04-02 21:14:10 +00:00
|
|
|
QPDFJob::getUOPagenos(
|
|
|
|
QPDFJob::UnderOverlay& uo, std::map<int, std::vector<int>>& pagenos)
|
2022-01-05 21:50:18 +00:00
|
|
|
{
|
|
|
|
size_t idx = 0;
|
|
|
|
size_t from_size = uo.from_pagenos.size();
|
|
|
|
size_t repeat_size = uo.repeat_pagenos.size();
|
2022-04-30 17:23:18 +00:00
|
|
|
for (int to_pageno: uo.to_pagenos) {
|
2022-04-02 21:14:10 +00:00
|
|
|
if (idx < from_size) {
|
2022-04-30 17:23:18 +00:00
|
|
|
pagenos[to_pageno].push_back(uo.from_pagenos.at(idx));
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (repeat_size) {
|
2022-04-30 17:23:18 +00:00
|
|
|
pagenos[to_pageno].push_back(
|
2022-01-05 21:50:18 +00:00
|
|
|
uo.repeat_pagenos.at((idx - from_size) % repeat_size));
|
|
|
|
}
|
2022-04-30 17:23:18 +00:00
|
|
|
++idx;
|
2022-01-05 21:50:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 15:16:28 +00:00
|
|
|
void
|
|
|
|
QPDFJob::handleUnderOverlay(QPDF& pdf)
|
|
|
|
{
|
2022-01-26 19:56:24 +00:00
|
|
|
validateUnderOverlay(pdf, &m->underlay);
|
|
|
|
validateUnderOverlay(pdf, &m->overlay);
|
2022-08-07 09:33:25 +00:00
|
|
|
if ((nullptr == m->underlay.pdf) && (nullptr == m->overlay.pdf)) {
|
2022-01-03 15:16:28 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
std::map<int, std::vector<int>> underlay_pagenos;
|
2022-01-26 21:40:14 +00:00
|
|
|
getUOPagenos(m->underlay, underlay_pagenos);
|
2022-04-02 21:14:10 +00:00
|
|
|
std::map<int, std::vector<int>> overlay_pagenos;
|
2022-01-26 21:40:14 +00:00
|
|
|
getUOPagenos(m->overlay, overlay_pagenos);
|
2022-01-03 15:16:28 +00:00
|
|
|
std::map<int, QPDFObjectHandle> underlay_fo;
|
|
|
|
std::map<int, QPDFObjectHandle> overlay_fo;
|
|
|
|
std::vector<QPDFPageObjectHelper> upages;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->underlay.pdf.get()) {
|
2022-01-26 19:56:24 +00:00
|
|
|
upages = QPDFPageDocumentHelper(*(m->underlay.pdf)).getAllPages();
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
std::vector<QPDFPageObjectHelper> opages;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->overlay.pdf.get()) {
|
2022-01-26 19:56:24 +00:00
|
|
|
opages = QPDFPageDocumentHelper(*(m->overlay.pdf)).getAllPages();
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QPDFPageDocumentHelper main_pdh(pdf);
|
|
|
|
std::vector<QPDFPageObjectHelper> main_pages = main_pdh.getAllPages();
|
|
|
|
size_t main_npages = main_pages.size();
|
2022-06-05 17:30:42 +00:00
|
|
|
doIfVerbose([&](Pipeline& v, std::string const& prefix) {
|
|
|
|
v << prefix << ": processing underlay/overlay\n";
|
2022-01-03 17:16:16 +00:00
|
|
|
});
|
2022-04-02 21:14:10 +00:00
|
|
|
for (size_t i = 0; i < main_npages; ++i) {
|
2022-06-05 17:30:42 +00:00
|
|
|
doIfVerbose([&](Pipeline& v, std::string const& prefix) {
|
|
|
|
v << " page " << 1 + i << "\n";
|
2022-01-03 17:16:16 +00:00
|
|
|
});
|
2022-04-02 21:14:10 +00:00
|
|
|
doUnderOverlayForPage(
|
|
|
|
pdf,
|
|
|
|
m->underlay,
|
|
|
|
underlay_pagenos,
|
|
|
|
i,
|
|
|
|
underlay_fo,
|
|
|
|
upages,
|
|
|
|
main_pages.at(i),
|
|
|
|
true);
|
|
|
|
doUnderOverlayForPage(
|
|
|
|
pdf,
|
|
|
|
m->overlay,
|
|
|
|
overlay_pagenos,
|
|
|
|
i,
|
|
|
|
overlay_fo,
|
|
|
|
opages,
|
|
|
|
main_pages.at(i),
|
|
|
|
false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
maybe_set_pagemode(QPDF& pdf, std::string const& pagemode)
|
2022-01-03 15:16:28 +00:00
|
|
|
{
|
|
|
|
auto root = pdf.getRoot();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (root.getKey("/PageMode").isNull()) {
|
2022-01-03 15:16:28 +00:00
|
|
|
root.replaceKey("/PageMode", QPDFObjectHandle::newName(pagemode));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFJob::addAttachments(QPDF& pdf)
|
|
|
|
{
|
|
|
|
maybe_set_pagemode(pdf, "/UseAttachments");
|
|
|
|
QPDFEmbeddedFileDocumentHelper efdh(pdf);
|
|
|
|
std::vector<std::string> duplicated_keys;
|
2022-04-30 13:43:07 +00:00
|
|
|
for (auto const& to_add: m->attachments_to_add) {
|
2022-04-02 21:14:10 +00:00
|
|
|
if ((!to_add.replace) && efdh.getEmbeddedFile(to_add.key)) {
|
2022-01-03 15:16:28 +00:00
|
|
|
duplicated_keys.push_back(to_add.key);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto fs = QPDFFileSpecObjectHelper::createFileSpec(
|
|
|
|
pdf, to_add.filename, to_add.path);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!to_add.description.empty()) {
|
2022-01-03 15:16:28 +00:00
|
|
|
fs.setDescription(to_add.description);
|
|
|
|
}
|
|
|
|
auto efs = QPDFEFStreamObjectHelper(fs.getEmbeddedFileStream());
|
2022-04-02 21:14:10 +00:00
|
|
|
efs.setCreationDate(to_add.creationdate).setModDate(to_add.moddate);
|
|
|
|
if (!to_add.mimetype.empty()) {
|
2022-01-03 15:16:28 +00:00
|
|
|
efs.setSubtype(to_add.mimetype);
|
|
|
|
}
|
|
|
|
|
|
|
|
efdh.replaceEmbeddedFile(to_add.key, fs);
|
2022-06-05 17:30:42 +00:00
|
|
|
doIfVerbose([&](Pipeline& v, std::string const& prefix) {
|
|
|
|
v << prefix << ": attached " << to_add.path << " as "
|
|
|
|
<< to_add.filename << " with key " << to_add.key << "\n";
|
2022-01-03 17:16:16 +00:00
|
|
|
});
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!duplicated_keys.empty()) {
|
2022-01-03 15:16:28 +00:00
|
|
|
std::string message;
|
2022-04-30 13:43:07 +00:00
|
|
|
for (auto const& k: duplicated_keys) {
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!message.empty()) {
|
2022-01-03 15:16:28 +00:00
|
|
|
message += ", ";
|
|
|
|
}
|
|
|
|
message += k;
|
|
|
|
}
|
|
|
|
message = pdf.getFilename() +
|
2022-04-02 21:14:10 +00:00
|
|
|
" already has attachments with the following keys: " + message +
|
2022-07-31 00:53:30 +00:00
|
|
|
"; use --replace to replace or --key to specify a different "
|
|
|
|
"key";
|
2022-01-03 15:16:28 +00:00
|
|
|
throw std::runtime_error(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFJob::copyAttachments(QPDF& pdf)
|
|
|
|
{
|
|
|
|
maybe_set_pagemode(pdf, "/UseAttachments");
|
|
|
|
QPDFEmbeddedFileDocumentHelper efdh(pdf);
|
|
|
|
std::vector<std::string> duplicates;
|
2022-04-30 13:43:07 +00:00
|
|
|
for (auto const& to_copy: m->attachments_to_copy) {
|
2022-06-05 17:30:42 +00:00
|
|
|
doIfVerbose([&](Pipeline& v, std::string const& prefix) {
|
|
|
|
v << prefix << ": copying attachments from " << to_copy.path
|
|
|
|
<< "\n";
|
2022-01-03 17:16:16 +00:00
|
|
|
});
|
2022-05-18 22:22:57 +00:00
|
|
|
auto other = processFile(
|
|
|
|
to_copy.path.c_str(), to_copy.password.c_str(), false, false);
|
2022-01-03 15:16:28 +00:00
|
|
|
QPDFEmbeddedFileDocumentHelper other_efdh(*other);
|
|
|
|
auto other_attachments = other_efdh.getEmbeddedFiles();
|
2022-04-30 13:43:07 +00:00
|
|
|
for (auto const& iter: other_attachments) {
|
2022-01-03 15:16:28 +00:00
|
|
|
std::string new_key = to_copy.prefix + iter.first;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (efdh.getEmbeddedFile(new_key)) {
|
2022-01-03 15:16:28 +00:00
|
|
|
duplicates.push_back(
|
|
|
|
"file: " + to_copy.path + ", key: " + new_key);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
|
|
|
auto new_fs_oh =
|
|
|
|
pdf.copyForeignObject(iter.second->getObjectHandle());
|
2022-01-03 15:16:28 +00:00
|
|
|
efdh.replaceEmbeddedFile(
|
|
|
|
new_key, QPDFFileSpecObjectHelper(new_fs_oh));
|
2022-06-05 17:30:42 +00:00
|
|
|
doIfVerbose([&](Pipeline& v, std::string const& prefix) {
|
|
|
|
v << " " << iter.first << " -> " << new_key << "\n";
|
2022-01-03 17:16:16 +00:00
|
|
|
});
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (other->anyWarnings()) {
|
2022-01-03 15:16:28 +00:00
|
|
|
this->m->warnings = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!duplicates.empty()) {
|
2022-01-03 15:16:28 +00:00
|
|
|
std::string message;
|
2022-04-30 13:43:07 +00:00
|
|
|
for (auto const& i: duplicates) {
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!message.empty()) {
|
2022-01-03 15:16:28 +00:00
|
|
|
message += "; ";
|
|
|
|
}
|
|
|
|
message += i;
|
|
|
|
}
|
|
|
|
message = pdf.getFilename() +
|
|
|
|
" already has attachments with keys that conflict with"
|
2022-04-02 21:14:10 +00:00
|
|
|
" attachments from other files: " +
|
|
|
|
message +
|
2022-01-03 15:16:28 +00:00
|
|
|
". Use --prefix with --copy-attachments-from"
|
|
|
|
" or manually copy individual attachments.";
|
|
|
|
throw std::runtime_error(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFJob::handleTransformations(QPDF& pdf)
|
|
|
|
{
|
|
|
|
QPDFPageDocumentHelper dh(pdf);
|
2022-01-22 22:37:51 +00:00
|
|
|
std::shared_ptr<QPDFAcroFormDocumentHelper> afdh;
|
2022-01-03 15:16:28 +00:00
|
|
|
auto make_afdh = [&]() {
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!afdh.get()) {
|
2022-01-22 22:37:51 +00:00
|
|
|
afdh = std::make_shared<QPDFAcroFormDocumentHelper>(pdf);
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
};
|
2022-01-26 19:56:24 +00:00
|
|
|
if (m->externalize_inline_images ||
|
2022-04-02 21:14:10 +00:00
|
|
|
(m->optimize_images && (!m->keep_inline_images))) {
|
2022-05-21 14:18:15 +00:00
|
|
|
for (auto& ph: dh.getAllPages()) {
|
2022-01-26 19:56:24 +00:00
|
|
|
ph.externalizeInlineImages(m->ii_min_bytes);
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->optimize_images) {
|
2022-01-03 15:16:28 +00:00
|
|
|
int pageno = 0;
|
2022-05-21 14:18:15 +00:00
|
|
|
for (auto& ph: dh.getAllPages()) {
|
2022-01-03 15:16:28 +00:00
|
|
|
++pageno;
|
|
|
|
QPDFObjectHandle page = ph.getObjectHandle();
|
2022-05-21 14:18:15 +00:00
|
|
|
for (auto& iter2: ph.getImages()) {
|
2022-01-03 15:16:28 +00:00
|
|
|
std::string name = iter2.first;
|
|
|
|
QPDFObjectHandle& image = iter2.second;
|
2022-01-26 21:40:14 +00:00
|
|
|
ImageOptimizer* io = new ImageOptimizer(
|
2022-04-02 21:14:10 +00:00
|
|
|
*this,
|
|
|
|
m->oi_min_width,
|
|
|
|
m->oi_min_height,
|
|
|
|
m->oi_min_area,
|
|
|
|
image);
|
2022-04-09 18:35:56 +00:00
|
|
|
std::shared_ptr<QPDFObjectHandle::StreamDataProvider> sdp(io);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (io->evaluate(
|
|
|
|
"image " + name + " on page " +
|
|
|
|
QUtil::int_to_string(pageno))) {
|
2022-01-03 15:16:28 +00:00
|
|
|
QPDFObjectHandle new_image =
|
|
|
|
QPDFObjectHandle::newStream(&pdf);
|
|
|
|
new_image.replaceDict(image.getDict().shallowCopy());
|
|
|
|
new_image.replaceStreamData(
|
|
|
|
sdp,
|
|
|
|
QPDFObjectHandle::newName("/DCTDecode"),
|
|
|
|
QPDFObjectHandle::newNull());
|
2022-04-02 21:14:10 +00:00
|
|
|
ph.getAttribute("/Resources", true)
|
|
|
|
.getKey("/XObject")
|
|
|
|
.replaceKey(name, new_image);
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->generate_appearances) {
|
2022-01-03 15:16:28 +00:00
|
|
|
make_afdh();
|
|
|
|
afdh->generateAppearancesIfNeeded();
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->flatten_annotations) {
|
|
|
|
dh.flattenAnnotations(
|
|
|
|
m->flatten_annotations_required, m->flatten_annotations_forbidden);
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->coalesce_contents) {
|
2022-05-21 14:18:15 +00:00
|
|
|
for (auto& page: dh.getAllPages()) {
|
2022-04-30 17:23:18 +00:00
|
|
|
page.coalesceContentStreams();
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->flatten_rotation) {
|
2022-01-03 15:16:28 +00:00
|
|
|
make_afdh();
|
2022-04-30 13:43:07 +00:00
|
|
|
for (auto& page: dh.getAllPages()) {
|
2022-01-22 22:37:51 +00:00
|
|
|
page.flattenRotation(afdh.get());
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->remove_page_labels) {
|
2022-01-03 15:16:28 +00:00
|
|
|
pdf.getRoot().removeKey("/PageLabels");
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!m->attachments_to_remove.empty()) {
|
2022-01-03 15:16:28 +00:00
|
|
|
QPDFEmbeddedFileDocumentHelper efdh(pdf);
|
2022-04-30 13:43:07 +00:00
|
|
|
for (auto const& key: m->attachments_to_remove) {
|
2022-04-02 21:14:10 +00:00
|
|
|
if (efdh.removeEmbeddedFile(key)) {
|
2022-06-05 17:30:42 +00:00
|
|
|
doIfVerbose([&](Pipeline& v, std::string const& prefix) {
|
|
|
|
v << prefix << ": removed attachment " << key << "\n";
|
2022-01-03 17:16:16 +00:00
|
|
|
});
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-01-03 15:16:28 +00:00
|
|
|
throw std::runtime_error("attachment " + key + " not found");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!m->attachments_to_add.empty()) {
|
2022-01-03 15:16:28 +00:00
|
|
|
addAttachments(pdf);
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!m->attachments_to_copy.empty()) {
|
2022-01-03 15:16:28 +00:00
|
|
|
copyAttachments(pdf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-05 21:13:43 +00:00
|
|
|
bool
|
|
|
|
QPDFJob::shouldRemoveUnreferencedResources(QPDF& pdf)
|
2022-01-03 15:16:28 +00:00
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->remove_unreferenced_page_resources == QPDFJob::re_no) {
|
2022-01-03 15:16:28 +00:00
|
|
|
return false;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (m->remove_unreferenced_page_resources == QPDFJob::re_yes) {
|
2022-01-03 15:16:28 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unreferenced resources are common in files where resources
|
|
|
|
// dictionaries are shared across pages. As a heuristic, we look
|
|
|
|
// in the file for shared resources dictionaries or shared XObject
|
|
|
|
// subkeys of resources dictionaries either on pages or on form
|
|
|
|
// XObjects in pages. If we find any, then there is a higher
|
|
|
|
// likelihood that the expensive process of finding unreferenced
|
|
|
|
// resources is worth it.
|
|
|
|
|
|
|
|
// Return true as soon as we find any shared resources.
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
std::set<QPDFObjGen> resources_seen; // shared resources detection
|
|
|
|
std::set<QPDFObjGen> nodes_seen; // loop detection
|
2022-01-03 15:16:28 +00:00
|
|
|
|
2022-06-05 17:30:42 +00:00
|
|
|
doIfVerbose([&](Pipeline& v, std::string const& prefix) {
|
|
|
|
v << prefix << ": " << pdf.getFilename()
|
|
|
|
<< ": checking for shared resources\n";
|
2022-01-03 17:16:16 +00:00
|
|
|
});
|
2022-01-03 15:16:28 +00:00
|
|
|
|
|
|
|
std::list<QPDFObjectHandle> queue;
|
|
|
|
queue.push_back(pdf.getRoot().getKey("/Pages"));
|
2022-04-02 21:14:10 +00:00
|
|
|
while (!queue.empty()) {
|
2022-01-03 15:16:28 +00:00
|
|
|
QPDFObjectHandle node = *queue.begin();
|
|
|
|
queue.pop_front();
|
|
|
|
QPDFObjGen og = node.getObjGen();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (nodes_seen.count(og)) {
|
2022-01-03 15:16:28 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
nodes_seen.insert(og);
|
|
|
|
QPDFObjectHandle dict = node.isStream() ? node.getDict() : node;
|
|
|
|
QPDFObjectHandle kids = dict.getKey("/Kids");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (kids.isArray()) {
|
2022-01-03 15:16:28 +00:00
|
|
|
// This is a non-leaf node.
|
2022-04-02 21:14:10 +00:00
|
|
|
if (dict.hasKey("/Resources")) {
|
2022-02-01 12:49:00 +00:00
|
|
|
QTC::TC("qpdf", "QPDFJob found resources in non-leaf");
|
2022-06-05 17:30:42 +00:00
|
|
|
doIfVerbose([&](Pipeline& v, std::string const& prefix) {
|
|
|
|
v << " found resources in non-leaf page node "
|
2022-07-23 20:16:23 +00:00
|
|
|
<< og.unparse(' ') << "\n";
|
2022-01-03 17:16:16 +00:00
|
|
|
});
|
2022-01-03 15:16:28 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
int n = kids.getArrayNItems();
|
2022-04-02 21:14:10 +00:00
|
|
|
for (int i = 0; i < n; ++i) {
|
2022-01-03 15:16:28 +00:00
|
|
|
queue.push_back(kids.getArrayItem(i));
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-01-03 15:16:28 +00:00
|
|
|
// This is a leaf node or a form XObject.
|
|
|
|
QPDFObjectHandle resources = dict.getKey("/Resources");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (resources.isIndirect()) {
|
2022-01-03 15:16:28 +00:00
|
|
|
QPDFObjGen resources_og = resources.getObjGen();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (resources_seen.count(resources_og)) {
|
2022-02-01 12:49:00 +00:00
|
|
|
QTC::TC("qpdf", "QPDFJob found shared resources in leaf");
|
2022-06-05 17:30:42 +00:00
|
|
|
doIfVerbose([&](Pipeline& v, std::string const& prefix) {
|
|
|
|
v << " found shared resources in leaf node "
|
2022-07-23 20:16:23 +00:00
|
|
|
<< og.unparse(' ') << ": "
|
|
|
|
<< resources_og.unparse(' ') << "\n";
|
2022-06-05 17:30:42 +00:00
|
|
|
});
|
2022-01-03 15:16:28 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
resources_seen.insert(resources_og);
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
QPDFObjectHandle xobject =
|
|
|
|
(resources.isDictionary() ? resources.getKey("/XObject")
|
|
|
|
: QPDFObjectHandle::newNull());
|
|
|
|
if (xobject.isIndirect()) {
|
2022-01-03 15:16:28 +00:00
|
|
|
QPDFObjGen xobject_og = xobject.getObjGen();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (resources_seen.count(xobject_og)) {
|
2022-02-01 12:49:00 +00:00
|
|
|
QTC::TC("qpdf", "QPDFJob found shared xobject in leaf");
|
2022-06-05 17:30:42 +00:00
|
|
|
doIfVerbose([&](Pipeline& v, std::string const& prefix) {
|
|
|
|
v << " found shared xobject in leaf node "
|
2022-07-23 20:16:23 +00:00
|
|
|
<< og.unparse(' ') << ": " << xobject_og.unparse(' ')
|
2022-06-05 17:30:42 +00:00
|
|
|
<< "\n";
|
|
|
|
});
|
2022-01-03 15:16:28 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
resources_seen.insert(xobject_og);
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (xobject.isDictionary()) {
|
2022-04-30 13:43:07 +00:00
|
|
|
for (auto const& k: xobject.getKeys()) {
|
2022-01-03 15:16:28 +00:00
|
|
|
QPDFObjectHandle xobj = xobject.getKey(k);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (xobj.isFormXObject()) {
|
2022-01-03 15:16:28 +00:00
|
|
|
queue.push_back(xobj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-05 17:30:42 +00:00
|
|
|
doIfVerbose([&](Pipeline& v, std::string const& prefix) {
|
|
|
|
v << prefix << ": no shared resources found\n";
|
2022-01-03 17:16:16 +00:00
|
|
|
});
|
2022-01-03 15:16:28 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
static QPDFObjectHandle
|
|
|
|
added_page(QPDF& pdf, QPDFObjectHandle page)
|
2022-01-03 15:16:28 +00:00
|
|
|
{
|
|
|
|
QPDFObjectHandle result = page;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (page.getOwningQPDF() != &pdf) {
|
2022-01-03 15:16:28 +00:00
|
|
|
// Calling copyForeignObject on an object we already copied
|
|
|
|
// will give us the already existing copy.
|
|
|
|
result = pdf.copyForeignObject(page);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
static QPDFObjectHandle
|
|
|
|
added_page(QPDF& pdf, QPDFPageObjectHelper page)
|
2022-01-03 15:16:28 +00:00
|
|
|
{
|
|
|
|
return added_page(pdf, page.getObjectHandle());
|
|
|
|
}
|
|
|
|
|
2022-01-05 21:13:43 +00:00
|
|
|
void
|
|
|
|
QPDFJob::handlePageSpecs(
|
2022-04-02 21:14:10 +00:00
|
|
|
QPDF& pdf, bool& warnings, std::vector<std::shared_ptr<QPDF>>& page_heap)
|
2022-01-03 15:16:28 +00:00
|
|
|
{
|
|
|
|
// Parse all page specifications and translate them into lists of
|
|
|
|
// actual pages.
|
|
|
|
|
|
|
|
// Handle "." as a shortcut for the input file
|
2022-04-30 17:23:18 +00:00
|
|
|
for (auto& page_spec: m->page_specs) {
|
2022-04-02 21:14:10 +00:00
|
|
|
if (page_spec.filename == ".") {
|
2022-01-26 19:56:24 +00:00
|
|
|
page_spec.filename = m->infilename.get();
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!m->keep_files_open_set) {
|
2022-01-03 15:16:28 +00:00
|
|
|
// Count the number of distinct files to determine whether we
|
|
|
|
// should keep files open or not. Rather than trying to code
|
|
|
|
// some portable heuristic based on OS limits, just hard-code
|
|
|
|
// this at a given number and allow users to override.
|
|
|
|
std::set<std::string> filenames;
|
2022-04-30 13:43:07 +00:00
|
|
|
for (auto& page_spec: m->page_specs) {
|
2022-01-03 15:16:28 +00:00
|
|
|
filenames.insert(page_spec.filename);
|
|
|
|
}
|
2022-01-26 19:56:24 +00:00
|
|
|
m->keep_files_open = (filenames.size() <= m->keep_files_open_threshold);
|
2022-04-02 21:14:10 +00:00
|
|
|
QTC::TC(
|
|
|
|
"qpdf",
|
|
|
|
"QPDFJob automatically set keep files open",
|
|
|
|
m->keep_files_open ? 0 : 1);
|
2022-06-05 17:30:42 +00:00
|
|
|
doIfVerbose([&](Pipeline& v, std::string const& prefix) {
|
|
|
|
v << prefix << ": selecting --keep-open-files="
|
|
|
|
<< (m->keep_files_open ? "y" : "n") << "\n";
|
2022-01-07 12:29:53 +00:00
|
|
|
});
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a QPDF object for each file that we may take pages from.
|
|
|
|
std::map<std::string, QPDF*> page_spec_qpdfs;
|
|
|
|
std::map<std::string, ClosedFileInputSource*> page_spec_cfis;
|
2022-01-26 19:56:24 +00:00
|
|
|
page_spec_qpdfs[m->infilename.get()] = &pdf;
|
2022-01-03 15:16:28 +00:00
|
|
|
std::vector<QPDFPageData> parsed_specs;
|
2022-04-02 21:14:10 +00:00
|
|
|
std::map<unsigned long long, std::set<QPDFObjGen>> copied_pages;
|
2022-04-30 17:23:18 +00:00
|
|
|
for (auto& page_spec: m->page_specs) {
|
2022-04-02 21:14:10 +00:00
|
|
|
if (page_spec_qpdfs.count(page_spec.filename) == 0) {
|
2022-01-03 15:16:28 +00:00
|
|
|
// Open the PDF file and store the QPDF object. Throw a
|
2022-01-22 22:37:51 +00:00
|
|
|
// std::shared_ptr to the qpdf into a heap so that it
|
2022-01-03 15:16:28 +00:00
|
|
|
// survives through copying to the output but gets cleaned up
|
|
|
|
// automatically at the end. Do not canonicalize the file
|
|
|
|
// name. Using two different paths to refer to the same
|
2022-01-06 20:33:29 +00:00
|
|
|
// file is a documented workaround for duplicating a page.
|
2022-01-03 15:16:28 +00:00
|
|
|
// If you are using this an example of how to do this with
|
|
|
|
// the API, you can just create two different QPDF objects
|
|
|
|
// to the same underlying file with the same path to
|
|
|
|
// achieve the same affect.
|
2022-01-22 23:43:05 +00:00
|
|
|
char const* password = page_spec.password.get();
|
2022-07-26 11:37:50 +00:00
|
|
|
if ((!m->encryption_file.empty()) && (password == nullptr) &&
|
2022-04-02 21:14:10 +00:00
|
|
|
(page_spec.filename == m->encryption_file)) {
|
2022-02-01 12:49:00 +00:00
|
|
|
QTC::TC("qpdf", "QPDFJob pages encryption password");
|
2022-01-26 19:56:24 +00:00
|
|
|
password = m->encryption_file_password.get();
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-06-05 17:30:42 +00:00
|
|
|
doIfVerbose([&](Pipeline& v, std::string const& prefix) {
|
|
|
|
v << prefix << ": processing " << page_spec.filename << "\n";
|
2022-01-03 17:16:16 +00:00
|
|
|
});
|
2022-04-09 18:35:56 +00:00
|
|
|
std::shared_ptr<InputSource> is;
|
2022-07-26 11:37:50 +00:00
|
|
|
ClosedFileInputSource* cis = nullptr;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!m->keep_files_open) {
|
2022-02-01 12:49:00 +00:00
|
|
|
QTC::TC("qpdf", "QPDFJob keep files open n");
|
2022-01-03 15:16:28 +00:00
|
|
|
cis = new ClosedFileInputSource(page_spec.filename.c_str());
|
2022-04-09 18:35:56 +00:00
|
|
|
is = std::shared_ptr<InputSource>(cis);
|
2022-01-03 15:16:28 +00:00
|
|
|
cis->stayOpen(true);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-01 12:49:00 +00:00
|
|
|
QTC::TC("qpdf", "QPDFJob keep files open y");
|
2022-05-04 16:02:39 +00:00
|
|
|
FileInputSource* fis =
|
|
|
|
new FileInputSource(page_spec.filename.c_str());
|
2022-04-09 18:35:56 +00:00
|
|
|
is = std::shared_ptr<InputSource>(fis);
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
std::shared_ptr<QPDF> qpdf_ph =
|
|
|
|
processInputSource(is, password, true);
|
2022-01-03 15:16:28 +00:00
|
|
|
page_heap.push_back(qpdf_ph);
|
2022-01-22 22:37:51 +00:00
|
|
|
page_spec_qpdfs[page_spec.filename] = qpdf_ph.get();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (cis) {
|
2022-01-03 15:16:28 +00:00
|
|
|
cis->stayOpen(false);
|
|
|
|
page_spec_cfis[page_spec.filename] = cis;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read original pages from the PDF, and parse the page range
|
|
|
|
// associated with this occurrence of the file.
|
2022-04-03 20:10:27 +00:00
|
|
|
parsed_specs.push_back(
|
|
|
|
// line-break
|
|
|
|
QPDFPageData(
|
|
|
|
page_spec.filename,
|
|
|
|
page_spec_qpdfs[page_spec.filename],
|
|
|
|
page_spec.range));
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::map<unsigned long long, bool> remove_unreferenced;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->remove_unreferenced_page_resources != QPDFJob::re_no) {
|
2022-04-30 17:23:18 +00:00
|
|
|
for (auto const& iter: page_spec_qpdfs) {
|
|
|
|
std::string const& filename = iter.first;
|
2022-07-26 11:37:50 +00:00
|
|
|
ClosedFileInputSource* cis = nullptr;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (page_spec_cfis.count(filename)) {
|
2022-01-03 15:16:28 +00:00
|
|
|
cis = page_spec_cfis[filename];
|
|
|
|
cis->stayOpen(true);
|
|
|
|
}
|
2022-04-30 17:23:18 +00:00
|
|
|
QPDF& other(*(iter.second));
|
2022-01-03 15:16:28 +00:00
|
|
|
auto other_uuid = other.getUniqueId();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (remove_unreferenced.count(other_uuid) == 0) {
|
2022-01-03 15:16:28 +00:00
|
|
|
remove_unreferenced[other_uuid] =
|
2022-01-05 21:13:43 +00:00
|
|
|
shouldRemoveUnreferencedResources(other);
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (cis) {
|
2022-01-03 15:16:28 +00:00
|
|
|
cis->stayOpen(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear all pages out of the primary QPDF's pages tree but leave
|
|
|
|
// the objects in place in the file so they can be re-added
|
|
|
|
// without changing their object numbers. This enables other
|
|
|
|
// things in the original file, such as outlines, to continue to
|
|
|
|
// work.
|
2022-06-05 17:30:42 +00:00
|
|
|
doIfVerbose([&](Pipeline& v, std::string const& prefix) {
|
|
|
|
v << prefix << ": removing unreferenced pages from primary input\n";
|
2022-01-03 17:16:16 +00:00
|
|
|
});
|
2022-01-03 15:16:28 +00:00
|
|
|
QPDFPageDocumentHelper dh(pdf);
|
|
|
|
std::vector<QPDFPageObjectHelper> orig_pages = dh.getAllPages();
|
2022-04-30 17:23:18 +00:00
|
|
|
for (auto const& page: orig_pages) {
|
|
|
|
dh.removePage(page);
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->collate && (parsed_specs.size() > 1)) {
|
2022-01-03 15:16:28 +00:00
|
|
|
// Collate the pages by selecting one page from each spec in
|
|
|
|
// order. When a spec runs out of pages, stop selecting from
|
|
|
|
// it.
|
|
|
|
std::vector<QPDFPageData> new_parsed_specs;
|
|
|
|
size_t nspecs = parsed_specs.size();
|
|
|
|
size_t cur_page = 0;
|
|
|
|
bool got_pages = true;
|
2022-04-02 21:14:10 +00:00
|
|
|
while (got_pages) {
|
2022-01-03 15:16:28 +00:00
|
|
|
got_pages = false;
|
2022-04-02 21:14:10 +00:00
|
|
|
for (size_t i = 0; i < nspecs; ++i) {
|
2022-01-03 15:16:28 +00:00
|
|
|
QPDFPageData& page_data = parsed_specs.at(i);
|
2022-04-02 21:14:10 +00:00
|
|
|
for (size_t j = 0; j < m->collate; ++j) {
|
|
|
|
if (cur_page + j < page_data.selected_pages.size()) {
|
2022-01-03 15:16:28 +00:00
|
|
|
got_pages = true;
|
2022-04-03 20:10:27 +00:00
|
|
|
new_parsed_specs.push_back(
|
|
|
|
// line-break
|
|
|
|
QPDFPageData(
|
|
|
|
page_data,
|
|
|
|
page_data.selected_pages.at(cur_page + j)));
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-26 19:56:24 +00:00
|
|
|
cur_page += m->collate;
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
parsed_specs = new_parsed_specs;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add all the pages from all the files in the order specified.
|
|
|
|
// Keep track of any pages from the original file that we are
|
|
|
|
// selecting.
|
|
|
|
std::set<int> selected_from_orig;
|
|
|
|
std::vector<QPDFObjectHandle> new_labels;
|
|
|
|
bool any_page_labels = false;
|
|
|
|
int out_pageno = 0;
|
2022-04-02 21:14:10 +00:00
|
|
|
std::map<unsigned long long, std::shared_ptr<QPDFAcroFormDocumentHelper>>
|
|
|
|
afdh_map;
|
2022-01-03 15:16:28 +00:00
|
|
|
auto this_afdh = get_afdh_for_qpdf(afdh_map, &pdf);
|
|
|
|
std::set<QPDFObjGen> referenced_fields;
|
2022-04-30 17:23:18 +00:00
|
|
|
for (auto& page_data: parsed_specs) {
|
2022-07-26 11:37:50 +00:00
|
|
|
ClosedFileInputSource* cis = nullptr;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (page_spec_cfis.count(page_data.filename)) {
|
2022-01-03 15:16:28 +00:00
|
|
|
cis = page_spec_cfis[page_data.filename];
|
|
|
|
cis->stayOpen(true);
|
|
|
|
}
|
|
|
|
QPDFPageLabelDocumentHelper pldh(*page_data.qpdf);
|
|
|
|
auto other_afdh = get_afdh_for_qpdf(afdh_map, page_data.qpdf);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (pldh.hasPageLabels()) {
|
2022-01-03 15:16:28 +00:00
|
|
|
any_page_labels = true;
|
|
|
|
}
|
2022-06-05 17:30:42 +00:00
|
|
|
doIfVerbose([&](Pipeline& v, std::string const& prefix) {
|
|
|
|
v << prefix << ": adding pages from " << page_data.filename << "\n";
|
2022-01-03 17:16:16 +00:00
|
|
|
});
|
2022-04-30 17:23:18 +00:00
|
|
|
for (auto pageno_iter: page_data.selected_pages) {
|
2022-01-03 15:16:28 +00:00
|
|
|
// Pages are specified from 1 but numbered from 0 in the
|
|
|
|
// vector
|
2022-04-30 17:23:18 +00:00
|
|
|
int pageno = pageno_iter - 1;
|
|
|
|
pldh.getLabelsForPageRange(
|
|
|
|
pageno, pageno, out_pageno++, new_labels);
|
2022-01-03 15:16:28 +00:00
|
|
|
QPDFPageObjectHelper to_copy =
|
|
|
|
page_data.orig_pages.at(QIntC::to_size(pageno));
|
|
|
|
QPDFObjGen to_copy_og = to_copy.getObjectHandle().getObjGen();
|
|
|
|
unsigned long long from_uuid = page_data.qpdf->getUniqueId();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (copied_pages[from_uuid].count(to_copy_og)) {
|
|
|
|
QTC::TC(
|
|
|
|
"qpdf",
|
|
|
|
"QPDFJob copy same page more than once",
|
|
|
|
(page_data.qpdf == &pdf) ? 0 : 1);
|
2022-01-03 15:16:28 +00:00
|
|
|
to_copy = to_copy.shallowCopyPage();
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-01-03 15:16:28 +00:00
|
|
|
copied_pages[from_uuid].insert(to_copy_og);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (remove_unreferenced[from_uuid]) {
|
2022-01-03 15:16:28 +00:00
|
|
|
to_copy.removeUnreferencedResources();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dh.addPage(to_copy, false);
|
|
|
|
bool first_copy_from_orig = false;
|
|
|
|
bool this_file = (page_data.qpdf == &pdf);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (this_file) {
|
2022-01-03 15:16:28 +00:00
|
|
|
// This is a page from the original file. Keep track
|
|
|
|
// of the fact that we are using it.
|
|
|
|
first_copy_from_orig = (selected_from_orig.count(pageno) == 0);
|
|
|
|
selected_from_orig.insert(pageno);
|
|
|
|
}
|
|
|
|
auto new_page = added_page(pdf, to_copy);
|
|
|
|
// Try to avoid gratuitously renaming fields. In the case
|
|
|
|
// of where we're just extracting a bunch of pages from
|
|
|
|
// the original file and not copying any page more than
|
|
|
|
// once, there's no reason to do anything with the fields.
|
|
|
|
// Since we don't remove fields from the original file
|
|
|
|
// until all copy operations are completed, any foreign
|
|
|
|
// pages that conflict with original pages will be
|
|
|
|
// adjusted. If we copy any page from the original file
|
|
|
|
// more than once, that page would be in conflict with the
|
|
|
|
// previous copy of itself.
|
|
|
|
if (other_afdh->hasAcroForm() &&
|
2022-04-02 21:14:10 +00:00
|
|
|
((!this_file) || (!first_copy_from_orig))) {
|
|
|
|
if (!this_file) {
|
2022-02-01 12:49:00 +00:00
|
|
|
QTC::TC("qpdf", "QPDFJob copy fields not this file");
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (!first_copy_from_orig) {
|
2022-02-01 12:49:00 +00:00
|
|
|
QTC::TC("qpdf", "QPDFJob copy fields non-first from orig");
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
try {
|
2022-01-03 15:16:28 +00:00
|
|
|
this_afdh->fixCopiedAnnotations(
|
2022-04-02 21:14:10 +00:00
|
|
|
new_page,
|
|
|
|
to_copy.getObjectHandle(),
|
|
|
|
*other_afdh,
|
2022-01-03 15:16:28 +00:00
|
|
|
&referenced_fields);
|
2022-04-02 21:14:10 +00:00
|
|
|
} catch (std::exception& e) {
|
2022-04-23 22:03:44 +00:00
|
|
|
pdf.warn(
|
2022-04-02 21:14:10 +00:00
|
|
|
qpdf_e_damaged_pdf,
|
|
|
|
"",
|
|
|
|
0,
|
2022-04-03 20:10:27 +00:00
|
|
|
("Exception caught while fixing copied"
|
|
|
|
" annotations. This may be a qpdf bug. " +
|
2022-04-23 22:03:44 +00:00
|
|
|
std::string("Exception: ") + e.what()));
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (page_data.qpdf->anyWarnings()) {
|
2022-01-03 15:16:28 +00:00
|
|
|
warnings = true;
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (cis) {
|
2022-01-03 15:16:28 +00:00
|
|
|
cis->stayOpen(false);
|
|
|
|
}
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (any_page_labels) {
|
|
|
|
QPDFObjectHandle page_labels = QPDFObjectHandle::newDictionary();
|
|
|
|
page_labels.replaceKey("/Nums", QPDFObjectHandle::newArray(new_labels));
|
2022-01-03 15:16:28 +00:00
|
|
|
pdf.getRoot().replaceKey("/PageLabels", page_labels);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete page objects for unused page in primary. This prevents
|
|
|
|
// those objects from being preserved by being referred to from
|
|
|
|
// other places, such as the outlines dictionary. Also make sure
|
|
|
|
// we keep form fields from pages we preserved.
|
2022-04-02 21:14:10 +00:00
|
|
|
for (size_t pageno = 0; pageno < orig_pages.size(); ++pageno) {
|
2022-01-03 15:16:28 +00:00
|
|
|
auto page = orig_pages.at(pageno);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (selected_from_orig.count(QIntC::to_int(pageno))) {
|
2022-04-30 13:43:07 +00:00
|
|
|
for (auto field: this_afdh->getFormFieldsForPage(page)) {
|
2022-02-01 12:49:00 +00:00
|
|
|
QTC::TC("qpdf", "QPDFJob pages keeping field from original");
|
2022-01-03 15:16:28 +00:00
|
|
|
referenced_fields.insert(field.getObjectHandle().getObjGen());
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-01-03 15:16:28 +00:00
|
|
|
pdf.replaceObject(
|
|
|
|
page.getObjectHandle().getObjGen(),
|
|
|
|
QPDFObjectHandle::newNull());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Remove unreferenced form fields
|
2022-04-02 21:14:10 +00:00
|
|
|
if (this_afdh->hasAcroForm()) {
|
2022-01-03 15:16:28 +00:00
|
|
|
auto acroform = pdf.getRoot().getKey("/AcroForm");
|
|
|
|
auto fields = acroform.getKey("/Fields");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (fields.isArray()) {
|
2022-01-03 15:16:28 +00:00
|
|
|
auto new_fields = QPDFObjectHandle::newArray();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (fields.isIndirect()) {
|
2022-01-03 15:16:28 +00:00
|
|
|
new_fields = pdf.makeIndirectObject(new_fields);
|
|
|
|
}
|
2022-04-30 13:43:07 +00:00
|
|
|
for (auto const& field: fields.aitems()) {
|
2022-04-02 21:14:10 +00:00
|
|
|
if (referenced_fields.count(field.getObjGen())) {
|
2022-01-03 15:16:28 +00:00
|
|
|
new_fields.appendItem(field);
|
|
|
|
}
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (new_fields.getArrayNItems() > 0) {
|
2022-02-01 12:49:00 +00:00
|
|
|
QTC::TC("qpdf", "QPDFJob keep some fields in pages");
|
2022-01-03 15:16:28 +00:00
|
|
|
acroform.replaceKey("/Fields", new_fields);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-01 12:49:00 +00:00
|
|
|
QTC::TC("qpdf", "QPDFJob no more fields in pages");
|
2022-01-03 15:16:28 +00:00
|
|
|
pdf.getRoot().removeKey("/AcroForm");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-05 21:13:43 +00:00
|
|
|
void
|
|
|
|
QPDFJob::handleRotations(QPDF& pdf)
|
2022-01-03 15:16:28 +00:00
|
|
|
{
|
|
|
|
QPDFPageDocumentHelper dh(pdf);
|
|
|
|
std::vector<QPDFPageObjectHelper> pages = dh.getAllPages();
|
|
|
|
int npages = QIntC::to_int(pages.size());
|
2022-04-30 17:23:18 +00:00
|
|
|
for (auto const& iter: m->rotations) {
|
|
|
|
std::string const& range = iter.first;
|
|
|
|
QPDFJob::RotationSpec const& rspec = iter.second;
|
2022-01-03 15:16:28 +00:00
|
|
|
// range has been previously validated
|
2022-05-21 14:18:15 +00:00
|
|
|
for (int pageno_iter: QUtil::parse_numrange(range.c_str(), npages)) {
|
2022-04-30 17:23:18 +00:00
|
|
|
int pageno = pageno_iter - 1;
|
2022-04-02 21:14:10 +00:00
|
|
|
if ((pageno >= 0) && (pageno < npages)) {
|
|
|
|
pages.at(QIntC::to_size(pageno))
|
|
|
|
.rotatePage(rspec.angle, rspec.relative);
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-05 20:01:35 +00:00
|
|
|
void
|
|
|
|
QPDFJob::maybeFixWritePassword(int R, std::string& password)
|
2022-01-03 15:16:28 +00:00
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
switch (m->password_mode) {
|
|
|
|
case QPDFJob::pm_bytes:
|
2022-02-01 12:49:00 +00:00
|
|
|
QTC::TC("qpdf", "QPDFJob password mode bytes");
|
2022-01-03 15:16:28 +00:00
|
|
|
break;
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
case QPDFJob::pm_hex_bytes:
|
2022-02-01 12:49:00 +00:00
|
|
|
QTC::TC("qpdf", "QPDFJob password mode hex-bytes");
|
2022-01-03 15:16:28 +00:00
|
|
|
password = QUtil::hex_decode(password);
|
|
|
|
break;
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
case QPDFJob::pm_unicode:
|
|
|
|
case QPDFJob::pm_auto:
|
2022-01-03 15:16:28 +00:00
|
|
|
{
|
|
|
|
bool has_8bit_chars;
|
|
|
|
bool is_valid_utf8;
|
|
|
|
bool is_utf16;
|
2022-04-02 21:14:10 +00:00
|
|
|
QUtil::analyze_encoding(
|
|
|
|
password, has_8bit_chars, is_valid_utf8, is_utf16);
|
|
|
|
if (!has_8bit_chars) {
|
2022-01-03 15:16:28 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->password_mode == QPDFJob::pm_unicode) {
|
|
|
|
if (!is_valid_utf8) {
|
2022-02-01 12:49:00 +00:00
|
|
|
QTC::TC("qpdf", "QPDFJob password not unicode");
|
2022-01-03 15:16:28 +00:00
|
|
|
throw std::runtime_error(
|
|
|
|
"supplied password is not valid UTF-8");
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (R < 5) {
|
2022-01-03 15:16:28 +00:00
|
|
|
std::string encoded;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!QUtil::utf8_to_pdf_doc(password, encoded)) {
|
2022-02-01 12:49:00 +00:00
|
|
|
QTC::TC("qpdf", "QPDFJob password not encodable");
|
2022-01-03 15:16:28 +00:00
|
|
|
throw std::runtime_error(
|
|
|
|
"supplied password cannot be encoded for"
|
|
|
|
" 40-bit or 128-bit encryption formats");
|
|
|
|
}
|
|
|
|
password = encoded;
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
|
|
|
if ((R < 5) && is_valid_utf8) {
|
2022-01-03 15:16:28 +00:00
|
|
|
std::string encoded;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (QUtil::utf8_to_pdf_doc(password, encoded)) {
|
2022-02-01 12:49:00 +00:00
|
|
|
QTC::TC("qpdf", "QPDFJob auto-encode password");
|
2022-06-05 17:30:42 +00:00
|
|
|
doIfVerbose([&](Pipeline& v,
|
|
|
|
std::string const& prefix) {
|
|
|
|
v << prefix << ": automatically converting Unicode"
|
|
|
|
<< " password to single-byte encoding as"
|
|
|
|
<< " required for 40-bit or 128-bit"
|
|
|
|
<< " encryption\n";
|
|
|
|
});
|
2022-01-03 15:16:28 +00:00
|
|
|
password = encoded;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-01 12:49:00 +00:00
|
|
|
QTC::TC("qpdf", "QPDFJob bytes fallback warning");
|
2022-06-05 17:30:42 +00:00
|
|
|
*this->m->log->getError()
|
2022-01-05 20:01:35 +00:00
|
|
|
<< this->m->message_prefix << ": WARNING: "
|
2022-01-03 15:16:28 +00:00
|
|
|
<< "supplied password looks like a Unicode"
|
|
|
|
<< " password with characters not allowed in"
|
2022-07-31 00:53:30 +00:00
|
|
|
<< " passwords for 40-bit and 128-bit "
|
|
|
|
"encryption;"
|
2022-01-03 15:16:28 +00:00
|
|
|
<< " most readers will not be able to open this"
|
|
|
|
<< " file with the supplied password."
|
2022-07-31 00:53:30 +00:00
|
|
|
<< " (Use --password-mode=bytes to suppress "
|
|
|
|
"this"
|
2022-06-05 17:30:42 +00:00
|
|
|
<< " warning and use the password anyway.)\n";
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if ((R >= 5) && (!is_valid_utf8)) {
|
2022-02-01 12:49:00 +00:00
|
|
|
QTC::TC("qpdf", "QPDFJob invalid utf-8 in auto");
|
2022-01-03 15:16:28 +00:00
|
|
|
throw std::runtime_error(
|
|
|
|
"supplied password is not a valid Unicode password,"
|
|
|
|
" which is required for 256-bit encryption; to"
|
|
|
|
" really use this password, rerun with the"
|
|
|
|
" --password-mode=bytes option");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-05 20:01:35 +00:00
|
|
|
void
|
|
|
|
QPDFJob::setEncryptionOptions(QPDF& pdf, QPDFWriter& w)
|
2022-01-03 15:16:28 +00:00
|
|
|
{
|
|
|
|
int R = 0;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->keylen == 40) {
|
2022-01-03 15:16:28 +00:00
|
|
|
R = 2;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (m->keylen == 128) {
|
|
|
|
if (m->force_V4 || m->cleartext_metadata || m->use_aes) {
|
2022-01-03 15:16:28 +00:00
|
|
|
R = 4;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-01-03 15:16:28 +00:00
|
|
|
R = 3;
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (m->keylen == 256) {
|
|
|
|
if (m->force_R5) {
|
2022-01-03 15:16:28 +00:00
|
|
|
R = 5;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-01-03 15:16:28 +00:00
|
|
|
R = 6;
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-01-03 15:16:28 +00:00
|
|
|
throw std::logic_error("bad encryption keylen");
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if ((R > 3) && (m->r3_accessibility == false)) {
|
2022-06-05 17:30:42 +00:00
|
|
|
*this->m->log->getError() << this->m->message_prefix
|
|
|
|
<< ": -accessibility=n is ignored for modern"
|
|
|
|
<< " encryption formats\n";
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-01-26 19:56:24 +00:00
|
|
|
maybeFixWritePassword(R, m->user_password);
|
|
|
|
maybeFixWritePassword(R, m->owner_password);
|
2022-04-02 21:14:10 +00:00
|
|
|
if ((R < 4) || ((R == 4) && (!m->use_aes))) {
|
|
|
|
if (!m->allow_weak_crypto) {
|
2022-04-30 19:41:14 +00:00
|
|
|
QTC::TC("qpdf", "QPDFJob weak crypto error");
|
2022-06-05 17:30:42 +00:00
|
|
|
*this->m->log->getError()
|
2022-01-05 20:01:35 +00:00
|
|
|
<< this->m->message_prefix
|
2022-07-31 00:53:30 +00:00
|
|
|
<< ": refusing to write a file with RC4, a weak "
|
|
|
|
"cryptographic "
|
2022-06-05 17:30:42 +00:00
|
|
|
"algorithm\n"
|
|
|
|
<< "Please use 256-bit keys for better security.\n"
|
|
|
|
<< "Pass --allow-weak-crypto to enable writing insecure "
|
|
|
|
"files.\n"
|
2022-04-30 20:05:28 +00:00
|
|
|
<< "See also "
|
2022-07-31 00:53:30 +00:00
|
|
|
"https://qpdf.readthedocs.io/en/stable/"
|
|
|
|
"weak-crypto.html\n";
|
2022-04-30 20:05:28 +00:00
|
|
|
throw std::runtime_error(
|
|
|
|
"refusing to write a file with weak crypto");
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
switch (R) {
|
|
|
|
case 2:
|
2022-04-30 20:05:28 +00:00
|
|
|
w.setR2EncryptionParametersInsecure(
|
2022-04-02 21:14:10 +00:00
|
|
|
m->user_password.c_str(),
|
|
|
|
m->owner_password.c_str(),
|
|
|
|
m->r2_print,
|
|
|
|
m->r2_modify,
|
|
|
|
m->r2_extract,
|
|
|
|
m->r2_annotate);
|
2022-01-03 15:16:28 +00:00
|
|
|
break;
|
2022-04-02 21:14:10 +00:00
|
|
|
case 3:
|
2022-04-30 20:05:28 +00:00
|
|
|
w.setR3EncryptionParametersInsecure(
|
2022-04-02 21:14:10 +00:00
|
|
|
m->user_password.c_str(),
|
|
|
|
m->owner_password.c_str(),
|
|
|
|
m->r3_accessibility,
|
|
|
|
m->r3_extract,
|
|
|
|
m->r3_assemble,
|
|
|
|
m->r3_annotate_and_form,
|
|
|
|
m->r3_form_filling,
|
|
|
|
m->r3_modify_other,
|
2022-01-26 19:56:24 +00:00
|
|
|
m->r3_print);
|
2022-01-03 15:16:28 +00:00
|
|
|
break;
|
2022-04-02 21:14:10 +00:00
|
|
|
case 4:
|
2022-04-30 20:05:28 +00:00
|
|
|
w.setR4EncryptionParametersInsecure(
|
2022-04-02 21:14:10 +00:00
|
|
|
m->user_password.c_str(),
|
|
|
|
m->owner_password.c_str(),
|
|
|
|
m->r3_accessibility,
|
|
|
|
m->r3_extract,
|
|
|
|
m->r3_assemble,
|
|
|
|
m->r3_annotate_and_form,
|
|
|
|
m->r3_form_filling,
|
|
|
|
m->r3_modify_other,
|
|
|
|
m->r3_print,
|
|
|
|
!m->cleartext_metadata,
|
|
|
|
m->use_aes);
|
2022-01-03 15:16:28 +00:00
|
|
|
break;
|
2022-04-02 21:14:10 +00:00
|
|
|
case 5:
|
2022-01-03 15:16:28 +00:00
|
|
|
w.setR5EncryptionParameters(
|
2022-04-02 21:14:10 +00:00
|
|
|
m->user_password.c_str(),
|
|
|
|
m->owner_password.c_str(),
|
|
|
|
m->r3_accessibility,
|
|
|
|
m->r3_extract,
|
|
|
|
m->r3_assemble,
|
|
|
|
m->r3_annotate_and_form,
|
|
|
|
m->r3_form_filling,
|
|
|
|
m->r3_modify_other,
|
|
|
|
m->r3_print,
|
|
|
|
!m->cleartext_metadata);
|
2022-01-03 15:16:28 +00:00
|
|
|
break;
|
2022-04-02 21:14:10 +00:00
|
|
|
case 6:
|
2022-01-03 15:16:28 +00:00
|
|
|
w.setR6EncryptionParameters(
|
2022-04-02 21:14:10 +00:00
|
|
|
m->user_password.c_str(),
|
|
|
|
m->owner_password.c_str(),
|
|
|
|
m->r3_accessibility,
|
|
|
|
m->r3_extract,
|
|
|
|
m->r3_assemble,
|
|
|
|
m->r3_annotate_and_form,
|
|
|
|
m->r3_form_filling,
|
|
|
|
m->r3_modify_other,
|
|
|
|
m->r3_print,
|
|
|
|
!m->cleartext_metadata);
|
2022-01-03 15:16:28 +00:00
|
|
|
break;
|
2022-04-02 21:14:10 +00:00
|
|
|
default:
|
2022-01-03 15:16:28 +00:00
|
|
|
throw std::logic_error("bad encryption R value");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
static void
|
|
|
|
parse_version(
|
|
|
|
std::string const& full_version_string,
|
|
|
|
std::string& version,
|
|
|
|
int& extension_level)
|
2022-01-05 21:50:18 +00:00
|
|
|
{
|
2022-02-05 13:15:07 +00:00
|
|
|
auto vp = QUtil::make_unique_cstr(full_version_string);
|
2022-01-22 22:37:51 +00:00
|
|
|
char* v = vp.get();
|
2022-01-05 21:50:18 +00:00
|
|
|
char* p1 = strchr(v, '.');
|
2022-07-26 11:37:50 +00:00
|
|
|
char* p2 = (p1 ? strchr(1 + p1, '.') : nullptr);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (p2 && *(p2 + 1)) {
|
2022-01-05 21:50:18 +00:00
|
|
|
*p2++ = '\0';
|
|
|
|
extension_level = QUtil::string_to_int(p2);
|
|
|
|
}
|
|
|
|
version = v;
|
|
|
|
}
|
|
|
|
|
2022-01-03 15:16:28 +00:00
|
|
|
void
|
|
|
|
QPDFJob::setWriterOptions(QPDF& pdf, QPDFWriter& w)
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->compression_level >= 0) {
|
2022-01-26 19:56:24 +00:00
|
|
|
Pl_Flate::setCompressionLevel(m->compression_level);
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->qdf_mode) {
|
2022-01-03 15:16:28 +00:00
|
|
|
w.setQDFMode(true);
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->preserve_unreferenced_objects) {
|
2022-01-03 15:16:28 +00:00
|
|
|
w.setPreserveUnreferencedObjects(true);
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->newline_before_endstream) {
|
2022-01-03 15:16:28 +00:00
|
|
|
w.setNewlineBeforeEndstream(true);
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->normalize_set) {
|
2022-01-26 19:56:24 +00:00
|
|
|
w.setContentNormalization(m->normalize);
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->stream_data_set) {
|
2022-01-26 19:56:24 +00:00
|
|
|
w.setStreamDataMode(m->stream_data_mode);
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->compress_streams_set) {
|
2022-01-26 19:56:24 +00:00
|
|
|
w.setCompressStreams(m->compress_streams);
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->recompress_flate_set) {
|
2022-01-26 19:56:24 +00:00
|
|
|
w.setRecompressFlate(m->recompress_flate);
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->decode_level_set) {
|
2022-01-26 19:56:24 +00:00
|
|
|
w.setDecodeLevel(m->decode_level);
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->decrypt) {
|
2022-01-03 15:16:28 +00:00
|
|
|
w.setPreserveEncryption(false);
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->deterministic_id) {
|
2022-01-03 15:16:28 +00:00
|
|
|
w.setDeterministicID(true);
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->static_id) {
|
2022-01-03 15:16:28 +00:00
|
|
|
w.setStaticID(true);
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->static_aes_iv) {
|
2022-01-03 15:16:28 +00:00
|
|
|
w.setStaticAesIV(true);
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->suppress_original_object_id) {
|
2022-01-03 15:16:28 +00:00
|
|
|
w.setSuppressOriginalObjectIDs(true);
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->copy_encryption) {
|
|
|
|
std::shared_ptr<QPDF> encryption_pdf = processFile(
|
|
|
|
m->encryption_file.c_str(),
|
|
|
|
m->encryption_file_password.get(),
|
2022-05-18 22:22:57 +00:00
|
|
|
false,
|
2022-04-02 21:14:10 +00:00
|
|
|
false);
|
2022-01-03 15:16:28 +00:00
|
|
|
w.copyEncryptionParameters(*encryption_pdf);
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->encrypt) {
|
2022-01-05 20:01:35 +00:00
|
|
|
setEncryptionOptions(pdf, w);
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->linearize) {
|
2022-01-03 15:16:28 +00:00
|
|
|
w.setLinearization(true);
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!m->linearize_pass1.empty()) {
|
2022-01-26 19:56:24 +00:00
|
|
|
w.setLinearizationPass1Filename(m->linearize_pass1);
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->object_stream_set) {
|
2022-01-26 19:56:24 +00:00
|
|
|
w.setObjectStreamMode(m->object_stream_mode);
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-02-08 16:27:38 +00:00
|
|
|
w.setMinimumPDFVersion(this->m->max_input_version);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!m->min_version.empty()) {
|
2022-01-03 15:16:28 +00:00
|
|
|
std::string version;
|
|
|
|
int extension_level = 0;
|
2022-01-26 19:56:24 +00:00
|
|
|
parse_version(m->min_version, version, extension_level);
|
2022-01-03 15:16:28 +00:00
|
|
|
w.setMinimumPDFVersion(version, extension_level);
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!m->force_version.empty()) {
|
2022-01-03 15:16:28 +00:00
|
|
|
std::string version;
|
|
|
|
int extension_level = 0;
|
2022-01-26 19:56:24 +00:00
|
|
|
parse_version(m->force_version, version, extension_level);
|
2022-01-03 15:16:28 +00:00
|
|
|
w.forcePDFVersion(version, extension_level);
|
|
|
|
}
|
2022-06-18 13:40:41 +00:00
|
|
|
if (m->progress) {
|
2022-06-19 00:44:44 +00:00
|
|
|
if (this->m->progress_handler) {
|
|
|
|
w.registerProgressReporter(
|
|
|
|
std::shared_ptr<QPDFWriter::ProgressReporter>(
|
|
|
|
new QPDFWriter::FunctionProgressReporter(
|
|
|
|
this->m->progress_handler)));
|
|
|
|
} else {
|
|
|
|
char const* outfilename = this->m->outfilename
|
|
|
|
? this->m->outfilename.get()
|
|
|
|
: "standard output";
|
|
|
|
w.registerProgressReporter(
|
|
|
|
std::shared_ptr<QPDFWriter::ProgressReporter>(
|
|
|
|
// line-break
|
|
|
|
new ProgressReporter(
|
|
|
|
*this->m->log->getInfo(),
|
|
|
|
this->m->message_prefix,
|
|
|
|
outfilename)));
|
|
|
|
}
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFJob::doSplitPages(QPDF& pdf, bool& warnings)
|
|
|
|
{
|
|
|
|
// Generate output file pattern
|
|
|
|
std::string before;
|
|
|
|
std::string after;
|
2022-01-26 19:56:24 +00:00
|
|
|
size_t len = strlen(m->outfilename.get());
|
|
|
|
char* num_spot = strstr(const_cast<char*>(m->outfilename.get()), "%d");
|
2022-07-26 11:37:50 +00:00
|
|
|
if (num_spot != nullptr) {
|
2022-02-01 12:49:00 +00:00
|
|
|
QTC::TC("qpdf", "QPDFJob split-pages %d");
|
2022-04-02 21:14:10 +00:00
|
|
|
before = std::string(
|
|
|
|
m->outfilename.get(),
|
|
|
|
QIntC::to_size(num_spot - m->outfilename.get()));
|
2022-01-03 15:16:28 +00:00
|
|
|
after = num_spot + 2;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (
|
|
|
|
(len >= 4) &&
|
|
|
|
(QUtil::str_compare_nocase(m->outfilename.get() + len - 4, ".pdf") ==
|
|
|
|
0)) {
|
2022-02-01 12:49:00 +00:00
|
|
|
QTC::TC("qpdf", "QPDFJob split-pages .pdf");
|
2022-01-26 19:56:24 +00:00
|
|
|
before = std::string(m->outfilename.get(), len - 4) + "-";
|
|
|
|
after = m->outfilename.get() + len - 4;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-01 12:49:00 +00:00
|
|
|
QTC::TC("qpdf", "QPDFJob split-pages other");
|
2022-01-26 19:56:24 +00:00
|
|
|
before = std::string(m->outfilename.get()) + "-";
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (shouldRemoveUnreferencedResources(pdf)) {
|
2022-01-03 15:16:28 +00:00
|
|
|
QPDFPageDocumentHelper dh(pdf);
|
|
|
|
dh.removeUnreferencedResources();
|
|
|
|
}
|
|
|
|
QPDFPageLabelDocumentHelper pldh(pdf);
|
|
|
|
QPDFAcroFormDocumentHelper afdh(pdf);
|
|
|
|
std::vector<QPDFObjectHandle> const& pages = pdf.getAllPages();
|
|
|
|
size_t pageno_len = QUtil::uint_to_string(pages.size()).length();
|
|
|
|
size_t num_pages = pages.size();
|
2022-04-02 21:14:10 +00:00
|
|
|
for (size_t i = 0; i < num_pages; i += QIntC::to_size(m->split_pages)) {
|
2022-01-03 15:16:28 +00:00
|
|
|
size_t first = i + 1;
|
2022-01-26 19:56:24 +00:00
|
|
|
size_t last = i + QIntC::to_size(m->split_pages);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (last > num_pages) {
|
2022-01-03 15:16:28 +00:00
|
|
|
last = num_pages;
|
|
|
|
}
|
|
|
|
QPDF outpdf;
|
|
|
|
outpdf.emptyPDF();
|
2022-01-22 22:37:51 +00:00
|
|
|
std::shared_ptr<QPDFAcroFormDocumentHelper> out_afdh;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (afdh.hasAcroForm()) {
|
2022-01-22 22:37:51 +00:00
|
|
|
out_afdh = std::make_shared<QPDFAcroFormDocumentHelper>(outpdf);
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->suppress_warnings) {
|
2022-01-03 15:16:28 +00:00
|
|
|
outpdf.setSuppressWarnings(true);
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
for (size_t pageno = first; pageno <= last; ++pageno) {
|
2022-01-03 15:16:28 +00:00
|
|
|
QPDFObjectHandle page = pages.at(pageno - 1);
|
|
|
|
outpdf.addPage(page, false);
|
|
|
|
auto new_page = added_page(outpdf, page);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (out_afdh.get()) {
|
2022-02-01 12:49:00 +00:00
|
|
|
QTC::TC("qpdf", "QPDFJob copy form fields in split_pages");
|
2022-04-02 21:14:10 +00:00
|
|
|
try {
|
2022-01-03 15:16:28 +00:00
|
|
|
out_afdh->fixCopiedAnnotations(new_page, page, afdh);
|
2022-04-02 21:14:10 +00:00
|
|
|
} catch (std::exception& e) {
|
2022-04-23 22:03:44 +00:00
|
|
|
pdf.warn(
|
2022-04-02 21:14:10 +00:00
|
|
|
qpdf_e_damaged_pdf,
|
|
|
|
"",
|
|
|
|
0,
|
2022-04-23 22:03:44 +00:00
|
|
|
("Exception caught while fixing copied"
|
|
|
|
" annotations. This may be a qpdf bug." +
|
|
|
|
std::string("Exception: ") + e.what()));
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (pldh.hasPageLabels()) {
|
2022-01-03 15:16:28 +00:00
|
|
|
std::vector<QPDFObjectHandle> labels;
|
|
|
|
pldh.getLabelsForPageRange(
|
|
|
|
QIntC::to_longlong(first - 1),
|
|
|
|
QIntC::to_longlong(last - 1),
|
2022-04-02 21:14:10 +00:00
|
|
|
0,
|
|
|
|
labels);
|
|
|
|
QPDFObjectHandle page_labels = QPDFObjectHandle::newDictionary();
|
|
|
|
page_labels.replaceKey("/Nums", QPDFObjectHandle::newArray(labels));
|
2022-01-03 15:16:28 +00:00
|
|
|
outpdf.getRoot().replaceKey("/PageLabels", page_labels);
|
|
|
|
}
|
|
|
|
std::string page_range =
|
|
|
|
QUtil::uint_to_string(first, QIntC::to_int(pageno_len));
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->split_pages > 1) {
|
|
|
|
page_range +=
|
|
|
|
"-" + QUtil::uint_to_string(last, QIntC::to_int(pageno_len));
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
std::string outfile = before + page_range + after;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (QUtil::same_file(m->infilename.get(), outfile.c_str())) {
|
2022-01-03 17:16:16 +00:00
|
|
|
throw std::runtime_error(
|
|
|
|
"split pages would overwrite input file with " + outfile);
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
QPDFWriter w(outpdf, outfile.c_str());
|
|
|
|
setWriterOptions(outpdf, w);
|
|
|
|
w.write();
|
2022-06-05 17:30:42 +00:00
|
|
|
doIfVerbose([&](Pipeline& v, std::string const& prefix) {
|
|
|
|
v << prefix << ": wrote file " << outfile << "\n";
|
2022-01-03 17:16:16 +00:00
|
|
|
});
|
2022-04-02 21:14:10 +00:00
|
|
|
if (outpdf.anyWarnings()) {
|
2022-01-03 15:16:28 +00:00
|
|
|
warnings = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFJob::writeOutfile(QPDF& pdf)
|
|
|
|
{
|
2022-01-22 23:30:39 +00:00
|
|
|
std::shared_ptr<char> temp_out;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->replace_input) {
|
2022-01-03 15:16:28 +00:00
|
|
|
// Append but don't prepend to the path to generate a
|
|
|
|
// temporary name. This saves us from having to split the path
|
|
|
|
// by directory and non-directory.
|
2022-01-22 23:30:39 +00:00
|
|
|
temp_out = QUtil::make_shared_cstr(
|
2022-01-26 19:56:24 +00:00
|
|
|
std::string(m->infilename.get()) + ".~qpdf-temp#");
|
|
|
|
// m->outfilename will be restored to 0 before temp_out
|
2022-01-03 15:16:28 +00:00
|
|
|
// goes out of scope.
|
2022-01-26 19:56:24 +00:00
|
|
|
m->outfilename = temp_out;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (strcmp(m->outfilename.get(), "-") == 0) {
|
2022-05-18 22:22:57 +00:00
|
|
|
m->outfilename = nullptr;
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-07-31 14:34:05 +00:00
|
|
|
if (this->m->json_version) {
|
|
|
|
writeJSON(pdf);
|
2022-05-18 22:22:57 +00:00
|
|
|
} else {
|
|
|
|
// QPDFWriter must have block scope so the output file will be
|
|
|
|
// closed after write() finishes.
|
2022-06-18 13:40:41 +00:00
|
|
|
QPDFWriter w(pdf);
|
|
|
|
if (this->m->outfilename) {
|
|
|
|
w.setOutputFilename(m->outfilename.get());
|
|
|
|
} else {
|
|
|
|
// saveToStandardOutput has already been called, but
|
|
|
|
// calling it again is defensive and harmless.
|
2022-06-18 17:38:36 +00:00
|
|
|
this->m->log->saveToStandardOutput(true);
|
2022-06-18 13:40:41 +00:00
|
|
|
w.setOutputPipeline(this->m->log->getSave().get());
|
|
|
|
}
|
2022-01-03 15:16:28 +00:00
|
|
|
setWriterOptions(pdf, w);
|
|
|
|
w.write();
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->outfilename) {
|
2022-06-05 17:30:42 +00:00
|
|
|
doIfVerbose([&](Pipeline& v, std::string const& prefix) {
|
|
|
|
v << prefix << ": wrote file " << m->outfilename.get() << "\n";
|
2022-01-03 17:16:16 +00:00
|
|
|
});
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->replace_input) {
|
2022-07-26 11:37:50 +00:00
|
|
|
m->outfilename = nullptr;
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (m->replace_input) {
|
2022-01-03 15:16:28 +00:00
|
|
|
// We must close the input before we can rename files
|
|
|
|
pdf.closeInputSource();
|
2022-01-26 19:56:24 +00:00
|
|
|
std::string backup = std::string(m->infilename.get()) + ".~qpdf-orig";
|
2022-01-03 15:16:28 +00:00
|
|
|
bool warnings = pdf.anyWarnings();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!warnings) {
|
2022-01-03 15:16:28 +00:00
|
|
|
backup.append(1, '#');
|
|
|
|
}
|
2022-01-26 19:56:24 +00:00
|
|
|
QUtil::rename_file(m->infilename.get(), backup.c_str());
|
|
|
|
QUtil::rename_file(temp_out.get(), m->infilename.get());
|
2022-04-02 21:14:10 +00:00
|
|
|
if (warnings) {
|
2022-06-05 17:30:42 +00:00
|
|
|
*this->m->log->getError()
|
|
|
|
<< this->m->message_prefix
|
|
|
|
<< ": there are warnings; original file kept in " << backup
|
|
|
|
<< "\n";
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
|
|
|
try {
|
2022-01-03 15:16:28 +00:00
|
|
|
QUtil::remove_file(backup.c_str());
|
2022-04-02 21:14:10 +00:00
|
|
|
} catch (QPDFSystemError& e) {
|
2022-06-05 17:30:42 +00:00
|
|
|
*this->m->log->getError()
|
2022-01-05 20:01:35 +00:00
|
|
|
<< this->m->message_prefix
|
2022-04-02 21:14:10 +00:00
|
|
|
<< ": unable to delete original file (" << e.what() << ");"
|
2022-01-03 15:16:28 +00:00
|
|
|
<< " original file left in " << backup
|
2022-06-05 17:30:42 +00:00
|
|
|
<< ", but the input was successfully replaced\n";
|
2022-01-03 15:16:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-18 22:22:57 +00:00
|
|
|
|
|
|
|
void
|
2022-07-31 14:34:05 +00:00
|
|
|
QPDFJob::writeJSON(QPDF& pdf)
|
2022-05-18 22:22:57 +00:00
|
|
|
{
|
|
|
|
// File pipeline must have block scope so it will be closed
|
|
|
|
// after write.
|
|
|
|
std::shared_ptr<QUtil::FileCloser> fc;
|
|
|
|
std::shared_ptr<Pipeline> fp;
|
|
|
|
if (m->outfilename.get()) {
|
|
|
|
QTC::TC("qpdf", "QPDFJob write json to file");
|
2022-07-31 14:34:05 +00:00
|
|
|
if (this->m->json_stream_prefix.empty()) {
|
|
|
|
this->m->json_stream_prefix = this->m->outfilename.get();
|
2022-05-18 22:22:57 +00:00
|
|
|
}
|
|
|
|
fc = std::make_shared<QUtil::FileCloser>(
|
|
|
|
QUtil::safe_fopen(this->m->outfilename.get(), "w"));
|
|
|
|
fp = std::make_shared<Pl_StdioFile>("json output", fc->f);
|
|
|
|
} else if (
|
2022-07-31 14:34:05 +00:00
|
|
|
(this->m->json_stream_data == qpdf_sj_file) &&
|
|
|
|
this->m->json_stream_prefix.empty()) {
|
2022-05-18 22:22:57 +00:00
|
|
|
QTC::TC("qpdf", "QPDFJob need json-stream-prefix for stdout");
|
|
|
|
usage("please specify --json-stream-prefix since the input file "
|
|
|
|
"name is unknown");
|
|
|
|
} else {
|
|
|
|
QTC::TC("qpdf", "QPDFJob write json to stdout");
|
2022-07-31 14:34:05 +00:00
|
|
|
this->m->log->saveToStandardOutput(true);
|
|
|
|
fp = this->m->log->getSave();
|
|
|
|
}
|
|
|
|
doJSON(pdf, fp.get());
|
2022-05-18 22:22:57 +00:00
|
|
|
}
|