use super::attachments::AttachmentAddOptions;
use super::attachments::AttachmentCopyOptions;
use super::image_optimization::{optimize_images, ImageOptimizationOptions};
use super::json::{JsonJobError, JsonJobOptions, JsonJobOutput, JsonStreamData};
use super::overlay::{
handle_under_overlay, overlay_verbose_report, OverlayKind, OverlaySpec, OverlayVerbosePage,
};
use super::page_range::PageRange;
use super::page_specs::{PageSpecInput, PageSpecJobOutput};
use super::page_split::SplitPageOptions;
use super::resource_pruning::RemoveUnreferencedResources;
use super::rotate::flatten_rotation_on_pages;
use super::rotate_spec::{parse_rotation_parameter, RotationSpec};
use crate::encryption::{EncryptMethod, EncryptParams, PasswordMode};
use crate::json_inspect::{DecodeLevel as JsonDecodeLevel, JsonKey};
use crate::linearization::{show_linearization_pdf_with_warnings, ShowLinearizationError};
use crate::pipeline::{Pipeline, PipelineHandle, PipelineResult};
use crate::qutil::{qpdf_string_to_int_checked, QpdfIntParse};
use crate::{
AcroFormDocumentHelper, Error, ObjectHandle, ObjectRef, ObjectStreamMode, PageDocumentHelper,
PageObjectHelper, Pdf, PdfOpenOptions, PdfVersion, PdfWriter, QPDFLogger, ReadSeek, Result,
UsageError, WriterConfiguration,
};
use std::cell::RefCell;
use std::collections::{BTreeMap, BTreeSet};
use std::fs::File;
use std::io::{BufReader, Cursor, Read, Seek, Write};
use std::path::{Path, PathBuf};
use std::rc::Rc;
type ProgressHandler = Box<dyn FnMut(u8) -> Result<()> + 'static>;
type SharedProgressHandler = Rc<RefCell<ProgressHandler>>;
fn path_description_bytes(path: &Path) -> Vec<u8> {
#[cfg(unix)]
{
use std::os::unix::ffi::OsStrExt;
path.as_os_str().as_bytes().to_vec()
}
#[cfg(not(unix))]
{
path.to_string_lossy().into_owned().into_bytes()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FlattenAnnotationsMode {
All,
Screen,
Print,
}
impl FlattenAnnotationsMode {
pub const fn qpdf_flags(self) -> (i64, i64) {
match self {
Self::All => (0, 0x3),
Self::Screen => (0, 0x23),
Self::Print => (0x4, 0x3),
}
}
}
const DEFAULT_KEEP_FILES_OPEN_THRESHOLD: usize = 200;
fn path_from_qpdf_json_bytes(bytes: &[u8]) -> PathBuf {
#[cfg(unix)]
{
use std::ffi::OsString;
use std::os::unix::ffi::OsStringExt;
PathBuf::from(OsString::from_vec(bytes.to_vec()))
}
#[cfg(windows)]
{
PathBuf::from(String::from_utf8_lossy(bytes).into_owned())
}
#[cfg(not(any(unix, windows)))]
{
PathBuf::from(String::from_utf8_lossy(bytes).into_owned())
}
}
fn path_with_suffix(path: &Path, suffix: &str) -> PathBuf {
let mut value = path.as_os_str().to_os_string();
value.push(suffix);
PathBuf::from(value)
}
fn path_component_to_qpdf_bytes(component: &std::ffi::OsStr) -> Vec<u8> {
#[cfg(unix)]
{
use std::os::unix::ffi::OsStrExt;
component.as_bytes().to_vec()
}
#[cfg(windows)]
{
component.to_string_lossy().into_owned().into_bytes()
}
#[cfg(not(any(unix, windows)))]
{
component.to_string_lossy().into_owned().into_bytes()
}
}
pub type JobDocument = Pdf<Box<dyn ReadSeek>>;
struct JobOutputPipeline(PipelineHandle);
impl Pipeline for JobOutputPipeline {
fn identifier(&self) -> &str {
"qpdf job output"
}
fn write(&mut self, data: &[u8]) -> PipelineResult<()> {
self.0.write(data)
}
fn finish(&mut self) -> PipelineResult<()> {
self.0.finish()
}
}
struct JobOutputWriter(PipelineHandle);
impl Write for JobOutputWriter {
fn write(&mut self, data: &[u8]) -> std::io::Result<usize> {
self.0.write(data).map_err(std::io::Error::other)?;
Ok(data.len())
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
#[derive(Debug, Clone, Default)]
struct JobConfiguration {
input_file: Option<PathBuf>,
empty_input: bool,
output_file: Option<PathBuf>,
password: Vec<u8>,
copy_encryption: Option<PathBuf>,
encryption_file_password: Vec<u8>,
password_mode: PasswordMode,
ignore_xref_streams: bool,
password_is_hex_key: bool,
suppress_password_recovery: bool,
suppress_recovery: bool,
max_input_version: Option<(String, i64)>,
verbose: bool,
json_input: bool,
update_from_json: Option<PathBuf>,
replace_input: bool,
check: bool,
show_npages: bool,
show_pages: bool,
show_page_images: bool,
check_linearization: bool,
require_output: bool,
progress: bool,
split_pages: Option<i32>,
keep_files_open: Option<bool>,
keep_files_open_threshold: Option<usize>,
rotations: BTreeMap<Vec<u8>, RotationSpec>,
remove_restrictions: bool,
coalesce_contents: bool,
optimize_images: bool,
externalize_inline_images: bool,
image_options: ImageOptimizationOptions,
flatten_annotations: Option<FlattenAnnotationsMode>,
flatten_rotation: bool,
generate_appearances: bool,
normalize_content: Option<bool>,
writer: WriterConfiguration,
linearize: bool,
linearize_pass1: Option<PathBuf>,
allow_weak_crypto: bool,
page_specs: Vec<JobPageConfig>,
page_specs_origin: PageSpecsOrigin,
collate: Option<Vec<usize>>,
overlays: Vec<JobOverlayConfig>,
underlays: Vec<JobOverlayConfig>,
attachments_to_add: Vec<AttachmentAddOptions>,
attachments_to_copy: Vec<JobCopyAttachmentsConfig>,
attachments_to_remove: Vec<Vec<u8>>,
remove_unreferenced_resources: RemoveUnreferencedResources,
set_page_labels: Option<Vec<PageLabelSpec>>,
remove_page_labels: bool,
json_version: Option<i32>,
json_output: bool,
json_decode_level: crate::writer::DecodeLevel,
json_decode_level_set: bool,
json_keys: Vec<JsonKey>,
json_objects: Vec<String>,
json_stream_data: JsonStreamData,
json_stream_data_set: bool,
json_stream_prefix: Option<Vec<u8>>,
test_json_schema: bool,
show_encryption_key: bool,
show_encryption: bool,
is_encrypted: bool,
requires_password: bool,
report_memory_usage: bool,
show_xref: bool,
show_linearization: bool,
show_object: Option<JobObjectSelector>,
show_raw_stream_data: bool,
show_filtered_stream_data: bool,
list_attachments: bool,
show_attachment: Option<Vec<u8>>,
}
fn qpdf_default_job_configuration() -> JobConfiguration {
JobConfiguration::default()
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
enum PageSpecsOrigin {
#[default]
None,
Config,
Json,
}
#[derive(Debug, Clone, PartialEq, Eq)]
struct PageLabelSpec {
first_page: i64,
style: crate::page_label_document_helper::LabelStyle,
start: i64,
prefix: Vec<u8>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum JobObjectSelector {
Trailer,
Object(ObjectRef),
Null,
NoObject,
}
#[derive(Debug, Clone)]
struct JobPageConfig {
path: PathBuf,
password: Option<Vec<u8>>,
range: PageRange,
}
#[derive(Debug, Clone)]
struct JobOverlayConfig {
path: PathBuf,
password: Vec<u8>,
from: PageRange,
to: PageRange,
repeat: Option<PageRange>,
kind: OverlayKind,
}
#[derive(Debug, Clone)]
struct JobCopyAttachmentsConfig {
path: PathBuf,
password: Vec<u8>,
prefix: Vec<u8>,
}
fn job_schema_scalar() -> crate::json::Json {
crate::json::Json::make_string("qpdf job option")
}
fn job_schema_dictionary(
entries: impl IntoIterator<Item = (&'static str, crate::json::Json)>,
) -> crate::json::Json {
let dictionary = crate::json::Json::make_dictionary();
for (key, value) in entries {
dictionary
.add_dictionary_member(key, value)
.expect("static qpdf job schema dictionary is valid");
}
dictionary
}
fn job_schema_array(item: crate::json::Json) -> crate::json::Json {
let array = crate::json::Json::make_array();
array
.add_array_element(item)
.expect("static qpdf job schema array is valid");
array
}
fn job_json_schema() -> crate::json::Json {
let scalar = job_schema_scalar();
let schema = crate::json::Json::make_dictionary();
for key in [
"inputFile",
"password",
"passwordFile",
"empty",
"jsonInput",
"outputFile",
"replaceInput",
"qdf",
"preserveUnreferenced",
"newlineBeforeEndstream",
"normalizeContent",
"streamData",
"compressStreams",
"recompressFlate",
"decodeLevel",
"decrypt",
"deterministicId",
"staticAesIv",
"staticId",
"noOriginalObjectIds",
"copyEncryption",
"encryptionFilePassword",
"linearize",
"linearizePass1",
"objectStreams",
"minVersion",
"forceVersion",
"progress",
"splitPages",
"jsonOutput",
"removeRestrictions",
"check",
"checkLinearization",
"filteredStreamData",
"rawStreamData",
"showEncryption",
"showEncryptionKey",
"showLinearization",
"showNpages",
"showObject",
"showPages",
"showXref",
"showAttachment",
"withImages",
"listAttachments",
"json",
"jsonStreamData",
"jsonStreamPrefix",
"updateFromJson",
"allowWeakCrypto",
"keepFilesOpen",
"keepFilesOpenThreshold",
"noWarn",
"verbose",
"testJsonSchema",
"ignoreXrefStreams",
"passwordIsHexKey",
"passwordMode",
"suppressPasswordRecovery",
"suppressRecovery",
"coalesceContents",
"compressionLevel",
"externalizeInlineImages",
"iiMinBytes",
"oiMinArea",
"oiMinHeight",
"oiMinWidth",
"removeUnreferencedResources",
"preserveUnreferencedResources",
"requiresPassword",
"isEncrypted",
"keepInlineImages",
"optimizeImages",
"removePageLabels",
"reportMemoryUsage",
"rotate",
"collate",
"flattenAnnotations",
"flattenRotation",
"generateAppearances",
"warningExit0",
"jobJsonFile",
] {
schema
.add_dictionary_member(key, scalar.clone())
.expect("static qpdf job schema key is valid");
}
schema
.add_dictionary_member("jsonKey", job_schema_array(scalar.clone()))
.expect("static qpdf job schema jsonKey is valid");
schema
.add_dictionary_member("jsonObject", job_schema_array(scalar.clone()))
.expect("static qpdf job schema jsonObject is valid");
schema
.add_dictionary_member("removeAttachment", job_schema_array(scalar.clone()))
.expect("static qpdf job schema removeAttachment is valid");
schema
.add_dictionary_member("setPageLabels", job_schema_array(scalar.clone()))
.expect("static qpdf job schema setPageLabels is valid");
let attachment = job_schema_dictionary([
("file", scalar.clone()),
("creationdate", scalar.clone()),
("description", scalar.clone()),
("filename", scalar.clone()),
("key", scalar.clone()),
("mimetype", scalar.clone()),
("moddate", scalar.clone()),
("replace", scalar.clone()),
]);
schema
.add_dictionary_member("addAttachment", job_schema_array(attachment))
.expect("static qpdf job schema addAttachment is valid");
let copy_attachments = job_schema_dictionary([
("file", scalar.clone()),
("password", scalar.clone()),
("prefix", scalar.clone()),
]);
schema
.add_dictionary_member("copyAttachmentsFrom", job_schema_array(copy_attachments))
.expect("static qpdf job schema copyAttachmentsFrom is valid");
let pages = job_schema_dictionary([
("file", scalar.clone()),
("password", scalar.clone()),
("range", scalar.clone()),
]);
schema
.add_dictionary_member("pages", job_schema_array(pages))
.expect("static qpdf job schema pages is valid");
let under_overlay = job_schema_dictionary([
("file", scalar.clone()),
("password", scalar.clone()),
("from", scalar.clone()),
("repeat", scalar.clone()),
("to", scalar.clone()),
]);
schema
.add_dictionary_member("overlay", job_schema_array(under_overlay.clone()))
.expect("static qpdf job schema overlay is valid");
schema
.add_dictionary_member("underlay", job_schema_array(under_overlay))
.expect("static qpdf job schema underlay is valid");
let encrypt_40 = job_schema_dictionary([
("annotate", scalar.clone()),
("extract", scalar.clone()),
("modify", scalar.clone()),
("print", scalar.clone()),
]);
let encrypt_128 = job_schema_dictionary([
("accessibility", scalar.clone()),
("annotate", scalar.clone()),
("assemble", scalar.clone()),
("cleartextMetadata", scalar.clone()),
("extract", scalar.clone()),
("form", scalar.clone()),
("modifyOther", scalar.clone()),
("modify", scalar.clone()),
("print", scalar.clone()),
("forceV4", scalar.clone()),
("useAes", scalar.clone()),
]);
let encrypt_256 = job_schema_dictionary([
("accessibility", scalar.clone()),
("annotate", scalar.clone()),
("assemble", scalar.clone()),
("cleartextMetadata", scalar.clone()),
("extract", scalar.clone()),
("form", scalar.clone()),
("modifyOther", scalar.clone()),
("modify", scalar.clone()),
("print", scalar.clone()),
("allowInsecure", scalar.clone()),
("forceR5", scalar.clone()),
]);
let encrypt = job_schema_dictionary([
("userPassword", scalar.clone()),
("ownerPassword", scalar.clone()),
("Bits", crate::json::Json::make_null()),
("40bit", encrypt_40),
("128bit", encrypt_128),
("256bit", encrypt_256),
]);
schema
.add_dictionary_member("encrypt", encrypt)
.expect("static qpdf job schema encrypt is valid");
schema
}
fn validate_job_json_schema(value: &crate::json::Json) -> Result<()> {
let mut errors = Vec::new();
if value.check_schema_with_flags(
&job_json_schema(),
crate::json::SchemaFlags::OPTIONAL,
&mut errors,
) {
return Ok(());
}
let mut message = "qpdf: job json has errors:".to_owned();
for error in errors {
message.push_str("\n ");
message.push_str(&error.to_string());
}
Err(Error::Usage(UsageError::new(message)))
}
fn read_job_json_file(path: &Path) -> Result<crate::json::Json> {
let bytes = std::fs::read(path)
.map_err(|error| Error::file_io("read job-json file", path.to_owned(), error))?;
let value =
crate::json::Json::parse(&bytes).map_err(|error| Error::System(error.to_string()))?;
if !value.is_dictionary() {
return Err(Error::Usage(UsageError::new(
"top-level object is supposed to be a dictionary",
)));
}
validate_job_json_schema(&value)?;
Ok(value)
}
fn job_json_members(
value: &crate::json::Json,
) -> std::collections::BTreeMap<Vec<u8>, crate::json::Json> {
let mut members = std::collections::BTreeMap::new();
value.for_each_dict_item(|key, item| {
members.insert(key.to_vec(), item);
});
members
}
fn job_json_string(
members: &std::collections::BTreeMap<Vec<u8>, crate::json::Json>,
key: &[u8],
) -> Result<Option<Vec<u8>>> {
let Some(value) = members.get(key) else {
return Ok(None);
};
value.get_string().map(Some).ok_or_else(|| {
Error::Usage(UsageError::new(format!(
".{}: value must be a string",
String::from_utf8_lossy(key)
)))
})
}
fn job_json_bare(
members: &std::collections::BTreeMap<Vec<u8>, crate::json::Json>,
key: &[u8],
) -> Result<bool> {
let Some(value) = members.get(key) else {
return Ok(false);
};
let path = format!(".{}", String::from_utf8_lossy(key));
match value.get_string() {
Some(value) if value.is_empty() => Ok(true),
Some(_) => Err(Error::Usage(UsageError::new(format!(
"{path}: value must be the empty string"
)))),
None => Err(Error::Usage(UsageError::new(format!(
"JSON handler: value at {path} is not of expected type"
)))),
}
}
fn job_json_choice(
members: &std::collections::BTreeMap<Vec<u8>, crate::json::Json>,
key: &[u8],
choices: &[&str],
required: bool,
) -> Result<Option<String>> {
let Some(value) = members.get(key) else {
return Ok(None);
};
let path = format!(".{}", String::from_utf8_lossy(key));
let value = value
.get_string()
.ok_or_else(|| Error::Usage(UsageError::new(format!("{path}: value must be a string"))))?;
if !required && value.is_empty() {
return Ok(Some(String::new()));
}
if let Some(choice) = choices.iter().find(|choice| value == choice.as_bytes()) {
return Ok(Some((*choice).to_owned()));
}
Err(Error::Usage(UsageError::new(format!(
"{path}: unexpected value; expected one of {}",
choices.join(", ")
))))
}
fn job_json_items(value: &crate::json::Json) -> Vec<crate::json::Json> {
let mut items = Vec::new();
if value.for_each_array_item(|item| items.push(item)) {
items
} else {
vec![value.clone()]
}
}
fn job_json_required_string(
members: &std::collections::BTreeMap<Vec<u8>, crate::json::Json>,
key: &[u8],
path: &str,
) -> Result<Vec<u8>> {
job_json_string(members, key)?
.ok_or_else(|| Error::Usage(UsageError::new(format!("{path}: value must be a string"))))
}
fn job_json_range(value: Option<&crate::json::Json>, path: &str) -> Result<PageRange> {
job_json_range_with_empty_default(value, path, true)
}
fn job_json_overlay_range(value: Option<&crate::json::Json>, path: &str) -> Result<PageRange> {
job_json_range_with_empty_default(value, path, false)
}
fn job_json_range_with_empty_default(
value: Option<&crate::json::Json>,
path: &str,
empty_is_all: bool,
) -> Result<PageRange> {
let Some(value) = value else {
return Ok(PageRange::all());
};
let bytes = value
.get_string()
.ok_or_else(|| Error::Usage(UsageError::new(format!("{path}: value must be a string"))))?;
if bytes.is_empty() {
return Ok(if empty_is_all {
PageRange::all()
} else {
PageRange::empty()
});
}
let value = String::from_utf8_lossy(&bytes);
PageRange::parse_numrange(&value)
.map_err(|error| Error::Usage(UsageError::new(format!("{path}: {error}"))))
}
fn job_json_yn(
members: &std::collections::BTreeMap<Vec<u8>, crate::json::Json>,
key: &[u8],
) -> Result<Option<bool>> {
Ok(job_json_choice(members, key, &["y", "n"], true)?.map(|value| value == "y"))
}
fn job_json_modify_permission(
value: &str,
permissions: &mut crate::PermissionsConfig,
) -> Result<()> {
let (modify, annotate, forms, assembly) = match value {
"all" => (true, true, true, true),
"annotate" => (false, true, true, true),
"form" => (false, false, true, true),
"assembly" => (false, false, false, true),
"none" => (false, false, false, false),
other => {
return Err(Error::Usage(UsageError::new(format!(
".encrypt: unexpected value; expected one of all, annotate, form, assembly, none (got {other})"
))))
}
};
permissions.modify_contents = modify;
permissions.annotate = annotate;
permissions.fill_forms = forms;
permissions.assemble = assembly;
Ok(())
}
fn job_json_print_permission(
value: &str,
permissions: &mut crate::PermissionsConfig,
) -> Result<()> {
permissions.print = match value {
"full" => crate::PrintPermission::High,
"low" => crate::PrintPermission::Low,
"none" => crate::PrintPermission::None,
other => {
return Err(Error::Usage(UsageError::new(format!(
".encrypt: unexpected value; expected one of full, low, none (got {other})"
))))
}
};
Ok(())
}
fn parse_job_encrypt(value: &crate::json::Json, allow_weak_crypto: bool) -> Result<EncryptParams> {
let members = job_json_members(value);
let user_password = job_json_string(&members, b"userPassword")?;
let owner_password = job_json_string(&members, b"ownerPassword")?;
let (Some(user_password), Some(owner_password)) = (user_password, owner_password) else {
return Err(Error::Usage(UsageError::new(
"the user and owner password are both required; use the empty string for the user password if you don't want a password",
)));
};
let key_lengths = ["40bit", "128bit", "256bit"]
.into_iter()
.filter(|key| members.contains_key(key.as_bytes()))
.collect::<Vec<_>>();
if key_lengths.len() > 1 {
return Err(Error::Usage(UsageError::new(
"exactly one of 40bit, 128bit, or 256bit must be given",
)));
}
let Some(key_length) = key_lengths.first().copied() else {
return Err(Error::Usage(UsageError::new(
"exactly one of 40bit, 128bit, or 256bit must be given; an empty dictionary may be supplied for one of them to set the key length without imposing any restrictions",
)));
};
let settings = members
.get(key_length.as_bytes())
.expect("key length was found in the encryption dictionary");
let settings = job_json_members(settings);
let allow_insecure = job_json_bare(&settings, b"allowInsecure")?;
if key_length == "256bit"
&& owner_password.is_empty()
&& !user_password.is_empty()
&& !allow_insecure
{
return Err(Error::Usage(UsageError::new(
"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.",
)));
}
let mut permissions = crate::PermissionsConfig::default();
if let Some(value) = job_json_yn(&settings, b"accessibility")? {
permissions.accessibility = value;
}
if let Some(value) = job_json_yn(&settings, b"annotate")? {
permissions.annotate = value;
}
if let Some(value) = job_json_yn(&settings, b"assemble")? {
permissions.assemble = value;
}
if let Some(value) = job_json_yn(&settings, b"extract")? {
permissions.extract = value;
}
if let Some(value) = job_json_yn(&settings, b"form")? {
permissions.fill_forms = value;
}
if let Some(value) = job_json_choice(
&settings,
b"modify",
&["all", "annotate", "form", "assembly", "none"],
true,
)? {
job_json_modify_permission(&value, &mut permissions)?;
}
if let Some(value) = job_json_yn(&settings, b"modifyOther")? {
permissions.modify_contents = value;
}
if let Some(value) = job_json_choice(&settings, b"print", &["full", "low", "none"], true)? {
job_json_print_permission(&value, &mut permissions)?;
}
let mut params = match key_length {
"40bit" => EncryptParams::rc4(EncryptMethod::V1Rc440, user_password, owner_password),
"128bit" => {
let use_aes = job_json_choice(&settings, b"useAes", &["y", "n"], true)?;
if use_aes.as_deref() == Some("y") {
EncryptParams::v4_aes128(user_password, owner_password)
} else if job_json_bare(&settings, b"forceV4")? {
EncryptParams::rc4(EncryptMethod::V4Rc4128, user_password, owner_password)
} else {
EncryptParams::rc4(EncryptMethod::V2Rc4128, user_password, owner_password)
}
}
"256bit" => {
if job_json_bare(&settings, b"forceR5")? {
EncryptParams::v5_r5(user_password, owner_password)
} else {
EncryptParams::v5_r6(user_password, owner_password)
}
}
_ => unreachable!("key length was validated above"), };
params.permissions = permissions;
if matches!(
params.method,
EncryptMethod::V4Aes128
| EncryptMethod::V4Rc4128
| EncryptMethod::V5R5Aes256
| EncryptMethod::V5R6Aes256
) {
params.permissions.accessibility = true;
}
if job_json_bare(&settings, b"cleartextMetadata")? {
params.encrypt_metadata = false;
}
if (params.is_weak_rc4() || params.is_deprecated_r5()) && !allow_weak_crypto {
return Err(Error::Usage(UsageError::new(
"refusing to write a file with weak or deprecated encryption without allowWeakCrypto",
)));
}
Ok(params)
}
fn parse_json_decode_level(value: &str) -> crate::writer::DecodeLevel {
match value {
"none" => crate::writer::DecodeLevel::None,
"generalized" => crate::writer::DecodeLevel::Generalized,
"specialized" => crate::writer::DecodeLevel::Specialized,
"all" => crate::writer::DecodeLevel::All,
_ => unreachable!("decode level was validated before conversion"), }
}
fn json_decode_level_for_output(value: crate::writer::DecodeLevel) -> JsonDecodeLevel {
match value {
crate::writer::DecodeLevel::None => JsonDecodeLevel::None,
crate::writer::DecodeLevel::Generalized => JsonDecodeLevel::Generalized,
crate::writer::DecodeLevel::Specialized => JsonDecodeLevel::Specialized,
crate::writer::DecodeLevel::All => JsonDecodeLevel::All,
}
}
fn parse_json_version(value: &str) -> i32 {
match value {
"1" => 1,
"2" | "latest" | "" => 2,
_ => unreachable!("JSON version was validated before conversion"), }
}
fn parse_job_version(value: &[u8], path: &str) -> Result<(String, i64)> {
let value = std::str::from_utf8(value).map_err(|_| {
Error::Usage(UsageError::new(format!(
"{path}: version must be valid UTF-8"
)))
})?;
crate::parse_pdf_version_spec(value)
.ok_or_else(|| Error::Usage(UsageError::new(format!("{path}: invalid version {value}"))))
}
fn qpdf_is_space(byte: u8) -> bool {
matches!(byte, b' ' | b'\n' | b'\r' | b'\t' | b'\x0c' | b'\x0b')
}
fn parse_qpdf_collate_uint(component: &[u8]) -> Result<usize> {
let component = &component[..component
.iter()
.position(|&byte| byte == 0)
.unwrap_or(component.len())];
let mut index = 0;
while component
.get(index)
.is_some_and(|&byte| qpdf_is_space(byte))
{
index += 1;
}
if component.get(index) == Some(&b'-') {
return Err(Error::System(format!(
"underflow converting {} to 64-bit unsigned integer",
String::from_utf8_lossy(component)
)));
}
if component.get(index) == Some(&b'+') {
index += 1;
}
let digit_start = index;
let mut value = 0_u64;
while let Some(&byte @ b'0'..=b'9') = component.get(index) {
value = value
.checked_mul(10)
.and_then(|value| value.checked_add(u64::from(byte - b'0')))
.ok_or_else(|| {
Error::System(format!(
"overflow converting {} to 64-bit unsigned integer",
String::from_utf8_lossy(component)
))
})?;
index += 1;
}
if index == digit_start {
return Ok(0);
}
if value > u64::from(u32::MAX) {
return Err(Error::System(format!(
"integer out of range converting {value} from a 8-byte unsigned type to a 4-byte unsigned type"
)));
}
Ok(value as usize)
}
fn parse_qpdf_collate_parameter(parameter: &[u8]) -> Result<Vec<usize>> {
if parameter.is_empty() {
return Ok(vec![1]);
}
let mut values = Vec::new();
let mut position = 0;
loop {
let end = parameter[position..]
.iter()
.position(|&byte| byte == b',')
.map(|offset| position + offset);
let count = end.unwrap_or(usize::MAX);
let component_end = position.saturating_add(count).min(parameter.len());
let component = ¶meter[position..component_end];
if component.is_empty() {
return Err(Error::Usage(UsageError::new("--collate: trailing comma")));
}
values.push(parse_qpdf_collate_uint(component)?);
let Some(end) = end else {
break;
};
position = end + 1;
}
Ok(values)
}
fn parse_job_split_pages(value: &[u8]) -> Result<i32> {
if value.is_empty() {
return Ok(1);
}
let text = String::from_utf8_lossy(value);
match qpdf_string_to_int_checked(&text) {
QpdfIntParse::NoDigits => Ok(0),
QpdfIntParse::Value(count) => Ok(count),
QpdfIntParse::Overflow(_) => Err(Error::Usage(UsageError::new(format!(
".splitPages: invalid page count {text}"
)))),
}
}
fn parse_job_compression_level(value: &[u8]) -> Result<i32> {
let text = String::from_utf8_lossy(value);
match qpdf_string_to_int_checked(&text) {
QpdfIntParse::NoDigits => Ok(0),
QpdfIntParse::Value(level) => Ok(level),
QpdfIntParse::Overflow(message) => Err(Error::System(message)),
}
}
fn parse_job_object_selector(value: &[u8]) -> Result<JobObjectSelector> {
let value = String::from_utf8_lossy(value);
if value == "trailer" {
return Ok(JobObjectSelector::Trailer);
}
let (number, generation) = value.split_once(',').unwrap_or((&value, "0"));
let number = parse_job_selector_integer(number)?;
let generation = if generation.is_empty() {
0
} else {
parse_job_selector_integer(generation)?
};
if number <= 0 {
return Ok(JobObjectSelector::NoObject);
}
if !(0..=i32::from(u16::MAX)).contains(&generation) {
return Ok(JobObjectSelector::Null);
}
Ok(JobObjectSelector::Object(ObjectRef::new(
u32::try_from(number).expect("positive i32 fits u32"),
u16::try_from(generation).expect("validated u16 generation"),
)))
}
fn parse_job_selector_integer(value: &str) -> Result<i32> {
let original = value;
let value = value.trim_start_matches(|character| {
matches!(
character,
' ' | '\n' | '\r' | '\t' | '\u{000c}' | '\u{000b}'
)
});
let sign_len = usize::from(matches!(value.as_bytes().first(), Some(b'+') | Some(b'-')));
let digits_end = sign_len
+ value[sign_len..]
.bytes()
.take_while(u8::is_ascii_digit)
.count();
if digits_end == sign_len {
return Ok(0);
}
let prefix = &value[..digits_end];
let parsed = prefix.parse::<i128>().map_err(|_| {
Error::Usage(UsageError::new(format!(
"overflow/underflow converting {original} to 64-bit integer"
)))
})?;
if !(i128::from(i64::MIN)..=i128::from(i64::MAX)).contains(&parsed) {
return Err(Error::Usage(UsageError::new(format!(
"overflow/underflow converting {original} to 64-bit integer"
))));
}
let parsed = parsed as i64;
i32::try_from(parsed).map_err(|_| {
Error::Usage(UsageError::new(format!(
"integer out of range converting {parsed} from a 8-byte signed type to a 4-byte signed type"
)))
})
}
fn parse_job_attachment(value: &crate::json::Json, path: &str) -> Result<AttachmentAddOptions> {
let members = job_json_members(value);
let file = job_json_required_string(&members, b"file", &format!("{path}.file"))?;
let path = path_from_qpdf_json_bytes(&file);
let basename = path
.file_name()
.map(path_component_to_qpdf_bytes)
.filter(|value| !value.is_empty())
.ok_or_else(|| {
Error::Usage(UsageError::new(
"file for --add-attachment may not be empty",
))
})?;
let filename = job_json_string(&members, b"filename")?.unwrap_or_else(|| basename.clone());
let key = job_json_string(&members, b"key")?.unwrap_or(basename);
let creation_date = job_json_string(&members, b"creationdate")?;
let modification_date = job_json_string(&members, b"moddate")?;
Ok(AttachmentAddOptions {
path,
key,
filename,
mimetype: job_json_string(&members, b"mimetype")?,
description: job_json_string(&members, b"description")?,
creation_date,
modification_date,
replace: job_json_bare(&members, b"replace")?,
verbose: false,
})
}
fn parse_job_overlay_specs(
destination: &mut Vec<JobOverlayConfig>,
value: &crate::json::Json,
kind: OverlayKind,
) -> Result<()> {
for (index, item) in job_json_items(value).into_iter().enumerate() {
let members = job_json_members(&item);
let file = job_json_string(&members, b"file")?.ok_or_else(|| {
Error::Usage(UsageError::new(
"file is required in underlay/overlay specification",
))
})?;
let from = job_json_overlay_range(
members.get(b"from".as_slice()),
&format!(
".{}[{index}].from",
match kind {
OverlayKind::Overlay => "overlay",
OverlayKind::Underlay => "underlay",
}
),
)?; let to = job_json_overlay_range(
members.get(b"to".as_slice()),
&format!(
".{}[{index}].to",
match kind {
OverlayKind::Overlay => "overlay",
OverlayKind::Underlay => "underlay",
}
),
)?; let repeat = members
.get(b"repeat".as_slice())
.map(|value| job_json_overlay_range(Some(value), "underlay/overlay repeat"))
.transpose()?;
destination.push(JobOverlayConfig {
path: path_from_qpdf_json_bytes(&file),
password: job_json_string(&members, b"password")?.unwrap_or_default(),
from,
to,
repeat,
kind,
});
}
Ok(())
}
const PAGE_LABEL_SPEC_ERROR: &str = "page label spec must be n:[D|a|A|r|R][/start[/prefix]]";
fn page_label_spec_error() -> Error {
Error::Usage(UsageError::new(PAGE_LABEL_SPEC_ERROR))
}
fn parse_decimal_bytes(value: &[u8]) -> Option<i64> {
if value.is_empty() || !value.iter().all(u8::is_ascii_digit) {
return None;
}
value.iter().try_fold(0i64, |value, digit| {
value
.checked_mul(10)
.and_then(|value| value.checked_add(i64::from(digit - b'0')))
})
}
fn parse_page_label_spec(spec: &[u8]) -> Result<PageLabelSpec> {
let Some(colon) = spec.iter().position(|byte| *byte == b':') else {
return Err(page_label_spec_error());
};
let first_page = &spec[..colon];
let label_spec = &spec[colon + 1..];
let first_page = if first_page == b"z" {
-1
} else if let Some(value) = first_page.strip_prefix(b"r") {
let value = parse_decimal_bytes(value).ok_or_else(page_label_spec_error)?;
value.checked_neg().ok_or_else(page_label_spec_error)?
} else {
parse_decimal_bytes(first_page).ok_or_else(page_label_spec_error)?
};
let mut parts = label_spec.splitn(3, |byte| *byte == b'/');
let style = match parts.next().unwrap_or_default() {
b"" => crate::page_label_document_helper::LabelStyle::None,
b"D" => crate::page_label_document_helper::LabelStyle::Decimal,
b"a" => crate::page_label_document_helper::LabelStyle::AlphaLower,
b"A" => crate::page_label_document_helper::LabelStyle::AlphaUpper,
b"r" => crate::page_label_document_helper::LabelStyle::RomanLower,
b"R" => crate::page_label_document_helper::LabelStyle::RomanUpper,
_ => return Err(page_label_spec_error()),
};
let start = match parts.next() {
None | Some(b"") => 1,
Some(value) => parse_decimal_bytes(value).ok_or_else(page_label_spec_error)?,
};
if start < 1 {
return Err(Error::Usage(UsageError::new(
"starting page number must be >= 1",
)));
}
let prefix = parts.next().map_or_else(Vec::new, ToOwned::to_owned);
Ok(PageLabelSpec {
first_page,
style,
start,
prefix,
})
}
fn parse_job_page_labels(
specs: &[PageLabelSpec],
page_count: usize,
) -> Result<Vec<(i64, PageLabelSpec)>> {
let page_count = i64::try_from(page_count)
.map_err(|_| Error::Unsupported("page count exceeds qpdf's range".to_owned()))?;
let mut entries = Vec::with_capacity(specs.len());
let mut last_page = 0i64;
for spec in specs {
let first_page = if spec.first_page < 0 {
page_count
.checked_add(1)
.and_then(|value| value.checked_add(spec.first_page))
.ok_or_else(|| Error::Unsupported("page label page overflow".to_owned()))?
} else {
spec.first_page
};
if entries.is_empty() {
if first_page != 1 {
return Err(Error::SystemBytes(
b"the first page label specification must start with page 1".to_vec(),
));
}
} else if first_page <= last_page {
return Err(Error::SystemBytes(
b"page label specifications must be in order by first page".to_vec(),
));
}
if first_page < 1 || first_page > page_count {
return Err(Error::SystemBytes(
format!(
"page label spec: page {first_page} is more than the total number of pages ({page_count})"
)
.into_bytes(),
));
}
entries.push((first_page - 1, spec.clone()));
last_page = first_page;
}
Ok(entries)
}
fn expand_arg_files(argv: &[String]) -> Result<Vec<String>> {
let mut expanded = Vec::with_capacity(argv.len());
let mut iter = argv.iter();
if let Some(program) = iter.next() {
expanded.push(program.clone());
}
for argument in iter {
let Some(path) = argument.strip_prefix('@').filter(|path| !path.is_empty()) else {
expanded.push(argument.clone());
continue;
};
match read_argument_file_lines(path)? {
Some(lines) => expanded.extend(lines),
None => expanded.push(argument.clone()),
}
}
Ok(expanded)
}
fn read_argument_file_lines(path: &str) -> Result<Option<Vec<String>>> {
let bytes = if path == "-" {
let mut bytes = Vec::new();
std::io::stdin()
.read_to_end(&mut bytes)
.map_err(|error| Error::file_io("read argument file", "-", error))?;
bytes
} else {
let mut file = match File::open(path) {
Ok(file) => file,
Err(_) => return Ok(None),
};
let mut bytes = Vec::new();
file.read_to_end(&mut bytes)
.map_err(|error| Error::file_io("read argument file", path, error))?;
bytes
};
Ok(Some(split_argument_file_lines(&bytes)))
}
fn split_argument_file_lines(bytes: &[u8]) -> Vec<String> {
let text = String::from_utf8_lossy(bytes);
let mut lines = Vec::new();
let mut line = String::new();
for ch in text.chars() {
if ch == '\n' {
if line.ends_with('\r') {
line.pop();
}
lines.push(std::mem::take(&mut line));
} else {
line.push(ch);
}
}
if !line.is_empty() {
lines.push(line);
}
lines
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(i32)]
pub enum JobExitCode {
Success = 0,
Error = 2,
Warning = 3,
}
#[derive(Debug, Clone, Copy, Default)]
struct EncryptionStatus {
encrypted: bool,
password_incorrect: bool,
}
impl JobExitCode {
#[must_use]
pub const fn as_i32(self) -> i32 {
self as i32
}
}
pub struct QPDFJob {
logger: QPDFLogger,
input_name: String,
input_name_bytes: Vec<u8>,
message_prefix: String,
warnings: bool,
suppress_warnings: bool,
warnings_exit_zero: bool,
progress_handler: Option<SharedProgressHandler>,
configuration: JobConfiguration,
page_source_documents: Vec<JobDocument>,
overlay_sources: Vec<OverlaySpec<Box<dyn ReadSeek>>>,
primary_copy_encryption: Option<crate::CopyEncryptionSource>,
encryption_status: EncryptionStatus,
create_qpdf_succeeded_without_document: bool,
empty_primary_created: bool,
partial_json_initialized: bool,
}
pub struct QPDFJobConfig<'a> {
job: &'a mut QPDFJob,
}
impl Default for QPDFJob {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug)]
enum InspectionFailure {
Reported(Error),
Unreported(Error),
}
impl From<Error> for InspectionFailure {
fn from(error: Error) -> Self {
Self::Unreported(error)
}
}
impl QPDFJob {
#[must_use]
pub fn config(&mut self) -> QPDFJobConfig<'_> {
QPDFJobConfig { job: self }
}
pub fn parse_collate(value: &str) -> Result<Vec<usize>> {
parse_qpdf_collate_parameter(value.as_bytes())
}
pub fn parse_keep_files_open_threshold(value: &str) -> Result<usize> {
parse_qpdf_collate_uint(value.as_bytes())
}
#[must_use]
pub fn new() -> Self {
Self {
logger: QPDFLogger::default_logger(),
input_name: String::new(),
input_name_bytes: Vec::new(),
message_prefix: "qpdf".to_owned(),
warnings: false,
suppress_warnings: false,
warnings_exit_zero: false,
progress_handler: None,
configuration: qpdf_default_job_configuration(),
page_source_documents: Vec::new(),
overlay_sources: Vec::new(),
primary_copy_encryption: None,
encryption_status: EncryptionStatus::default(),
create_qpdf_succeeded_without_document: false,
empty_primary_created: false,
partial_json_initialized: false,
}
}
#[must_use]
pub fn logger(&self) -> QPDFLogger {
self.logger.clone()
}
pub fn set_logger(&mut self, logger: QPDFLogger) {
self.logger = logger;
}
pub fn set_output_streams(
&mut self,
output: Option<PipelineHandle>,
error: Option<PipelineHandle>,
) {
let logger = QPDFLogger::create();
logger.set_output_streams(output, error);
self.logger = logger;
}
pub fn set_message_prefix(&mut self, message_prefix: impl Into<String>) {
self.message_prefix = message_prefix.into();
}
pub fn set_input_name(&mut self, input_name: impl Into<String>) {
let input_name = input_name.into();
self.input_name_bytes = input_name.as_bytes().to_vec();
self.input_name = input_name;
}
#[must_use]
pub fn input_name(&self) -> &str {
&self.input_name
}
#[must_use]
pub fn input_name_bytes(&self) -> &[u8] {
&self.input_name_bytes
}
pub fn set_input_name_bytes(&mut self, input_name: impl AsRef<[u8]>) {
self.input_name_bytes = input_name.as_ref().to_vec();
self.input_name = String::from_utf8_lossy(&self.input_name_bytes).into_owned();
}
#[must_use]
pub fn input_version_floor(&self) -> Option<PdfVersion> {
let (version, extension_level) = self.configuration.max_input_version.as_ref()?;
let version = crate::parse_pdf_version(version)?;
Some(PdfVersion::new(
version.major(),
version.minor(),
*extension_level,
))
}
pub fn set_input_file(&mut self, input_file: impl Into<PathBuf>) -> Result<()> {
if self.configuration.input_file.is_some() || self.configuration.empty_input {
return Err(Error::Usage(UsageError::new(
"input file has already been given",
)));
}
let input_file = input_file.into();
self.set_input_name_bytes(path_description_bytes(&input_file));
self.configuration.input_file = Some(input_file);
Ok(())
}
pub fn set_output_file(&mut self, output_file: impl Into<PathBuf>) -> Result<()> {
if self.configuration.output_file.is_some() || self.configuration.replace_input {
return Err(Error::Usage(UsageError::new(
"output file has already been given",
)));
}
self.configuration.output_file = Some(output_file.into());
Ok(())
}
pub fn set_password(&mut self, password: impl Into<Vec<u8>>) {
self.configuration.password = password.into();
}
pub fn set_progress(&mut self, value: bool) {
self.configuration.progress = value;
}
pub fn set_verbose(&mut self, value: bool) {
self.configuration.verbose = value;
}
pub fn set_writer_configuration(&mut self, configuration: WriterConfiguration) {
self.configuration.writer = configuration;
}
pub fn set_content_normalization(&mut self, value: bool) {
self.configuration.normalize_content = Some(value);
}
pub(crate) fn content_normalization_enabled(&self) -> bool {
self.configuration.normalize_content.unwrap_or(false)
}
pub fn set_suppress_recovery(&mut self, value: bool) {
self.configuration.suppress_recovery = value;
}
pub fn set_ignore_xref_streams(&mut self, value: bool) {
self.configuration.ignore_xref_streams = value;
}
pub fn set_password_mode(&mut self, value: PasswordMode) {
self.configuration.password_mode = value;
}
pub fn set_password_is_hex_key(&mut self, value: bool) {
self.configuration.password_is_hex_key = value;
}
pub fn set_suppress_password_recovery(&mut self, value: bool) {
self.configuration.suppress_password_recovery = value;
}
pub fn set_allow_weak_crypto(&mut self, value: bool) {
self.configuration.allow_weak_crypto = value;
}
pub fn set_linearization(&mut self, value: bool, pass1: Option<PathBuf>) {
self.configuration.linearize = value;
self.configuration.linearize_pass1 = pass1;
}
pub fn set_keep_files_open(&mut self, value: bool) {
self.configuration.keep_files_open = Some(value);
}
pub fn set_keep_files_open_threshold(&mut self, value: usize) {
self.configuration.keep_files_open_threshold = Some(value);
}
#[must_use]
pub fn keep_files_open_for_page_specs(&self, specs: &[PageSpecInput]) -> bool {
self.configuration.keep_files_open.unwrap_or_else(|| {
let distinct_sources = specs
.iter()
.map(|spec| spec.source_index)
.collect::<BTreeSet<_>>()
.len();
distinct_sources
<= self
.configuration
.keep_files_open_threshold
.unwrap_or(DEFAULT_KEEP_FILES_OPEN_THRESHOLD)
})
}
#[must_use]
pub(crate) fn object_stream_mode_for_page_specs(&self) -> crate::ObjectStreamMode {
self.configuration.writer.object_stream_mode()
}
pub fn report_page_spec_selection(&self, specs: &[PageSpecInput]) -> Result<()> {
if !self.configuration.verbose || self.configuration.keep_files_open.is_some() {
return Ok(());
}
let mut message = self.message_prefix.as_bytes().to_vec();
message.extend_from_slice(b": selecting --keep-open-files=");
message.extend_from_slice(if self.keep_files_open_for_page_specs(specs) {
b"y\n"
} else {
b"n\n"
});
self.logger.info(message)
}
pub fn report_page_source_processing(&self, source_name: impl AsRef<[u8]>) -> Result<()> {
if !self.configuration.verbose {
return Ok(());
}
let mut message = self.message_prefix.as_bytes().to_vec();
message.extend_from_slice(b": processing ");
message.extend_from_slice(source_name.as_ref());
message.push(b'\n');
self.logger.info(message)
}
pub(crate) fn verbose(&self) -> bool {
self.configuration.verbose
}
pub fn set_show_encryption_key(&mut self, show: bool) {
self.configuration.show_encryption_key = show;
}
pub fn set_with_images(&mut self, show: bool) {
self.configuration.show_page_images = show;
}
pub(crate) fn show_page_images(&self) -> bool {
self.configuration.show_page_images
}
pub(crate) fn show_encryption_key(&self) -> bool {
self.configuration.show_encryption_key
}
#[must_use]
pub fn message_prefix(&self) -> &str {
&self.message_prefix
}
pub fn register_progress_reporter<F>(&mut self, reporter: F)
where
F: FnMut(u8) -> Result<()> + 'static,
{
self.progress_handler = Some(Rc::new(RefCell::new(Box::new(reporter))));
}
pub fn configure_writer_progress<R>(&self, writer: &mut PdfWriter<'_, R>)
where
R: Read + Seek + 'static,
{
let reporter = match self.progress_handler.as_ref() {
Some(reporter) => Rc::clone(reporter),
None if self.configuration.progress => {
let logger = self.logger.clone();
let prefix = self.message_prefix.clone();
let output_name = self
.output_destination()
.filter(|path| path.as_path() != Path::new("-"))
.map_or_else(
|| "standard output".to_owned(),
|path| path.display().to_string(),
);
let callback: ProgressHandler = Box::new(move |percent| {
logger.info(format!(
"{prefix}: {output_name}: write progress: {percent}%\n"
))
});
Rc::new(RefCell::new(callback))
}
None => return,
};
writer
.register_progress_reporter(Box::new(move |percent| (reporter.borrow_mut())(percent)));
}
pub fn initialize_from_argv(&mut self, argv: &[String]) -> Result<()> {
let argv = expand_arg_files(argv)?;
let mut configuration = JobConfiguration::default();
let mut positionals = Vec::new();
let mut page_label_specs: Option<Vec<String>> = None;
for argument in argv.iter().skip(1) {
if let Some(specs) = page_label_specs.as_mut() {
if argument == "--" {
let parsed = std::mem::take(specs)
.into_iter()
.map(|spec| parse_page_label_spec(spec.as_bytes()))
.collect::<Result<Vec<_>>>()?;
configuration.set_page_labels = Some(parsed);
page_label_specs = None;
continue;
}
if argument.len() > 1 && argument.starts_with('-') {
return Err(UsageError::new(format!(
"unrecognized argument {argument} (set page labels options must be terminated with --)"
))
.into());
}
specs.push(argument.clone());
continue;
}
if argument == "--" {
continue;
}
if argument.starts_with("--") {
match argument.as_str() {
"--remove-page-labels" => configuration.remove_page_labels = true,
"--set-page-labels" => page_label_specs = Some(Vec::new()),
"--deterministic-id" => configuration.writer.set_deterministic_id(true),
"--static-id" => configuration.writer.set_static_id(true),
"--replace-input" => {
if configuration.replace_input {
return Err(UsageError::new(
"replace-input can't be used since output file has already been given",
)
.into());
}
configuration.replace_input = true;
}
"--decrypt" => {
configuration.writer.set_preserve_encryption(false);
}
"--progress" => configuration.progress = true,
"--check" => configuration.check = true,
_ if argument.starts_with("--keep-files-open=") => {
let value = &argument["--keep-files-open=".len()..];
configuration.keep_files_open = match value {
"y" => Some(true),
"n" => Some(false),
_ => {
return Err(UsageError::new(format!(
"invalid value for --keep-files-open: {value}"
))
.into())
}
};
}
_ if argument.starts_with("--keep-files-open-threshold=") => {
let value = &argument["--keep-files-open-threshold=".len()..];
configuration.keep_files_open_threshold =
Some(parse_qpdf_collate_uint(value.as_bytes())?);
}
_ if argument.starts_with("--password=") => {
configuration.password = argument.as_bytes()[11..].to_vec();
}
_ if argument.starts_with("--object-streams=") => {
configuration
.writer
.set_object_stream_mode(parse_object_stream_mode(&argument[17..])?);
}
_ => {
return Err(
UsageError::new(format!("unrecognized argument {argument}")).into()
);
}
}
} else if argument.starts_with('-') {
return Err(UsageError::new(format!("unrecognized argument {argument}")).into());
} else {
positionals.push(argument.clone());
}
}
if page_label_specs.is_some() {
return Err(UsageError::new("missing -- at end of set page labels options").into());
}
if positionals.len() > 2 {
return Err(UsageError::new(format!("unknown argument {}", positionals[2])).into());
}
configuration.input_file = positionals.first().map(PathBuf::from);
configuration.output_file = positionals.get(1).map(PathBuf::from);
if configuration.replace_input && configuration.output_file.is_some() {
return Err(UsageError::new(
"replace-input can't be used since output file has already been given",
)
.into());
}
if configuration.input_file.is_none() && !configuration.check {
return Err(UsageError::new("an input file name is required").into());
}
self.configuration = configuration;
let input_name = self
.configuration
.input_file
.as_ref()
.map_or_else(String::new, |path| path.display().to_string());
self.set_input_name(input_name);
self.warnings = false;
Ok(())
}
pub fn initialize_from_json(&mut self, json: &str) -> Result<()> {
self.initialize_from_json_bytes(json.as_bytes())
}
pub fn initialize_from_json_bytes(&mut self, json: &[u8]) -> Result<()> {
self.initialize_from_json_with_partial(json, false)
}
pub fn initialize_from_json_partial(&mut self, json: &str) -> Result<()> {
self.initialize_from_json_partial_bytes(json.as_bytes())
}
pub fn initialize_from_json_partial_bytes(&mut self, json: &[u8]) -> Result<()> {
self.initialize_from_json_with_partial(json, true)
}
fn configured_open_options(&self, password: Vec<u8>) -> PdfOpenOptions {
PdfOpenOptions {
repair: !self.configuration.suppress_recovery,
ignore_xref_streams: self.configuration.ignore_xref_streams,
password,
password_mode: self.configuration.password_mode,
suppress_password_recovery: self.configuration.suppress_password_recovery,
password_is_hex_key: self.configuration.password_is_hex_key,
verbose: self.configuration.verbose,
message_prefix: self.message_prefix.as_bytes().to_vec(),
..PdfOpenOptions::default()
}
}
fn initialize_from_json_with_partial(&mut self, json: &[u8], partial: bool) -> Result<()> {
self.set_message_prefix("qpdfjob json");
let value =
crate::json::Json::parse(json).map_err(|error| Error::System(error.to_string()))?;
if !value.is_dictionary() {
return Err(Error::Usage(UsageError::new(
"top-level object is supposed to be a dictionary",
)));
}
validate_job_json_schema(&value)?;
let mut configuration = if partial && self.partial_json_initialized {
self.configuration.clone()
} else {
let mut configuration = qpdf_default_job_configuration();
configuration.require_output = true;
configuration.json_decode_level = crate::writer::DecodeLevel::Generalized;
configuration.page_specs = self.configuration.page_specs.clone();
configuration.page_specs_origin = self.configuration.page_specs_origin;
configuration
};
self.dispatch_job_json_document(&mut configuration, &value, &mut BTreeSet::new())?;
self.configuration = configuration;
self.partial_json_initialized = partial;
let input_name = self
.configuration
.input_file
.as_ref()
.map_or_else(String::new, |path| path.display().to_string());
self.set_input_name(input_name);
self.warnings = false;
if !partial {
self.check_configuration()?;
}
Ok(())
}
fn dispatch_job_json_document(
&mut self,
configuration: &mut JobConfiguration,
value: &crate::json::Json,
active: &mut BTreeSet<PathBuf>,
) -> Result<()> {
validate_job_json_schema(value)?;
for (key, item) in job_json_members(value) {
if key == b"jobJsonFile" {
let mut members = std::collections::BTreeMap::new();
members.insert(key.clone(), item);
let path_bytes = job_json_string(&members, b"jobJsonFile")?
.expect("jobJsonFile member is present in the one-member dictionary");
let path = path_from_qpdf_json_bytes(&path_bytes);
let identity = std::fs::canonicalize(&path).unwrap_or_else(|_| path.clone());
if !active.insert(identity.clone()) {
return Err(Error::Usage(UsageError::new(format!(
"recursive jobJsonFile reference: {}",
path.display()
))));
}
let nested_result = (|| {
let nested = read_job_json_file(&path)?;
self.dispatch_job_json_document(configuration, &nested, active)
})();
active.remove(&identity);
nested_result.map_err(|error| {
Error::System(format!(
"error with job-json file {}: {error}\nRun {} --job-json-help for information on the file format.",
path.display(),
self.message_prefix
))
})?;
} else {
let mut members = std::collections::BTreeMap::new();
members.insert(key, item);
self.apply_job_json_members(configuration, members)?;
}
}
Ok(())
}
fn apply_job_json_members(
&mut self,
configuration: &mut JobConfiguration,
members: std::collections::BTreeMap<Vec<u8>, crate::json::Json>,
) -> Result<()> {
if let Some(input) = job_json_string(&members, b"inputFile")? {
if configuration.input_file.is_some() || configuration.empty_input {
return Err(Error::Usage(UsageError::new(
"input file has already been given",
)));
}
if input.is_empty() {
configuration.empty_input = true;
} else {
configuration.input_file = Some(path_from_qpdf_json_bytes(&input));
}
}
if job_json_bare(&members, b"empty")? {
if configuration.input_file.is_some() || configuration.empty_input {
return Err(Error::Usage(UsageError::new(
"empty input can't be used since input file has already been given",
)));
}
configuration.empty_input = true;
}
if let Some(output) = job_json_string(&members, b"outputFile")? {
if configuration.output_file.is_some() || configuration.replace_input {
return Err(Error::Usage(UsageError::new(
"output file has already been given",
)));
}
configuration.output_file = Some(path_from_qpdf_json_bytes(&output));
}
if let Some(copy_encryption) = job_json_string(&members, b"copyEncryption")? {
configuration.copy_encryption = Some(path_from_qpdf_json_bytes(©_encryption));
configuration.writer.clear_encryption_parameters();
}
if members.contains_key(b"encryptionFilePassword".as_slice()) {
configuration.encryption_file_password =
job_json_string(&members, b"encryptionFilePassword")?.unwrap_or_default();
}
if job_json_bare(&members, b"replaceInput")? {
if configuration.output_file.is_some() || configuration.replace_input {
return Err(Error::Usage(UsageError::new(
"replace-input can't be used since output file has already been given",
)));
}
configuration.replace_input = true;
}
if members.contains_key(b"password".as_slice()) {
configuration.password = job_json_string(&members, b"password")?.unwrap_or_default();
}
if let Some(password_file) = job_json_string(&members, b"passwordFile")? {
let path = path_from_qpdf_json_bytes(&password_file);
let bytes = std::fs::read(&path)
.map_err(|error| Error::file_io("read password file", path.clone(), error))?;
let first_line_len = bytes
.iter()
.position(|&byte| byte == b'\n')
.unwrap_or(bytes.len());
let mut first_line = bytes[..first_line_len].to_vec();
if first_line.ends_with(b"\r") {
first_line.pop();
}
configuration.password = first_line;
}
if job_json_bare(&members, b"ignoreXrefStreams")? {
configuration.ignore_xref_streams = true;
}
if job_json_bare(&members, b"passwordIsHexKey")? {
configuration.password_is_hex_key = true;
}
if job_json_bare(&members, b"suppressPasswordRecovery")? {
configuration.suppress_password_recovery = true;
}
if job_json_bare(&members, b"suppressRecovery")? {
configuration.suppress_recovery = true;
}
if let Some(value) = job_json_choice(
&members,
b"passwordMode",
&["bytes", "hex-bytes", "unicode", "auto"],
true,
)? {
configuration.password_mode = match value.as_str() {
"bytes" => PasswordMode::Bytes,
"hex-bytes" => PasswordMode::HexBytes,
"unicode" => PasswordMode::Unicode,
"auto" => PasswordMode::Auto,
_ => unreachable!("passwordMode was validated above"), };
}
if job_json_bare(&members, b"jsonInput")? {
configuration.json_input = true;
}
if job_json_bare(&members, b"qdf")? {
configuration.writer.set_qdf_mode(true);
}
if job_json_bare(&members, b"preserveUnreferenced")? {
configuration.writer.set_preserve_unreferenced_objects(true);
}
if job_json_bare(&members, b"newlineBeforeEndstream")? {
configuration.writer.set_newline_before_endstream(true);
}
if let Some(value) = job_json_choice(&members, b"normalizeContent", &["y", "n"], true)? {
configuration.normalize_content = Some(value == "y");
}
if let Some(value) = job_json_choice(
&members,
b"streamData",
&["compress", "preserve", "uncompress"],
true,
)? {
configuration
.writer
.set_stream_data_mode(match value.as_str() {
"compress" => crate::StreamDataMode::Compress,
"preserve" => crate::StreamDataMode::Preserve,
"uncompress" => crate::StreamDataMode::Uncompress,
_ => unreachable!("streamData was validated above"), });
}
if let Some(value) = job_json_choice(&members, b"compressStreams", &["y", "n"], true)? {
configuration.writer.set_compress_streams(value == "y");
}
if job_json_bare(&members, b"recompressFlate")? {
configuration.writer.set_recompress_flate(true);
}
if let Some(value) = job_json_string(&members, b"compressionLevel")? {
configuration
.writer
.set_compression_level(parse_job_compression_level(&value)?);
}
if let Some(value) = job_json_choice(
&members,
b"decodeLevel",
&["none", "generalized", "specialized", "all"],
true,
)? {
let level = parse_json_decode_level(&value);
configuration.writer.set_decode_level(level);
configuration.json_decode_level = level;
configuration.json_decode_level_set = true;
}
if job_json_bare(&members, b"decrypt")? {
configuration.writer.set_preserve_encryption(false);
configuration.writer.clear_encryption_parameters();
configuration.copy_encryption = None;
}
if job_json_bare(&members, b"deterministicId")? {
configuration.writer.set_deterministic_id(true);
}
if job_json_bare(&members, b"staticAesIv")? {
configuration.writer.set_static_aes_iv(true);
}
if job_json_bare(&members, b"staticId")? {
configuration.writer.set_static_id(true);
}
if job_json_bare(&members, b"noOriginalObjectIds")? {
configuration.writer.set_suppress_original_object_ids(true);
}
if job_json_bare(&members, b"allowWeakCrypto")? {
configuration.allow_weak_crypto = true;
}
if job_json_bare(&members, b"progress")? {
configuration.progress = true;
}
if job_json_bare(&members, b"verbose")? {
configuration.verbose = true;
}
if let Some(value) = job_json_yn(&members, b"keepFilesOpen")? {
configuration.keep_files_open = Some(value);
}
if let Some(value) = job_json_string(&members, b"keepFilesOpenThreshold")? {
configuration.keep_files_open_threshold = Some(parse_qpdf_collate_uint(&value)?);
}
if let Some(value) = job_json_string(&members, b"splitPages")? {
configuration.split_pages = Some(parse_job_split_pages(&value)?);
}
if let Some(value) = job_json_string(&members, b"rotate")? {
let value = &value[..value
.iter()
.position(|&byte| byte == 0)
.unwrap_or(value.len())];
let rotation = parse_rotation_parameter(value)?;
configuration
.rotations
.insert(rotation.range, rotation.spec);
}
if job_json_bare(&members, b"removeRestrictions")? {
configuration.remove_restrictions = true;
}
if job_json_bare(&members, b"coalesceContents")? {
configuration.coalesce_contents = true;
}
if job_json_bare(&members, b"externalizeInlineImages")? {
configuration.externalize_inline_images = true;
}
if job_json_bare(&members, b"keepInlineImages")? {
configuration.image_options.keep_inline_images = true;
}
if job_json_bare(&members, b"optimizeImages")? {
configuration.optimize_images = true;
}
if let Some(value) = job_json_string(&members, b"iiMinBytes")? {
configuration.image_options.inline_min_bytes =
parse_qpdf_collate_uint(&value)? as usize;
}
if let Some(value) = job_json_string(&members, b"oiMinArea")? {
configuration.image_options.min_area = parse_qpdf_collate_uint(&value)? as u32;
}
if let Some(value) = job_json_string(&members, b"oiMinHeight")? {
configuration.image_options.min_height = parse_qpdf_collate_uint(&value)? as u32;
}
if let Some(value) = job_json_string(&members, b"oiMinWidth")? {
configuration.image_options.min_width = parse_qpdf_collate_uint(&value)? as u32;
}
if let Some(value) = job_json_choice(
&members,
b"flattenAnnotations",
&["all", "print", "screen"],
true,
)? {
configuration.flatten_annotations = Some(match value.as_str() {
"all" => FlattenAnnotationsMode::All,
"print" => FlattenAnnotationsMode::Print,
"screen" => FlattenAnnotationsMode::Screen,
_ => unreachable!("flattenAnnotations was validated above"), });
}
if job_json_bare(&members, b"flattenRotation")? {
configuration.flatten_rotation = true;
}
if job_json_bare(&members, b"generateAppearances")? {
configuration.generate_appearances = true;
}
if let Some(value) = job_json_choice(
&members,
b"objectStreams",
&["disable", "preserve", "generate"],
true,
)? {
configuration
.writer
.set_object_stream_mode(parse_object_stream_mode(&value)?);
}
if let Some(value) = job_json_string(&members, b"minVersion")? {
let (version, extension) = parse_job_version(&value, ".minVersion")?;
configuration
.writer
.set_minimum_pdf_version(version, extension);
}
if let Some(value) = job_json_string(&members, b"forceVersion")? {
let (version, extension) = parse_job_version(&value, ".forceVersion")?;
configuration.writer.force_pdf_version(version, extension);
}
if let Some(value) = job_json_string(&members, b"linearizePass1")? {
configuration.linearize_pass1 = Some(path_from_qpdf_json_bytes(&value));
}
if job_json_bare(&members, b"linearize")? {
configuration.linearize = true;
}
if let Some(value) = job_json_string(&members, b"updateFromJson")? {
configuration.update_from_json = Some(path_from_qpdf_json_bytes(&value));
}
if let Some(value) = job_json_string(&members, b"collate")? {
let value = String::from_utf8_lossy(&value);
configuration
.collate
.get_or_insert_with(Vec::new)
.extend(Self::parse_collate(&value)?);
}
if let Some(value) = job_json_choice(&members, b"json", &["1", "2", "latest"], false)? {
configuration.json_version = Some(parse_json_version(&value));
configuration.require_output = false;
}
if let Some(value) = job_json_choice(&members, b"jsonOutput", &["2", "latest"], false)? {
configuration.json_output = true;
configuration.json_version = Some(parse_json_version(&value));
if !configuration.json_stream_data_set {
configuration.json_stream_data = JsonStreamData::Inline;
}
if !configuration.json_decode_level_set {
configuration.json_decode_level = crate::writer::DecodeLevel::None;
}
configuration.require_output = false;
configuration.json_keys.push(JsonKey::Qpdf);
}
if let Some(value) = job_json_string(&members, b"jsonStreamPrefix")? {
configuration.json_stream_prefix = Some(value);
}
if let Some(value) = job_json_choice(
&members,
b"jsonStreamData",
&["none", "inline", "file"],
true,
)? {
configuration.json_stream_data = match value.as_str() {
"none" => JsonStreamData::None,
"inline" => JsonStreamData::Inline,
"file" => JsonStreamData::File,
_ => unreachable!("jsonStreamData was validated above"), };
configuration.json_stream_data_set = true;
}
if let Some(value) = members.get(b"jsonKey".as_slice()) {
for item in job_json_items(value) {
let item = item.get_string().ok_or_else(|| {
Error::Usage(UsageError::new(".jsonKey: value must be a string"))
})?;
let item = String::from_utf8_lossy(&item);
let key = JsonKey::from_str(&item).ok_or_else(|| {
Error::Usage(UsageError::new(
".jsonKey: unexpected value; expected one of acroform, attachments, encrypt, objectinfo, objects, outlines, pagelabels, pages, qpdf",
))
})?;
configuration.json_keys.push(key);
}
}
if let Some(value) = members.get(b"jsonObject".as_slice()) {
for item in job_json_items(value) {
let item = item.get_string().ok_or_else(|| {
Error::Usage(UsageError::new(".jsonObject: value must be a string"))
})?;
configuration
.json_objects
.push(String::from_utf8_lossy(&item).into_owned());
}
}
if job_json_bare(&members, b"testJsonSchema")? {
configuration.test_json_schema = true;
}
if job_json_bare(&members, b"showEncryptionKey")? {
configuration.show_encryption_key = true;
}
if job_json_bare(&members, b"noWarn")? {
self.suppress_warnings = true;
}
if job_json_bare(&members, b"warningExit0")? {
self.warnings_exit_zero = true;
}
if job_json_bare(&members, b"check")? {
configuration.check = true;
configuration.require_output = false;
}
if job_json_bare(&members, b"showNpages")? {
configuration.show_npages = true;
configuration.require_output = false;
}
if job_json_bare(&members, b"showPages")? {
configuration.show_pages = true;
configuration.require_output = false;
}
if job_json_bare(&members, b"showEncryption")? {
configuration.show_encryption = true;
configuration.require_output = false;
}
if job_json_bare(&members, b"isEncrypted")? {
configuration.is_encrypted = true;
configuration.require_output = false;
}
if job_json_bare(&members, b"requiresPassword")? {
configuration.requires_password = true;
configuration.require_output = false;
}
if job_json_bare(&members, b"checkLinearization")? {
configuration.check_linearization = true;
configuration.require_output = false;
}
if job_json_bare(&members, b"showXref")? {
configuration.show_xref = true;
configuration.require_output = false;
}
if job_json_bare(&members, b"showLinearization")? {
configuration.show_linearization = true;
configuration.require_output = false;
}
if job_json_bare(&members, b"filteredStreamData")? {
configuration.show_filtered_stream_data = true;
}
if job_json_bare(&members, b"rawStreamData")? {
configuration.show_raw_stream_data = true;
}
if let Some(value) = job_json_string(&members, b"showObject")? {
configuration.show_object = Some(parse_job_object_selector(&value)?);
configuration.require_output = false;
}
if job_json_bare(&members, b"listAttachments")? {
configuration.list_attachments = true;
configuration.require_output = false;
}
if let Some(value) = job_json_string(&members, b"showAttachment")? {
configuration.show_attachment = Some(value);
configuration.require_output = false;
}
if job_json_bare(&members, b"withImages")? {
configuration.show_page_images = true;
}
if job_json_bare(&members, b"reportMemoryUsage")? {
configuration.report_memory_usage = true;
}
if let Some(value) = members.get(b"encrypt".as_slice()) {
configuration.copy_encryption = None;
configuration
.writer
.set_encryption_parameters(parse_job_encrypt(
value,
configuration.allow_weak_crypto,
)?); }
if let Some(value) = members.get(b"pages".as_slice()) {
if configuration.page_specs_origin != PageSpecsOrigin::None {
return Err(Error::Usage(UsageError::new(
"--pages may only be specified one time",
)));
}
let items = job_json_items(value);
if items.is_empty() {
return Err(Error::Usage(UsageError::new(
"--pages: no page specifications given",
)));
}
configuration.page_specs_origin = PageSpecsOrigin::Json;
for (index, item) in items.into_iter().enumerate() {
let item_members = job_json_members(&item);
let file = job_json_string(&item_members, b"file")?.ok_or_else(|| {
Error::Usage(UsageError::new("file is required in page specification"))
})?;
let range = job_json_range(
item_members.get(b"range".as_slice()),
&format!(".pages[{index}].range"),
)?; configuration.page_specs.push(JobPageConfig {
path: path_from_qpdf_json_bytes(&file),
password: job_json_string(&item_members, b"password")?,
range,
}); }
}
if let Some(value) = members.get(b"overlay".as_slice()) {
parse_job_overlay_specs(&mut configuration.overlays, value, OverlayKind::Overlay)?;
}
if let Some(value) = members.get(b"underlay".as_slice()) {
parse_job_overlay_specs(&mut configuration.underlays, value, OverlayKind::Underlay)?;
}
if let Some(value) = members.get(b"addAttachment".as_slice()) {
for (index, item) in job_json_items(value).into_iter().enumerate() {
configuration.attachments_to_add.push(parse_job_attachment(
&item,
&format!(".addAttachment[{index}]"),
)?);
}
}
if let Some(value) = members.get(b"copyAttachmentsFrom".as_slice()) {
for (index, item) in job_json_items(value).into_iter().enumerate() {
let item_members = job_json_members(&item);
let file = job_json_required_string(
&item_members,
b"file",
&format!(".copyAttachmentsFrom[{index}].file"),
)?; configuration
.attachments_to_copy
.push(JobCopyAttachmentsConfig {
path: path_from_qpdf_json_bytes(&file),
password: job_json_string(&item_members, b"password")?.unwrap_or_default(),
prefix: job_json_string(&item_members, b"prefix")?.unwrap_or_default(),
}); }
}
if let Some(value) = members.get(b"removeAttachment".as_slice()) {
for item in job_json_items(value) {
configuration
.attachments_to_remove
.push(item.get_string().ok_or_else(|| {
Error::Usage(UsageError::new(".removeAttachment: value must be a string"))
})?);
}
}
if let Some(value) = members.get(b"setPageLabels".as_slice()) {
let mut labels = Vec::new();
for item in job_json_items(value) {
let label = item.get_string().ok_or_else(|| {
Error::Usage(UsageError::new(".setPageLabels: value must be a string"))
})?;
labels.push(parse_page_label_spec(&label)?);
}
configuration.set_page_labels = Some(labels);
}
if job_json_bare(&members, b"removePageLabels")? {
configuration.remove_page_labels = true;
}
if let Some(value) = job_json_choice(
&members,
b"removeUnreferencedResources",
&["auto", "yes", "no"],
true,
)? {
configuration.remove_unreferenced_resources = match value.as_str() {
"auto" => RemoveUnreferencedResources::Auto,
"yes" => RemoveUnreferencedResources::Yes,
"no" => RemoveUnreferencedResources::No,
_ => unreachable!("removeUnreferencedResources was validated above"), };
}
if job_json_bare(&members, b"preserveUnreferencedResources")? {
configuration.remove_unreferenced_resources = RemoveUnreferencedResources::No;
}
Ok(())
}
pub fn open_document<R>(
&mut self,
source: R,
input_name: impl Into<String>,
options: PdfOpenOptions,
) -> Result<JobDocument>
where
R: Read + Seek + 'static,
{
let input_name = input_name.into();
self.open_document_with_description(source, input_name.as_bytes(), options)
}
pub fn open_document_with_description<R>(
&mut self,
source: R,
input_name: impl AsRef<[u8]>,
mut options: PdfOpenOptions,
) -> Result<JobDocument>
where
R: Read + Seek + 'static,
{
let input_name = input_name.as_ref().to_vec();
self.set_input_name_bytes(&input_name);
options.logger = Some(self.logger.clone());
options.description = input_name;
options.verbose |= self.configuration.verbose;
options.message_prefix = self.message_prefix.as_bytes().to_vec();
options.suppress_warnings |= self.suppress_warnings;
let mut pdf = Pdf::<Box<dyn ReadSeek>>::open_with_options(Box::new(source), options)?;
pdf.root_handle()?;
self.update_writer_version_floor(&mut pdf)?;
self.record_document_warnings(&pdf);
Ok(pdf)
}
pub fn create_empty_document(&mut self) -> Result<JobDocument> {
self.empty_primary_created = true;
let mut options = self.configured_open_options(Vec::new());
options.logger = Some(self.logger.clone());
options.suppress_warnings = self.suppress_warnings;
options.description = b"empty PDF".to_vec();
let mut pdf = crate::engine::open_empty_with_options_erased(options)?;
self.input_name.clear();
self.input_name_bytes.clear();
pdf.root_handle()?;
self.record_document_warnings(&pdf);
Ok(pdf)
}
pub(crate) fn page_spec_source_sort_key(
&self,
source_index: usize,
source_description: &[u8],
) -> Vec<u8> {
if source_index == 0 && (self.configuration.empty_input || self.empty_primary_created) {
Vec::new()
} else {
source_description.to_vec()
}
}
pub fn create_from_json_document<S>(
&mut self,
source: S,
input_name: impl AsRef<[u8]>,
) -> Result<JobDocument>
where
S: Read + Seek + 'static,
{
let input_name = input_name.as_ref().to_vec();
self.set_input_name_bytes(&input_name);
let mut options = self.configured_open_options(Vec::new());
options.logger = Some(self.logger.clone());
options.suppress_warnings = self.suppress_warnings;
let pdf = crate::json::create_from_json_erased(source, input_name, options)?;
self.record_document_warnings(&pdf);
Ok(pdf)
}
fn finish_created_document(&mut self, mut pdf: JobDocument) -> Result<JobDocument> {
self.page_source_documents.clear();
self.overlay_sources.clear();
self.primary_copy_encryption = None;
self.encryption_status = EncryptionStatus {
encrypted: pdf.is_encrypted(),
password_incorrect: false,
};
let configuration = self.configuration.clone();
if configuration.is_encrypted || configuration.requires_password {
return Ok(pdf);
}
if let Some(update_path) = configuration.update_from_json.as_deref() {
let update_file = File::open(update_path).map_err(|error| {
Error::file_io("open update JSON", update_path.to_path_buf(), error)
})?;
self.update_from_json(
&mut pdf,
BufReader::new(update_file),
path_description_bytes(update_path),
)?; }
self.prepare_document(pdf, &configuration)
}
fn prepare_document(
&mut self,
primary: JobDocument,
configuration: &JobConfiguration,
) -> Result<JobDocument> {
if configuration.page_specs.is_empty() {
let mut primary = primary;
self.apply_configured_rotations(&mut primary, configuration)?;
self.prepare_document_transformations(&mut primary, configuration)?;
return Ok(primary);
}
let mut page_sources = vec![primary];
let mut source_paths: Vec<PathBuf> = Vec::new();
let mut source_passwords: Vec<Option<Vec<u8>>> = Vec::new();
let mut specs = Vec::with_capacity(configuration.page_specs.len());
for page in &configuration.page_specs {
let source_index = if page.path == Path::new(".")
|| self.configuration.input_file.as_deref() == Some(page.path.as_path())
{
0
} else if let Some(index) = source_paths.iter().position(|path| *path == page.path) {
index + 1
} else {
source_paths.push(page.path.clone());
let password = page.password.clone().or_else(|| {
(configuration
.copy_encryption
.as_deref()
.map(Path::as_os_str)
== Some(page.path.as_os_str()))
.then(|| configuration.encryption_file_password.clone())
});
source_passwords.push(password);
source_paths.len()
};
specs.push(PageSpecInput::new(source_index, page.range.clone()));
}
let keep_files_open = self.keep_files_open_for_page_specs(&specs);
self.report_page_spec_selection(&specs)?;
for (path, password) in source_paths.iter().zip(source_passwords.iter()) {
self.report_page_source_processing(path_description_bytes(path))?;
let mut source = self.open_job_source(path, password.as_deref().unwrap_or_default())?;
self.update_writer_version_floor(&mut source)?;
self.record_document_warnings(&source);
if !keep_files_open {
source.set_input_source_stay_open(false);
}
page_sources.push(source);
}
let primary_copy_encryption = if page_sources.len() > 1 {
page_sources[0].writer_copy_encryption_source()?
} else {
None
};
if page_sources.len() == 1 && specs.iter().all(|spec| spec.source_index == 0) {
{
let page_output = self.handle_page_specs(
&mut page_sources,
&specs,
configuration.collate.as_deref(),
configuration.remove_unreferenced_resources,
configuration.writer.preserves_unreferenced_objects(),
)?; match page_output {
PageSpecJobOutput::InPlace {
pdf,
result,
prune_mode,
} => {
QPDFJob::complete_in_place_page_selection(pdf, &result, prune_mode)?;
}
PageSpecJobOutput::Merged(_) => {
return Err(Error::Internal(
"single-source page selection returned a merged target".to_owned(),
));
}
}
}
let mut primary = page_sources
.pop()
.ok_or_else(|| Error::Internal("page selection lost its primary".to_owned()))?;
self.apply_configured_rotations(&mut primary, configuration)?;
self.prepare_document_transformations(&mut primary, configuration)?;
return Ok(primary);
}
let target = self.create_page_selection_target()?;
let page_output = self.handle_page_specs_with_target(
&mut page_sources,
&specs,
configuration.collate.as_deref(),
configuration.remove_unreferenced_resources,
configuration.writer.preserves_unreferenced_objects(),
target,
)?; let mut primary = match page_output {
PageSpecJobOutput::Merged(merged) => {
for source in page_sources.iter().skip(1) {
source.resolver.disconnect_all();
}
self.page_source_documents = page_sources;
self.primary_copy_encryption = primary_copy_encryption;
*merged
}
PageSpecJobOutput::InPlace { .. } => {
return Err(Error::Internal(
"multi-source page selection returned an in-place target".to_owned(),
));
}
};
self.apply_configured_rotations(&mut primary, configuration)?;
self.prepare_document_transformations(&mut primary, configuration)?;
Ok(primary)
}
fn create_page_selection_target(&self) -> Result<JobDocument> {
let mut options = self.configured_open_options(Vec::new());
options.logger = Some(self.logger.clone());
options.suppress_warnings = self.suppress_warnings;
options.description = b"empty PDF".to_vec();
crate::engine::open_empty_with_options_erased(options)
}
pub fn create_qpdf(&mut self) -> Result<Option<JobDocument>> {
self.create_qpdf_succeeded_without_document = false;
match self.check_configuration() {
Ok(()) => {}
Err(error @ Error::Usage(_)) => return Err(error),
Err(error) => {
self.report_job_error(&error)?;
return Ok(None);
}
}
if self.configuration.empty_input {
let pdf = self.create_empty_document()?;
return match self.finish_created_document(pdf) {
Ok(pdf) => Ok(Some(pdf)),
Err(error) => {
self.report_job_error(&error)?;
Ok(None)
}
};
}
let Some(input) = self.configuration.input_file.clone() else {
let error = Error::Unsupported("qpdfjob input file is not configured".to_owned());
self.report_job_error(&error)?;
return Ok(None);
};
let file = match File::open(&input) {
Ok(file) => file,
Err(error) => {
let error = Error::file_io("open", input.clone(), error);
self.report_job_error(&error)?;
return Ok(None);
}
};
if self.configuration.json_input {
return match self.create_from_json_document(file, path_description_bytes(&input)) {
Ok(pdf) => match self.finish_created_document(pdf) {
Ok(pdf) => Ok(Some(pdf)),
Err(error) => {
self.report_job_error(&error)?;
Ok(None)
}
},
Err(error) => {
self.report_job_error(&error)?;
Ok(None)
}
};
}
let input_name = path_description_bytes(&input);
let needs_encryption_inspection_open = self.configuration.show_encryption
|| self.configuration.is_encrypted
|| self.configuration.requires_password;
let open_result: Result<JobDocument> = if needs_encryption_inspection_open {
let source: Box<dyn ReadSeek> = Box::new(BufReader::new(file));
self.open_for_encryption_inspection_with_description(
source,
&input_name,
self.configured_open_options(self.configuration.password.clone()),
)
} else {
self.open_document_with_description(
BufReader::new(file),
&input_name,
self.configured_open_options(self.configuration.password.clone()),
)
};
match open_result {
Ok(mut pdf)
if needs_encryption_inspection_open
&& pdf.is_encrypted()
&& pdf.encryption_file_key().is_none() =>
{
if self.configuration.is_encrypted || self.configuration.requires_password {
self.encryption_status = EncryptionStatus {
encrypted: true,
password_incorrect: true,
};
} else {
self.show_encryption(&mut pdf, self.configuration.password_is_hex_key)?;
}
self.create_qpdf_succeeded_without_document = true;
Ok(None)
}
Ok(pdf) => match self.finish_created_document(pdf) {
Ok(pdf) => Ok(Some(pdf)),
Err(error) => {
self.report_job_error(&error)?;
Ok(None)
}
},
Err(error) => {
self.report_job_error(&error)?;
Ok(None)
}
}
}
pub fn write_qpdf<R>(&mut self, pdf: &mut Pdf<R>) -> Result<()>
where
R: Read + Seek + 'static,
{
let creates_output = self.configuration.output_file.is_some()
|| self.configuration.replace_input
|| self.configuration.json_version.is_some();
if !creates_output {
let configuration = self.configuration.clone();
if let Err(failure) = self.run_configured_inspection(pdf, &configuration) {
let error = match failure {
InspectionFailure::Reported(error) => error,
InspectionFailure::Unreported(error) => {
self.report_job_error(&error)?;
error
}
};
return Err(error);
}
self.drain_document_warnings(pdf);
self.complete(false)?;
if self.configuration.report_memory_usage {
self.report_memory_usage()?;
}
return Ok(());
}
let output = self
.output_destination()
.expect("creates_output guarantees an output destination");
self.reserve_standard_output()?;
let mut writer_configuration = self.configuration.writer.clone();
if let Some(value) = self.configuration.normalize_content {
writer_configuration.set_content_normalization(value);
}
if let Some((version, extension_level)) = self.configuration.max_input_version.clone() {
writer_configuration.set_minimum_pdf_version(version, extension_level);
}
if let Some(path) = self.configuration.copy_encryption.clone() {
match self.copy_encryption_source(&path) {
Ok(Some(source)) => writer_configuration.copy_encryption_parameters(source),
Ok(None) => {
writer_configuration.set_preserve_encryption(false);
}
Err(error) => {
self.report_job_error(&error)?;
return Err(error);
}
}
}
let splitting = self.configuration.split_pages.is_some_and(|size| size != 0);
if self.configuration.copy_encryption.is_none()
&& !pdf.is_encrypted()
&& !splitting
&& writer_configuration.can_preserve_encryption()
{
if let Some(source) = self.primary_copy_encryption.take() {
writer_configuration.copy_encryption_parameters(source);
}
}
let auto_password_notices = match writer_configuration
.normalize_encryption_passwords(self.configuration.password_mode)
{
Ok(notices) => notices,
Err(error) => {
self.report_job_error(&error)?;
return Err(error);
}
};
for notice in auto_password_notices {
match notice {
crate::encryption::PasswordWriteNotice::Info if self.configuration.verbose => {
self.logger.info(format!(
"{}: automatically converting Unicode password to single-byte encoding as required for 40-bit or 128-bit encryption\n",
self.message_prefix
))?;
}
crate::encryption::PasswordWriteNotice::Warning => {
self.logger.error(format!(
"{}: WARNING: supplied password looks like a Unicode password with characters not allowed in passwords for 40-bit and 128-bit encryption; most readers will not be able to open this file with the supplied password. (Use --password-mode=bytes to suppress this warning and use the password anyway.)\n",
self.message_prefix
))?;
}
crate::encryption::PasswordWriteNotice::None
| crate::encryption::PasswordWriteNotice::Info => {}
}
}
if !self.configuration.allow_weak_crypto
&& writer_configuration
.encryption_parameters()
.is_some_and(EncryptParams::is_weak_rc4)
{
let message = format!(
"{}: refusing to write a file with RC4, a weak cryptographic algorithm\nPlease use 256-bit keys for better security.\nPass --allow-weak-crypto to enable writing insecure files.\nSee also https://qpdf.readthedocs.io/en/stable/weak-crypto.html\n",
self.message_prefix
);
self.logger.error(message)?;
let error = Error::System("refusing to write a file with weak crypto".to_string());
self.report_job_error(&error)?;
return Err(error);
}
writer_configuration.set_linearization(self.configuration.linearize);
if let Some(path) = self.configuration.linearize_pass1.as_deref() {
writer_configuration.set_linearization_pass1_filename(path.to_path_buf());
}
let progress_requested = self.configuration.progress;
let write_result: Result<()> =
if let Some(split_pages) = self.configuration.split_pages.filter(|size| *size != 0) {
let mut split_options = SplitPageOptions::new(1, output.clone())
.with_qpdf_chunk_size(split_pages)
.with_writer_configuration(writer_configuration.clone())
.with_verbose(self.configuration.verbose)
.with_remove_unreferenced_resources(
self.configuration.remove_unreferenced_resources,
);
if let Some(input) = self.configuration.input_file.clone() {
split_options = split_options.with_input_path(input);
}
self.split_pages(pdf, split_options).map(|_| ())
} else if self.configuration.json_version.is_some() {
let configuration = self.configuration.clone();
self.write_configured_json(pdf, &configuration)
} else {
(|| {
let mut writer = PdfWriter::new(pdf);
writer_configuration.apply_to(&mut writer);
if progress_requested {
self.configure_writer_progress(&mut writer);
}
if output == Path::new("-") {
self.logger.save_to_standard_output(true)?;
writer.set_output_pipeline(JobOutputPipeline(self.logger.get_save()?))?;
} else {
writer.set_output_file(&output)?;
}
writer.write()
})()
};
match write_result {
Ok(()) => {
if pdf.any_warnings() {
self.record_warnings();
}
if self.configuration.verbose && output != Path::new("-") && !splitting {
let mut message = self.message_prefix.as_bytes().to_vec();
message.extend_from_slice(b": wrote file ");
message.extend_from_slice(&path_description_bytes(&output));
message.push(b'\n');
self.logger.info(message)?;
}
if self.configuration.replace_input {
pdf.close_input_source();
for source in &self.page_source_documents {
source.close_input_source();
}
for spec in &self.overlay_sources {
spec.source.close_input_source();
}
if let Err(error) = self.finish_replace_input() {
self.report_job_error(&error)?;
return Err(error);
}
}
self.drain_document_warnings(pdf);
self.complete(output != Path::new("-"))?;
if self.configuration.report_memory_usage {
self.report_memory_usage()?;
}
Ok(())
}
Err(error) => {
self.report_job_error(&error)?;
Err(error)
}
}
}
pub fn apply_transformations<R>(&mut self, pdf: &mut Pdf<R>) -> Result<()>
where
R: Read + Seek + 'static,
{
self.reserve_standard_output()?;
let configuration = self.configuration.clone();
self.apply_configured_rotations(pdf, &configuration)?;
self.prepare_document_transformations(pdf, &configuration)
}
fn output_destination(&self) -> Option<PathBuf> {
self.configuration
.output_file
.clone()
.or_else(|| {
self.configuration
.replace_input
.then(|| self.replace_input_path())
.flatten()
})
.or_else(|| {
self.configuration
.json_version
.is_some()
.then(|| PathBuf::from("-"))
})
}
fn reserve_standard_output(&mut self) -> Result<()> {
if self.output_destination().as_deref() != Some(Path::new("-")) {
return Ok(());
}
if let Err(error) = self.logger.save_to_standard_output(true) {
self.report_job_error(&error)?;
return Err(error);
}
Ok(())
}
pub fn run(&mut self) -> Result<JobExitCode> {
self.partial_json_initialized = false;
if self.configuration.is_encrypted || self.configuration.requires_password {
return self.run_encryption_status();
}
let Some(mut pdf) = self.create_qpdf()? else {
return Ok(if self.create_qpdf_succeeded_without_document {
self.get_exit_code()
} else {
JobExitCode::Error
});
};
let status = match self.write_qpdf(&mut pdf) {
Ok(()) => self.get_exit_code(),
Err(_error) => JobExitCode::Error,
};
Ok(status)
}
pub fn inspect_configured<R>(
&mut self,
pdf: &mut Pdf<R>,
) -> std::result::Result<JobExitCode, super::check::CheckError>
where
R: Read + Seek + 'static,
{
let configuration = self.configuration.clone();
match self.run_configured_inspection(pdf, &configuration) {
Ok(()) => {
self.drain_document_warnings(pdf);
self.complete(false)?;
if self.configuration.report_memory_usage {
self.report_memory_usage()?;
}
Ok(self.get_exit_code())
}
Err(InspectionFailure::Reported(_error)) => {
Err(super::check::CheckError::ErrorsDetected)
}
Err(InspectionFailure::Unreported(error)) => {
Err(super::check::CheckError::Operation(error))
}
}
}
fn report_memory_usage(&self) -> Result<()> {
self.logger.warn(format!(
"qpdf-max-memory-usage {}\n",
crate::memory_usage::max_memory_usage()
))
}
fn run_encryption_status(&mut self) -> Result<JobExitCode> {
self.check_configuration()?;
self.encryption_status = EncryptionStatus::default();
if self.configuration.empty_input {
return Ok(self.get_exit_code());
}
let Some(input) = self.configuration.input_file.clone() else {
return Err(UsageError::new("an input file name is required").into());
};
let file = match File::open(&input) {
Ok(file) => file,
Err(error) => {
let error = Error::file_io("open", input.clone(), error);
self.report_job_error(&error)?;
return Ok(JobExitCode::Error);
}
};
let options = self.configured_open_options(self.configuration.password.clone());
let pdf = match self.open_for_encryption_inspection_with_description(
BufReader::new(file),
path_description_bytes(&input),
options,
) {
Ok(pdf) => pdf,
Err(error) => {
self.report_job_error(&error)?;
return Ok(JobExitCode::Error);
}
};
let encrypted = pdf.is_encrypted();
self.encryption_status = EncryptionStatus {
encrypted,
password_incorrect: encrypted && pdf.encryption_file_key().is_none(),
};
if self.configuration.is_encrypted {
return Ok(self.get_exit_code());
}
if !encrypted {
return Ok(self.get_exit_code());
}
Ok(self.get_exit_code())
}
fn apply_configured_rotations<R>(
&mut self,
pdf: &mut Pdf<R>,
configuration: &JobConfiguration,
) -> Result<()>
where
R: Read + Seek,
{
if configuration.rotations.is_empty() {
return Ok(());
}
let page_refs = PageDocumentHelper::new(pdf).get_all_pages()?;
let page_count = crate::qutil::qpdf_size_to_int(page_refs.len())?;
for (range, rotation) in &configuration.rotations {
let selected = crate::qutil::parse_numrange(range, page_count)?;
for page in selected {
let index = page.wrapping_sub(1);
if index >= 0 && index < page_count {
let mut page = PageObjectHelper::new(page_refs[index as usize], pdf);
page.rotate_page(rotation.angle, rotation.relative)?;
}
}
}
Ok(())
}
fn prepare_document_transformations<R>(
&mut self,
pdf: &mut Pdf<R>,
configuration: &JobConfiguration,
) -> Result<()>
where
R: Read + Seek + 'static,
{
let mut overlay_specs =
Vec::with_capacity(configuration.overlays.len() + configuration.underlays.len());
for overlay in configuration
.underlays
.iter()
.chain(configuration.overlays.iter())
{
let mut source = self.open_job_source(&overlay.path, &overlay.password)?;
self.update_writer_version_floor(&mut source)?;
overlay_specs.push(OverlaySpec {
source,
kind: overlay.kind,
from: overlay.from.clone(),
to: overlay.to.clone(),
repeat: overlay.repeat.clone(),
});
}
if configuration.verbose && !overlay_specs.is_empty() {
let report = overlay_verbose_report(pdf, &mut overlay_specs)?;
self.report_overlay_progress(&report, configuration)?;
}
handle_under_overlay(pdf, &mut overlay_specs)?;
self.overlay_sources = overlay_specs;
if configuration.remove_restrictions {
let mut acroform = AcroFormDocumentHelper::new(pdf)?;
acroform.disable_digital_signatures()?;
}
if configuration.optimize_images {
let mut image_options = configuration.image_options;
if configuration.externalize_inline_images {
image_options.keep_inline_images = false;
}
optimize_images(
pdf,
&self.logger,
&self.message_prefix,
configuration.verbose,
image_options,
)?; } else if configuration.externalize_inline_images {
let page_refs = PageDocumentHelper::new(pdf).get_all_pages()?;
for page_ref in page_refs {
PageObjectHelper::new(page_ref, pdf).externalize_inline_images(
configuration.image_options.inline_min_bytes,
false,
)?; }
}
if configuration.generate_appearances {
let mut acroform = AcroFormDocumentHelper::new(pdf)?;
acroform.generate_appearances_if_needed()?;
}
if let Some(mode) = configuration.flatten_annotations {
let (required_flags, forbidden_flags) = mode.qpdf_flags();
PageDocumentHelper::new(pdf).flatten_annotations(required_flags, forbidden_flags)?;
}
if configuration.coalesce_contents {
let page_refs = PageDocumentHelper::new(pdf).get_all_pages()?;
for page_ref in page_refs {
PageObjectHelper::new(page_ref, pdf).coalesce_content_streams()?;
}
}
if configuration.flatten_rotation {
let page_refs = PageDocumentHelper::new(pdf).get_all_pages()?;
flatten_rotation_on_pages(pdf, &page_refs)?;
}
self.apply_page_label_transformations(pdf, configuration)?;
for key in &configuration.attachments_to_remove {
if !pdf.embedded_files().remove_embedded_file(key)? {
let mut message = b"attachment ".to_vec();
message.extend_from_slice(key);
message.extend_from_slice(b" not found");
return Err(Error::SystemBytes(message));
}
if configuration.verbose {
let mut message = self.message_prefix.clone().into_bytes();
message.extend_from_slice(b": removed attachment ");
message.extend_from_slice(key);
message.push(b'\n');
self.logger.info(message)?; } }
let attachments_to_add = configuration
.attachments_to_add
.iter()
.cloned()
.map(|mut option| {
option.verbose = configuration.verbose;
option
})
.collect::<Vec<_>>();
self.add_attachments(pdf, &attachments_to_add)?;
let copy_options = configuration
.attachments_to_copy
.iter()
.map(|copy| AttachmentCopyOptions {
path: copy.path.clone(),
password: copy.password.clone(),
prefix: copy.prefix.clone(),
verbose: configuration.verbose,
})
.collect::<Vec<_>>();
self.copy_attachments_with_opener(pdf, ©_options, |job, option| {
job.open_job_source(&option.path, &option.password)
})?;
Ok(())
}
fn report_overlay_progress(
&self,
report: &[OverlayVerbosePage],
configuration: &JobConfiguration,
) -> Result<()> {
let paths: Vec<&Path> = configuration
.underlays
.iter()
.chain(configuration.overlays.iter())
.map(|overlay| overlay.path.as_path())
.collect();
let mut message = self.message_prefix.as_bytes().to_vec();
message.extend_from_slice(b": processing underlay/overlay\n");
for page in report {
message.extend_from_slice(b" page ");
message.extend_from_slice(page.dest_page.to_string().as_bytes());
message.push(b'\n');
for source in &page.sources {
let path = paths.get(source.spec_index).ok_or_else(|| {
Error::Internal("overlay verbose source index out of range".into())
})?;
message.extend_from_slice(b" ");
message.extend_from_slice(&path_description_bytes(path));
message.push(b' ');
message.extend_from_slice(match source.kind {
OverlayKind::Underlay => b"underlay" as &[u8],
OverlayKind::Overlay => b"overlay" as &[u8],
});
message.push(b' ');
message.extend_from_slice(source.src_page.to_string().as_bytes());
message.push(b'\n');
}
}
self.logger.info(message)
}
fn run_configured_inspection<R>(
&mut self,
pdf: &mut Pdf<R>,
configuration: &JobConfiguration,
) -> std::result::Result<(), InspectionFailure>
where
R: Read + Seek + 'static,
{
pdf.set_logger(self.logger.clone());
if configuration.check {
if let Err(error) = self.run_check_report(pdf) {
return match error {
super::check::CheckError::ErrorsDetected => Err(InspectionFailure::Reported(
Error::Unsupported("errors detected".to_owned()),
)),
super::check::CheckError::Operation(error) => {
Err(InspectionFailure::Unreported(error))
}
};
}
}
if configuration.show_npages {
self.show_npages_report(pdf)?;
}
if configuration.show_encryption {
self.show_encryption(pdf, configuration.password_is_hex_key)?;
}
if configuration.check_linearization {
self.check_linearization_report(pdf)?;
}
if configuration.show_linearization {
self.show_linearization_report(pdf)?;
}
if configuration.show_xref {
self.show_xref_report(pdf)?;
}
if let Some(selector) = configuration.show_object {
match selector {
JobObjectSelector::Trailer => {
let object = pdf.trailer();
self.show_object_report(
pdf,
&object,
configuration.show_raw_stream_data,
configuration.show_filtered_stream_data,
)?;
}
JobObjectSelector::Object(object_ref) => {
let object = pdf.get_object_handle(object_ref);
self.show_object_report(
pdf,
&object,
configuration.show_raw_stream_data,
configuration.show_filtered_stream_data,
)?;
}
JobObjectSelector::Null => self.logger.info(b"null\n")?,
JobObjectSelector::NoObject => {}
}
}
if configuration.show_pages {
self.show_pages_report_with_images(pdf, configuration.show_page_images)?;
}
if configuration.list_attachments {
self.list_attachments_report(pdf, configuration.verbose)?;
}
if let Some(key) = configuration
.show_attachment
.as_deref()
.filter(|key| !key.is_empty())
{
self.show_attachment_report(pdf, key)?;
}
Ok(())
}
pub fn show_linearization<R: Read + Seek>(&mut self, pdf: &mut Pdf<R>) -> Result<JobExitCode> {
self.show_linearization_report(pdf)?;
self.drain_document_warnings(pdf);
self.complete(false)?;
Ok(self.get_exit_code())
}
fn show_linearization_report<R: Read + Seek>(&mut self, pdf: &mut Pdf<R>) -> Result<()> {
let suppress_warnings = pdf.suppress_warnings() || self.suppress_warnings;
pdf.set_logger(self.logger.clone());
pdf.set_suppress_warnings(suppress_warnings);
let input_name = self.input_name_bytes().to_owned();
let output = show_linearization_pdf_with_warnings(pdf, &input_name)
.map_err(map_show_linearization_error)?;
for warning in output.warnings {
self.record_warnings();
if !suppress_warnings {
let mut line = b"WARNING: ".to_vec();
line.extend_from_slice(&input_name);
line.extend_from_slice(b": ");
line.extend_from_slice(&warning);
line.push(b'\n');
self.logger.warn(line)?;
}
}
self.logger.info(output.dump)
}
fn open_job_source(&mut self, path: &Path, password: &[u8]) -> Result<JobDocument> {
let input_name = path_description_bytes(path);
let mut options = self.configured_open_options(password.to_vec());
options.logger = Some(self.logger.clone());
options.description = input_name.clone();
options.suppress_warnings |= self.suppress_warnings;
let result = (|| {
let mut pdf = Pdf::<Box<dyn ReadSeek>>::open_file_with_options(path, options)?;
pdf.root_handle()?;
Ok(pdf)
})();
if result.is_err() {
self.set_input_name_bytes(&input_name);
}
result
}
fn update_writer_version_floor(&mut self, source: &mut JobDocument) -> Result<()> {
let version = source.get_version_as_pdf_version()?;
let (version, extension_level) = version.get_version();
crate::writer::update_minimum_pdf_version(
&mut self.configuration.max_input_version,
version,
extension_level,
);
Ok(())
}
fn copy_encryption_source(
&mut self,
path: &Path,
) -> Result<Option<crate::CopyEncryptionSource>> {
let password = self.configuration.encryption_file_password.clone();
let mut donor = self.open_job_source(path, &password)?;
self.record_document_warnings(&donor);
donor.writer_copy_encryption_source()
}
fn write_configured_json<R>(
&mut self,
pdf: &mut Pdf<R>,
configuration: &JobConfiguration,
) -> Result<()>
where
R: Read + Seek + 'static,
{
let version = configuration
.json_version
.expect("json_version is present for configured JSON output");
let options = JsonJobOptions {
decode_level: json_decode_level_for_output(configuration.json_decode_level),
stream_data: configuration.json_stream_data,
stream_prefix: configuration.json_stream_prefix.as_deref(),
keys: &configuration.json_keys,
objects: &configuration.json_objects,
};
if let Some(path) = configuration
.output_file
.as_deref()
.filter(|path| *path != Path::new("-"))
{
let mut file = File::create(path)
.map_err(|error| Error::file_io("open JSON output", path.to_path_buf(), error))?;
return self
.write_json_without_completion(
pdf,
version,
configuration.test_json_schema,
configuration.json_output,
configuration.show_encryption_key,
options,
JsonJobOutput::File {
filename: path,
writer: &mut file,
},
)
.map_err(Error::from);
}
self.logger.save_to_standard_output(true)?;
let mut output = JobOutputWriter(self.logger.get_save()?);
self.write_json_without_completion(
pdf,
version,
configuration.test_json_schema,
configuration.json_output,
configuration.show_encryption_key,
options,
JsonJobOutput::Stdout(&mut output),
)
.map_err(Error::from)
}
fn apply_page_label_transformations<R>(
&mut self,
pdf: &mut Pdf<R>,
configuration: &JobConfiguration,
) -> Result<()>
where
R: Read + Seek,
{
if configuration.remove_page_labels {
let root = pdf.root_handle()?;
root.remove_key(b"/PageLabels"); }
let Some(specs) = configuration.set_page_labels.as_deref() else {
return Ok(());
};
if specs.is_empty() {
return Ok(());
}
let root = pdf.root_handle()?;
let page_count = crate::page_document_helper::PageDocumentHelper::new(pdf)
.get_all_pages()?
.len();
let entries = parse_job_page_labels(specs, page_count)?;
let mut nums = Vec::with_capacity(entries.len() * 2);
for (index, spec) in entries {
nums.push(ObjectHandle::integer(index));
nums.push(crate::page_label_document_helper::PageLabelDocumentHelper::<R>::page_label_dict_bytes(
spec.style,
spec.start,
&spec.prefix,
));
}
root.replace_key(
b"/PageLabels",
ObjectHandle::dictionary(vec![(b"/Nums".to_vec(), ObjectHandle::array(nums))]),
)?; Ok(())
}
fn replace_input_path(&self) -> Option<PathBuf> {
self.configuration
.input_file
.as_ref()
.map(|path| path_with_suffix(path, ".~qpdf-temp#"))
}
fn finish_replace_input(&self) -> Result<()> {
let input = self.configuration.input_file.as_ref().ok_or_else(|| {
Error::Usage(UsageError::new("--replace-input requires an input file"))
})?; let temp = self
.replace_input_path()
.ok_or_else(|| Error::System("replace-input temporary path is missing".to_owned()))?;
let mut backup = path_with_suffix(input, ".~qpdf-orig");
if !self.warnings {
backup = path_with_suffix(&backup, "#");
}
std::fs::rename(input, &backup)
.map_err(|error| Error::file_io("rename original input", input.clone(), error))?;
if let Err(error) = std::fs::rename(&temp, input) {
let _ = std::fs::rename(&backup, input);
return Err(Error::file_io("replace input", input.clone(), error));
}
if self.warnings {
self.logger.error(format!(
"{}: there are warnings; original file kept in {}\n",
self.message_prefix,
backup.display()
))?; } else if let Err(error) = std::fs::remove_file(&backup) {
self.logger.error(format!(
"{}: unable to delete original file ({}); original file left in {}, but the input was successfully replaced\n",
self.message_prefix,
error,
backup.display()
))?;
}
Ok(())
}
pub fn check_configuration(&self) -> Result<()> {
let implicit_json_stdout =
self.configuration.json_version.is_some() && self.configuration.output_file.is_none();
if self.configuration.input_file.is_none()
&& !self.configuration.empty_input
&& (self.configuration.require_output
|| self.configuration.check
|| self.configuration.show_npages
|| self.configuration.show_pages
|| self.configuration.check_linearization
|| self.configuration.show_xref
|| self.configuration.show_linearization
|| self.configuration.show_object.is_some()
|| self.configuration.list_attachments
|| self.configuration.show_attachment.is_some()
|| self.configuration.show_encryption
|| self.configuration.is_encrypted
|| self.configuration.requires_password
|| self.configuration.output_file.is_some()
|| self.configuration.replace_input)
{
return Err(UsageError::new("an input file name is required").into());
}
if self.configuration.replace_input {
if self.configuration.output_file.is_some() {
return Err(UsageError::new(
"--replace-input may not be used when an output file is specified",
)
.into());
}
if self.configuration.empty_input {
return Err(UsageError::new("--replace-input may not be used with --empty").into());
}
if self.configuration.split_pages.is_some_and(|size| size != 0) {
return Err(
UsageError::new("--split-pages may not be used with --replace-input").into(),
);
}
if self.configuration.json_version.is_some() {
return Err(UsageError::new("--json may not be used with --replace-input").into());
}
}
if self.configuration.require_output
&& self.configuration.output_file.is_none()
&& !self.configuration.replace_input
{
return Err(UsageError::new(
"an output file name is required; use - for standard output",
)
.into());
}
if (self.configuration.check
|| self.configuration.show_npages
|| self.configuration.show_pages
|| self.configuration.check_linearization
|| self.configuration.show_xref
|| self.configuration.show_linearization
|| self.configuration.show_object.is_some()
|| self.configuration.list_attachments
|| self.configuration.show_attachment.is_some()
|| self.configuration.show_encryption
|| self.configuration.is_encrypted
|| self.configuration.requires_password)
&& (self.configuration.output_file.is_some()
|| self.configuration.replace_input
|| self.configuration.json_version.is_some())
{
return Err(UsageError::new("no output file may be given for this option").into());
}
if self.configuration.is_encrypted && self.configuration.requires_password {
return Err(UsageError::new(
"--requires-password and --is-encrypted may not be given together",
)
.into());
}
if self.configuration.output_file.as_deref() == Some(Path::new("-")) || implicit_json_stdout
{
if self.configuration.split_pages.is_some_and(|size| size != 0) {
return Err(UsageError::new(
"--split-pages may not be used when writing to standard output",
)
.into());
}
self.logger.save_to_standard_output(true)?;
}
if let (Some(input), Some(output)) = (
self.configuration.input_file.as_deref(),
self.configuration.output_file.as_deref(),
) {
if !self.configuration.replace_input
&& !self.configuration.split_pages.is_some_and(|size| size != 0)
&& crate::qutil::same_file(input, output)
{
return Err(UsageError::new(
"input file and output file are the same; use --replace-input to intentionally overwrite the input",
)
.into());
}
}
if self.configuration.json_version == Some(1) {
if self.configuration.json_keys.contains(&JsonKey::Qpdf) {
return Err(UsageError::new(
"json key \"qpdf\" is only valid for json version > 1",
)
.into());
}
} else if self
.configuration
.json_keys
.iter()
.any(|key| matches!(key, JsonKey::Objects | JsonKey::Objectinfo))
{
return Err(UsageError::new(
"json keys \"objects\" and \"objectinfo\" are only valid for json version 1",
)
.into());
}
Ok(())
}
pub fn report_job_error(&self, error: &Error) -> Result<()> {
let pipeline = self.logger.get_error()?;
pipeline
.write(self.message_prefix.as_bytes())
.map_err(Error::from)?;
pipeline.write(b": ").map_err(Error::from)?;
pipeline
.write(&self.job_error_message_with_input(error))
.map_err(Error::from)?;
pipeline.write(b"\n").map_err(Error::from)
}
fn job_error_message_with_input(&self, error: &Error) -> Vec<u8> {
if let Some(message) = error.raw_message() {
return message.to_vec();
}
match error {
Error::OpenFailure { source, .. } => self.job_error_message_with_input(source),
Error::Encrypted(crate::EncryptedError::BadPassword)
if !self.input_name_bytes.is_empty() =>
{
let mut rendered = self.input_name_bytes.clone();
rendered.extend_from_slice(b": invalid password");
rendered
}
Error::FileIo {
operation,
path,
source,
} => {
let mut rendered = operation.as_bytes().to_vec();
rendered.push(b' ');
rendered.extend_from_slice(&path_description_bytes(path));
rendered.extend_from_slice(b": ");
rendered.extend_from_slice(qpdf_file_io_source_message(source).as_bytes());
rendered
}
Error::Io(error) if !self.input_name_bytes.is_empty() => {
let mut rendered = self.input_name_bytes.clone();
rendered.extend_from_slice(b": ");
rendered.extend_from_slice(qpdf_file_io_source_message(error).as_bytes());
rendered
}
Error::Parse { offset, message } if !self.input_name_bytes.is_empty() => {
let mut rendered = self.input_name_bytes.clone();
rendered.extend_from_slice(b": ");
if message == "unable to find trailer dictionary while recovering damaged file" {
rendered.extend_from_slice(message.as_bytes());
} else {
rendered.extend_from_slice(
format!("parse error at byte {offset}: {message}").as_bytes(),
);
}
rendered
}
_ => Self::job_error_message(error),
}
}
fn job_error_message(error: &Error) -> Vec<u8> {
if let Some(message) = error.raw_message() {
return message.to_vec();
}
match error {
Error::FileIo {
operation,
path,
source,
} => {
let source = qpdf_file_io_source_message(source);
format!("{operation} {}: {source}", path.display()).into_bytes()
}
Error::Io(error) => qpdf_file_io_source_message(error).into_bytes(),
Error::Encrypted(crate::EncryptedError::BadPassword) => b"invalid password".to_vec(),
_ => error.to_string().into_bytes(),
}
}
pub fn create_from_json<S>(
&mut self,
source: S,
input_name: impl AsRef<[u8]>,
) -> Result<Pdf<Cursor<Vec<u8>>>>
where
S: Read + Seek + 'static,
{
let input_name = input_name.as_ref().to_vec();
self.set_input_name_bytes(&input_name);
let pdf = Pdf::create_from_json_with_options(
source,
input_name,
PdfOpenOptions {
logger: Some(self.logger.clone()),
suppress_warnings: self.suppress_warnings,
..PdfOpenOptions::default()
},
)?;
self.record_document_warnings(&pdf);
Ok(pdf)
}
pub fn open<R>(
&mut self,
source: R,
input_name: impl Into<String>,
options: PdfOpenOptions,
) -> Result<Pdf<R>>
where
R: Read + Seek,
{
let input_name = input_name.into();
self.open_with_description(source, input_name.as_bytes(), options)
}
pub fn open_with_description<R>(
&mut self,
source: R,
input_name: impl AsRef<[u8]>,
mut options: PdfOpenOptions,
) -> Result<Pdf<R>>
where
R: Read + Seek,
{
let input_name = input_name.as_ref().to_vec();
self.set_input_name_bytes(&input_name);
options.logger = Some(self.logger.clone());
options.description = input_name;
options.verbose |= self.configuration.verbose;
options.message_prefix = self.message_prefix.as_bytes().to_vec();
options.suppress_warnings |= self.suppress_warnings;
let mut pdf = Pdf::open_with_options(source, options)?;
pdf.root_handle()?;
self.record_document_warnings(&pdf);
Ok(pdf)
}
pub fn open_for_encryption_inspection<R>(
&mut self,
source: R,
input_name: impl Into<String>,
options: PdfOpenOptions,
) -> Result<Pdf<R>>
where
R: Read + Seek,
{
let input_name = input_name.into();
self.open_for_encryption_inspection_with_description(source, input_name.as_bytes(), options)
}
pub fn open_for_encryption_inspection_with_description<R>(
&mut self,
source: R,
input_name: impl AsRef<[u8]>,
mut options: PdfOpenOptions,
) -> Result<Pdf<R>>
where
R: Read + Seek,
{
let input_name = input_name.as_ref().to_vec();
self.set_input_name_bytes(&input_name);
options.logger = Some(self.logger.clone());
options.description = input_name;
options.verbose |= self.configuration.verbose;
options.message_prefix = self.message_prefix.as_bytes().to_vec();
options.suppress_warnings |= self.suppress_warnings;
let mut pdf = Pdf::open_for_encryption_inspection(source, options)?;
let authentication_failed = pdf.is_encrypted() && pdf.encryption_file_key().is_none();
if !authentication_failed {
pdf.root_handle()?;
}
self.record_document_warnings(&pdf);
Ok(pdf)
}
pub fn update_from_json<R, S>(
&mut self,
pdf: &mut Pdf<R>,
source: S,
input_name: impl AsRef<[u8]>,
) -> Result<()>
where
R: Read + Seek,
S: Read + Seek + 'static,
{
let input_name = input_name.as_ref().to_vec();
pdf.set_logger(self.logger.clone());
pdf.update_from_json(source, input_name)?;
self.record_document_warnings(pdf);
Ok(())
}
pub fn inspect<R, F, E>(
&mut self,
pdf: &mut Pdf<R>,
inspection: F,
) -> std::result::Result<JobExitCode, E>
where
R: Read + Seek,
F: FnOnce(&mut Pdf<R>) -> std::result::Result<(), E>,
E: From<crate::Error>,
{
pdf.set_logger(self.logger.clone());
inspection(pdf)?;
self.drain_document_warnings(pdf);
self.complete(false)?;
Ok(self.get_exit_code())
}
pub fn write_json<R>(
&mut self,
pdf: &mut Pdf<R>,
options: JsonJobOptions<'_>,
output: JsonJobOutput<'_>,
) -> std::result::Result<JobExitCode, JsonJobError>
where
R: Read + Seek,
{
self.write_json_with_version(pdf, 2, false, false, false, false, options, output)
}
#[allow(clippy::too_many_arguments)]
pub fn write_json_with_version<R>(
&mut self,
pdf: &mut Pdf<R>,
version: i32,
test_json_schema: bool,
json_output: bool,
show_encryption_key: bool,
verbose: bool,
options: JsonJobOptions<'_>,
output: JsonJobOutput<'_>,
) -> std::result::Result<JobExitCode, JsonJobError>
where
R: Read + Seek,
{
let output_filename = match &output {
JsonJobOutput::File { filename, .. } => Some((*filename).to_path_buf()),
JsonJobOutput::Stdout(_) => None,
};
let creates_output = output_filename.is_some();
self.write_json_without_completion(
pdf,
version,
test_json_schema,
json_output,
show_encryption_key,
options,
output,
)?;
if verbose || self.configuration.verbose {
if let Some(filename) = output_filename {
let mut message = self.message_prefix.as_bytes().to_vec();
message.extend_from_slice(b": wrote file ");
message.extend_from_slice(&path_description_bytes(&filename));
message.push(b'\n');
self.logger.info(message)?;
}
}
self.drain_document_warnings(pdf);
self.complete(creates_output)?;
Ok(self.get_exit_code())
}
#[allow(clippy::too_many_arguments)]
fn write_json_without_completion<R>(
&mut self,
pdf: &mut Pdf<R>,
version: i32,
test_json_schema: bool,
json_output: bool,
show_encryption_key: bool,
options: JsonJobOptions<'_>,
output: JsonJobOutput<'_>,
) -> std::result::Result<(), JsonJobError>
where
R: Read + Seek,
{
pdf.set_logger(self.logger.clone());
super::json::write_json_with_version_with_logger(
pdf,
version,
test_json_schema,
json_output,
show_encryption_key,
options,
output,
&self.logger,
)?;
Ok(())
}
pub fn record_warnings(&mut self) {
self.warnings = true;
}
pub fn record_document_warnings<R>(&mut self, pdf: &Pdf<R>)
where
R: Read + Seek,
{
if pdf.any_warnings() {
self.record_warnings();
}
}
pub(crate) fn drain_document_warnings<R>(&mut self, pdf: &Pdf<R>)
where
R: Read + Seek,
{
if !pdf.get_warnings().is_empty() {
self.record_warnings();
}
}
#[must_use]
pub fn has_warnings(&self) -> bool {
self.warnings
}
pub fn set_suppress_warnings(&mut self, value: bool) {
self.suppress_warnings = value;
}
pub(crate) fn warnings_suppressed(&self) -> bool {
self.suppress_warnings
}
pub fn set_warnings_exit_zero(&mut self, value: bool) {
self.warnings_exit_zero = value;
}
#[must_use]
pub fn get_exit_code(&self) -> JobExitCode {
if self.configuration.is_encrypted {
return if self.encryption_status.encrypted {
JobExitCode::Success
} else {
JobExitCode::Error
};
}
if self.configuration.requires_password {
return if !self.encryption_status.encrypted {
JobExitCode::Error
} else if self.encryption_status.password_incorrect {
JobExitCode::Success
} else {
JobExitCode::Warning
};
}
if self.warnings && !self.warnings_exit_zero {
JobExitCode::Warning
} else {
JobExitCode::Success
}
}
pub fn complete(&self, creates_output: bool) -> Result<()> {
if self.warnings && !self.suppress_warnings {
let suffix = if creates_output {
"; resulting file may have some problems"
} else {
""
};
self.logger.warn(format!(
"{}: operation succeeded with warnings{suffix}\n",
self.message_prefix
))?;
}
Ok(())
}
}
pub(crate) fn qpdf_file_io_source_message(source: &std::io::Error) -> String {
let message = match source.kind() {
std::io::ErrorKind::NotFound => Some("No such file or directory"),
std::io::ErrorKind::PermissionDenied => Some("Permission denied"),
std::io::ErrorKind::AlreadyExists => Some("File exists"),
std::io::ErrorKind::InvalidInput => Some("Invalid argument"),
std::io::ErrorKind::IsADirectory => Some("Is a directory"),
std::io::ErrorKind::NotADirectory => Some("Not a directory"),
_ => None,
};
if let Some(message) = message {
return message.to_owned();
}
let rendered = source.to_string();
source
.raw_os_error()
.and_then(|code| rendered.strip_suffix(&format!(" (os error {code})")))
.unwrap_or(&rendered)
.to_owned()
}
impl QPDFJobConfig<'_> {
pub fn copy_encryption(
&mut self,
path: impl Into<PathBuf>,
password: impl Into<Vec<u8>>,
) -> &mut Self {
self.job.configuration.copy_encryption = Some(path.into());
self.job.configuration.encryption_file_password = password.into();
self.job.configuration.writer.clear_encryption_parameters();
self
}
pub fn input_file(&mut self, input_file: impl Into<PathBuf>) -> Result<&mut Self> {
self.job.set_input_file(input_file)?;
Ok(self)
}
pub fn empty_input(&mut self) -> Result<&mut Self> {
if self.job.configuration.input_file.is_some() || self.job.configuration.empty_input {
return Err(Error::Usage(UsageError::new(
"empty input can't be used since input file has already been given",
)));
}
self.job.configuration.empty_input = true;
Ok(self)
}
pub fn output_file(&mut self, output_file: impl Into<PathBuf>) -> Result<&mut Self> {
self.job.set_output_file(output_file)?;
Ok(self)
}
pub fn replace_input(&mut self) -> Result<&mut Self> {
if self.job.configuration.output_file.is_some() || self.job.configuration.replace_input {
return Err(Error::Usage(UsageError::new(
"replace-input can't be used since output file has already been given",
)));
}
self.job.configuration.replace_input = true;
Ok(self)
}
pub fn json_input(&mut self) -> &mut Self {
self.job.configuration.json_input = true;
self
}
pub fn update_from_json(&mut self, path: impl Into<PathBuf>) -> &mut Self {
self.job.configuration.update_from_json = Some(path.into());
self
}
pub fn set_page_labels<I, S>(&mut self, specs: I) -> Result<&mut Self>
where
I: IntoIterator<Item = S>,
S: AsRef<[u8]>,
{
let specs = specs
.into_iter()
.map(|spec| parse_page_label_spec(spec.as_ref()))
.collect::<Result<Vec<_>>>()?;
self.job.configuration.set_page_labels = Some(specs);
Ok(self)
}
pub fn remove_page_labels(&mut self) -> &mut Self {
self.job.configuration.remove_page_labels = true;
self
}
pub fn remove_restrictions(&mut self) -> &mut Self {
self.job.configuration.remove_restrictions = true;
self
}
pub fn coalesce_contents(&mut self) -> &mut Self {
self.job.configuration.coalesce_contents = true;
self
}
pub fn check(&mut self) -> &mut Self {
self.job.configuration.check = true;
self.job.configuration.require_output = false;
self
}
pub fn check_linearization(&mut self) -> &mut Self {
self.job.configuration.check_linearization = true;
self.job.configuration.require_output = false;
self
}
pub fn show_npages(&mut self) -> &mut Self {
self.job.configuration.show_npages = true;
self.job.configuration.require_output = false;
self
}
pub fn show_pages(&mut self) -> &mut Self {
self.job.configuration.show_pages = true;
self.job.configuration.require_output = false;
self
}
pub fn show_xref(&mut self) -> &mut Self {
self.job.configuration.show_xref = true;
self.job.configuration.require_output = false;
self
}
pub fn show_linearization(&mut self) -> &mut Self {
self.job.configuration.show_linearization = true;
self.job.configuration.require_output = false;
self
}
pub fn show_encryption(&mut self) -> &mut Self {
self.job.configuration.show_encryption = true;
self.job.configuration.require_output = false;
self
}
pub fn list_attachments(&mut self) -> &mut Self {
self.job.configuration.list_attachments = true;
self.job.configuration.require_output = false;
self
}
pub fn show_attachment(&mut self, key: impl Into<Vec<u8>>) -> &mut Self {
self.job.configuration.show_attachment = Some(key.into());
self.job.configuration.require_output = false;
self
}
pub fn raw_stream_data(&mut self) -> &mut Self {
self.job.configuration.show_raw_stream_data = true;
self
}
pub fn filtered_stream_data(&mut self) -> &mut Self {
self.job.configuration.show_filtered_stream_data = true;
self
}
pub fn normalize_content(&mut self, value: bool) -> &mut Self {
self.job.set_content_normalization(value);
self
}
pub fn generate_appearances(&mut self) -> &mut Self {
self.job.configuration.generate_appearances = true;
self
}
pub fn flatten_annotations(&mut self, mode: FlattenAnnotationsMode) -> &mut Self {
self.job.configuration.flatten_annotations = Some(mode);
self
}
pub fn flatten_rotation(&mut self) -> &mut Self {
self.job.configuration.flatten_rotation = true;
self
}
pub fn rotate(&mut self, parameter: impl AsRef<[u8]>) -> Result<&mut Self> {
let parameter = parse_rotation_parameter(parameter.as_ref())?;
self.job
.configuration
.rotations
.insert(parameter.range, parameter.spec);
Ok(self)
}
pub fn collate(&mut self, parameter: impl AsRef<[u8]>) -> Result<&mut Self> {
let values = parse_qpdf_collate_parameter(parameter.as_ref())?;
self.job
.configuration
.collate
.get_or_insert_with(Vec::new)
.extend(values);
Ok(self)
}
pub fn split_pages(&mut self, parameter: impl AsRef<[u8]>) -> Result<&mut Self> {
self.job.configuration.split_pages = Some(parse_job_split_pages(parameter.as_ref())?);
Ok(self)
}
pub fn remove_unreferenced_resources(
&mut self,
mode: RemoveUnreferencedResources,
) -> &mut Self {
self.job.configuration.remove_unreferenced_resources = mode;
self
}
pub fn externalize_inline_images(&mut self, min_bytes: usize) -> &mut Self {
self.job.configuration.externalize_inline_images = true;
self.job.configuration.image_options.inline_min_bytes = min_bytes;
self
}
pub fn optimize_images(&mut self, options: ImageOptimizationOptions) -> &mut Self {
self.job.configuration.optimize_images = true;
self.job.configuration.image_options = options;
self
}
pub fn overlay(
&mut self,
path: impl Into<PathBuf>,
password: impl Into<Vec<u8>>,
from: PageRange,
to: PageRange,
repeat: Option<PageRange>,
) -> &mut Self {
self.job.configuration.overlays.push(JobOverlayConfig {
path: path.into(),
password: password.into(),
from,
to,
repeat,
kind: OverlayKind::Overlay,
});
self
}
pub fn underlay(
&mut self,
path: impl Into<PathBuf>,
password: impl Into<Vec<u8>>,
from: PageRange,
to: PageRange,
repeat: Option<PageRange>,
) -> &mut Self {
self.job.configuration.underlays.push(JobOverlayConfig {
path: path.into(),
password: password.into(),
from,
to,
repeat,
kind: OverlayKind::Underlay,
});
self
}
pub fn writer_configuration(&mut self, configuration: WriterConfiguration) -> &mut Self {
self.job.configuration.writer = configuration;
self
}
pub fn add_page_spec(
&mut self,
file: impl Into<PathBuf>,
range: &str,
password: Option<Vec<u8>>,
) -> Result<&mut Self> {
if self.job.configuration.page_specs_origin == PageSpecsOrigin::Json {
return Err(Error::Usage(UsageError::new(
"--pages may only be specified one time",
)));
}
let range = if range.is_empty() {
PageRange::all()
} else {
PageRange::parse_numrange(range)
.map_err(|error| Error::Usage(UsageError::new(error.to_string())))?
};
self.job.configuration.page_specs_origin = PageSpecsOrigin::Config;
self.job.configuration.page_specs.push(JobPageConfig {
path: file.into(),
password,
range,
});
Ok(self)
}
pub fn add_attachment(&mut self, options: AttachmentAddOptions) -> &mut Self {
self.job.configuration.attachments_to_add.push(options);
self
}
pub fn remove_attachment(&mut self, key: impl Into<Vec<u8>>) -> &mut Self {
self.job
.configuration
.attachments_to_remove
.push(key.into());
self
}
pub fn copy_attachments_from(
&mut self,
path: impl Into<PathBuf>,
password: impl Into<Vec<u8>>,
prefix: impl Into<Vec<u8>>,
) -> &mut Self {
self.job
.configuration
.attachments_to_copy
.push(JobCopyAttachmentsConfig {
path: path.into(),
password: password.into(),
prefix: prefix.into(),
});
self
}
pub fn qdf(&mut self) -> &mut Self {
self.job.configuration.writer.set_qdf_mode(true);
self
}
pub fn deterministic_id(&mut self) -> &mut Self {
self.job.configuration.writer.set_deterministic_id(true);
self
}
pub fn object_streams(&mut self, mode: &str) -> Result<&mut Self> {
self.job
.configuration
.writer
.set_object_stream_mode(parse_object_stream_mode(mode)?);
Ok(self)
}
pub fn progress(&mut self) -> &mut Self {
self.job.set_progress(true);
self
}
pub fn verbose(&mut self) -> &mut Self {
self.job.set_verbose(true);
self
}
pub fn show_object(&mut self, selector: &str) -> Result<&mut Self> {
self.job.configuration.show_object = Some(parse_job_object_selector(selector.as_bytes())?);
self.job.configuration.require_output = false;
Ok(self)
}
pub fn check_configuration(&mut self) -> Result<()> {
self.job.check_configuration()
}
}
fn map_show_linearization_error(error: ShowLinearizationError) -> Error {
match error {
ShowLinearizationError::Io(error) => match error.downcast::<Error>() {
Ok(error) => *error,
Err(error) => Error::System(format!("I/O error: {error}")),
},
ShowLinearizationError::Malformed { message } => {
Error::System(format!("malformed linearization data: {message}"))
}
}
}
fn parse_object_stream_mode(value: &str) -> Result<ObjectStreamMode> {
match value {
"preserve" => Ok(ObjectStreamMode::Preserve),
"disable" => Ok(ObjectStreamMode::Disable),
"generate" => Ok(ObjectStreamMode::Generate),
other => Err(Error::Unsupported(format!(
"qpdfjob: invalid objectStreams value {other}"
))),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::job::overlay::OverlayVerboseSource;
use crate::{Error, ObjectHandle, PageDocumentHelper, PageInput, PdfOpenOptions};
use std::io::Cursor;
#[test]
fn config_verbose_enables_the_job_verbose_setting() {
let mut job = QPDFJob::new();
job.config().verbose();
assert!(job.verbose());
}
#[test]
fn config_normalize_content_enables_the_job_inspection_setting() {
let mut job = QPDFJob::new();
job.config().normalize_content(true);
assert!(job.content_normalization_enabled());
}
#[test]
fn input_version_floor_exposes_the_highest_opened_source_version() {
let bytes = std::fs::read(
std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../tests/fixtures/compat/one-page-v17.pdf"),
)
.expect("committed versioned fixture");
let mut job = QPDFJob::new();
job.open_document(
Cursor::new(bytes),
"one-page-v17.pdf",
PdfOpenOptions::default(),
)
.expect("versioned fixture opens");
assert_eq!(
job.input_version_floor(),
Some(PdfVersion::new(1, 7, 0)),
"the public floor must expose qpdf's accumulated max_input_version"
);
}
#[test]
fn explicit_normalize_content_survives_a_later_writer_configuration_replacement() {
let mut pdf = Pdf::empty().expect("empty PDF");
let contents = pdf
.new_stream_with_data(Rc::new(b"q\rQ".to_vec()))
.expect("content stream");
let page = ObjectHandle::dictionary(vec![
(b"/Type".to_vec(), ObjectHandle::name(b"Page".to_vec())),
(
b"/MediaBox".to_vec(),
ObjectHandle::array(vec![
ObjectHandle::integer(0),
ObjectHandle::integer(0),
ObjectHandle::integer(100),
ObjectHandle::integer(100),
]),
),
(b"/Contents".to_vec(), contents),
]);
PageDocumentHelper::new(&mut pdf)
.add_page(PageInput::direct(page), false)
.expect("page insertion");
let tempdir = tempfile::tempdir().expect("temporary directory");
let output_path = tempdir.path().join("normalized.pdf");
let mut job = QPDFJob::new();
job.set_output_file(&output_path).expect("output path");
job.set_content_normalization(true);
job.config()
.writer_configuration(WriterConfiguration::default());
job.write_qpdf(&mut pdf)
.expect("normalized document should be written");
let mut written = Pdf::open(Cursor::new(
std::fs::read(&output_path).expect("written PDF bytes"),
))
.expect("written PDF should reopen");
let page_ref = PageDocumentHelper::new(&mut written)
.get_all_pages()
.expect("written page list")
.into_iter()
.next()
.expect("written page");
let page = written.get_object_handle(page_ref);
page.try_is_scalar().expect("written page resolves");
let contents = page
.try_get_key(b"/Contents")
.expect("written contents key")
.object_ref()
.expect("written contents reference");
let stream = written.get_object_handle(contents);
assert_eq!(
stream
.get_stream_data(crate::writer::DecodeLevel::All)
.expect("written stream data")
.as_ref(),
b"q\nQ"
);
}
#[test]
fn job_error_message_uses_qpdf_portable_not_found_text() {
let missing = Error::file_io(
"open",
"missing-parent/output.pdf",
std::io::Error::from(std::io::ErrorKind::NotFound),
);
assert_eq!(
QPDFJob::job_error_message(&missing),
b"open missing-parent/output.pdf: No such file or directory"
);
let fallback = Error::file_io(
"open",
"output.pdf",
std::io::Error::other("native fallback"),
);
assert_eq!(
QPDFJob::job_error_message(&fallback),
b"open output.pdf: native fallback"
);
}
#[test]
fn config_writer_configuration_reaches_the_job_writer() {
let tempdir = tempfile::tempdir().expect("temporary output directory");
let output = tempdir.path().join("static-id.pdf");
let mut pdf = Pdf::open(Cursor::new(
std::fs::read(
std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../tests/fixtures/compat/one-page.pdf"),
)
.expect("committed one-page fixture"),
))
.expect("one-page fixture parses");
let mut job = QPDFJob::new();
job.set_output_file(&output)
.expect("output path is accepted");
let mut writer = WriterConfiguration::default();
writer.set_static_id(true);
job.config().writer_configuration(writer);
job.write_qpdf(&mut pdf).expect("job write succeeds");
let bytes = std::fs::read(&output).expect("static-id output exists");
assert!(
bytes
.windows(b"<31415926535897932384626433832795>".len())
.any(|window| window == b"<31415926535897932384626433832795>"),
"writer configuration must reach the canonical writer"
);
}
#[test]
fn overlay_verbose_progress_rejects_an_invalid_source_index() {
let job = QPDFJob::new();
let report = [OverlayVerbosePage {
dest_page: 1,
sources: vec![OverlayVerboseSource {
spec_index: 0,
kind: OverlayKind::Overlay,
src_page: 1,
}],
}];
let configuration = job.configuration.clone();
let error = job
.report_overlay_progress(&report, &configuration)
.expect_err("an unpaired verbose source must be rejected");
assert!(matches!(
error,
Error::Internal(message) if message == "overlay verbose source index out of range"
));
}
#[test]
fn create_empty_document_keeps_the_input_configuration_reusable() {
let mut job = QPDFJob::new();
job.create_empty_document().expect("empty document");
assert_eq!(
job.page_spec_source_sort_key(0, b"empty PDF"),
Vec::<u8>::new()
);
assert_eq!(
job.page_spec_source_sort_key(1, b"other.pdf"),
b"other.pdf".to_vec()
);
assert!(
!job.configuration.empty_input,
"the factory must not configure --empty"
);
job.set_input_file("input.pdf")
.expect("a job that created an empty document can still take an input file");
assert_eq!(
job.page_spec_source_sort_key(0, b"input.pdf"),
Vec::<u8>::new(),
"the empty primary created earlier keeps qpdf's empty map key"
);
}
fn trailer_root_pdf(root: &str) -> Vec<u8> {
let mut bytes = b"%PDF-1.4\n".to_vec();
let xref_start = bytes.len();
bytes.extend_from_slice(b"xref\n0 1\n0000000000 65535 f \n");
bytes.extend_from_slice(
format!("trailer\n<< /Size 1 /Root {root} >>\nstartxref\n{xref_start}\n%%EOF\n")
.as_bytes(),
);
bytes
}
#[test]
fn open_rejects_a_dangling_root_before_returning_a_job_document() {
let mut job = QPDFJob::new();
assert!(matches!(
job.open(
Cursor::new(trailer_root_pdf("99 0 R")),
"dangling-root.pdf",
PdfOpenOptions::default(),
),
Err(Error::QpdfExc(warning)) if warning.get_message_detail() == b"unable to find /Root dictionary"
));
}
#[test]
fn open_rejects_a_non_dictionary_root_before_returning_a_job_document() {
let mut job = QPDFJob::new();
assert!(matches!(
job.open(
Cursor::new(trailer_root_pdf("42")),
"wrong-type-root.pdf",
PdfOpenOptions::default(),
),
Err(Error::QpdfExc(warning)) if warning.get_message_detail() == b"unable to find /Root dictionary"
));
}
#[test]
fn open_accepts_a_direct_dictionary_root() {
let mut job = QPDFJob::new();
assert!(job
.open(
Cursor::new(trailer_root_pdf("<< /Type /Catalog >>")),
"direct-root.pdf",
PdfOpenOptions::default(),
)
.is_ok());
}
#[test]
fn page_label_transform_noops_when_the_document_has_no_root() {
let mut bytes = Vec::new();
bytes.extend_from_slice(b"%PDF-1.7\n");
let off1 = bytes.len() as u64;
bytes.extend_from_slice(b"1 0 obj\n<< /Type /Catalog >>\nendobj\n");
let xref = bytes.len() as u64;
bytes.extend_from_slice(
format!(
"xref\n0 2\n0000000000 65535 f \n{off1:010} 00000 n \ntrailer\n<< /Size 2 >>\nstartxref\n{xref}\n%%EOF\n"
)
.as_bytes(),
);
let mut pdf = Pdf::open(Cursor::new(bytes)).expect("rootless trailer still opens");
let mut job = QPDFJob::new();
job.configuration.set_page_labels = Some(Vec::new());
let configuration = job.configuration.clone();
job.apply_page_label_transformations(&mut pdf, &configuration)
.expect("missing root is a qpdf-tolerant no-op");
}
#[test]
fn page_label_transform_noops_when_the_root_is_not_a_dictionary() {
let mut pdf = Pdf::empty().expect("empty PDF has a root");
let root_ref = pdf.root_ref().expect("empty PDF has a root");
pdf.replace_object(root_ref, ObjectHandle::integer(0))
.expect("replace the root with a scalar");
let mut job = QPDFJob::new();
job.configuration.set_page_labels = Some(Vec::new());
let configuration = job.configuration.clone();
job.apply_page_label_transformations(&mut pdf, &configuration)
.expect("non-dictionary root is a qpdf-tolerant no-op");
}
struct RecordingInfoSink {
bytes: std::sync::Arc<std::sync::Mutex<Vec<u8>>>,
}
impl crate::pipeline::Pipeline for RecordingInfoSink {
fn identifier(&self) -> &str {
"recording info sink"
}
fn write(&mut self, data: &[u8]) -> crate::pipeline::PipelineResult<()> {
self.bytes.lock().unwrap().extend_from_slice(data);
Ok(())
}
fn finish(&mut self) -> crate::pipeline::PipelineResult<()> {
Ok(())
}
}
#[test]
fn open_for_encryption_inspection_applies_the_job_verbose_policy_and_prefix() {
let mut source = Pdf::open(Cursor::new(
std::fs::read(
std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../tests/fixtures/minimal.pdf"),
)
.expect("committed minimal fixture"),
))
.expect("minimal fixture parses");
let mut writer = crate::PdfWriter::new(&mut source);
writer.set_encryption_parameters(crate::EncryptParams::v4_aes128(
b"caf\xe9".to_vec(),
b"owner".to_vec(),
));
writer.set_output_memory().expect("memory output");
writer.write().expect("encrypt fixture");
let encrypted = writer.get_buffer().expect("encrypted bytes");
let bytes = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
let logger = crate::QPDFLogger::create();
logger.set_info(Some(crate::pipeline::PipelineHandle::new(
RecordingInfoSink {
bytes: std::sync::Arc::clone(&bytes),
},
)));
let mut job = QPDFJob::new();
job.set_logger(logger);
job.configuration.verbose = true;
job.set_message_prefix("job");
let pdf = job
.open_for_encryption_inspection(
Cursor::new(encrypted),
"input.pdf",
PdfOpenOptions {
password: "caf\u{e9}".as_bytes().to_vec(),
..PdfOpenOptions::default()
},
)
.expect("qpdf-compatible password recovery authenticates");
drop(pdf);
let output = bytes.lock().unwrap();
assert!(
output.starts_with(b"job: supplied password didn't work; trying other"),
"the inspection open must emit the job-prefixed retry line: {:?}",
String::from_utf8_lossy(&output) );
}
#[test]
fn open_for_encryption_inspection_walks_the_root_after_successful_raw_key_auth() {
let mut source = std::fs::read(
std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../tests/fixtures/encrypted/v5-aes-256-r6.pdf"),
)
.expect("committed encrypted fixture");
let needle = b"/Root 1 0 R";
let at = source
.windows(needle.len())
.position(|window| window == needle)
.expect("fixture has a /Root 1 0 R trailer reference");
source[at..at + needle.len()].copy_from_slice(b"/Root 9 0 R");
let hex_key = b"fc459408a5282b7c59daa5162f860e82315679cc04942ef57993bfd287f30290".to_vec();
let mut job = QPDFJob::new();
let result = job.open_for_encryption_inspection(
Cursor::new(source),
"dangling-root-hex-key.pdf",
PdfOpenOptions {
password: hex_key,
password_is_hex_key: true,
..PdfOpenOptions::default()
},
);
match result {
Err(Error::QpdfExc(warning)) => assert_eq!(
warning.get_message_detail(),
b"unable to find /Root dictionary"
),
Err(other) => {
panic!("expected the dangling-/Root error, got a different error: {other}")
}
Ok(_) => panic!(
"a successful raw-key open must still surface a dangling /Root, \
proving the root walk ran rather than being skipped as a \
(mis-detected) authentication failure"
),
}
}
#[test]
fn show_linearization_mapping_preserves_core_errors() {
let error =
ShowLinearizationError::Io(Box::new(Error::System("sink write failure 1".to_owned())));
let mapped = map_show_linearization_error(error);
assert!(matches!(
mapped,
Error::System(message) if message == "sink write failure 1"
));
let error = ShowLinearizationError::Io(Box::new(std::io::Error::other("disk gone")));
assert!(matches!(
map_show_linearization_error(error),
Error::System(message) if message == "I/O error: disk gone"
));
let error = ShowLinearizationError::Malformed {
message: "bad hint table".to_owned(),
};
assert!(matches!(
map_show_linearization_error(error),
Error::System(message) if message == "malformed linearization data: bad hint table"
));
}
#[test]
fn configured_inspection_maps_detected_check_errors_to_an_operation_error() {
let mut pdf = Pdf::open(Cursor::new(
include_bytes!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../tests/fixtures/compat/one-page.pdf"
))
.to_vec(),
))
.expect("one-page fixture parses");
let page_ref = PageDocumentHelper::new(&mut pdf)
.get_all_pages()
.expect("page tree resolves")[0];
let page = pdf.get_object_handle(page_ref);
page.try_dereference().expect("page resolves");
page.replace_key(b"/Contents", ObjectHandle::integer(42))
.expect("page remains mutable");
let mut job = QPDFJob::new();
job.config().check();
let failure = job
.inspect_configured(&mut pdf)
.expect_err("check errors must abort the enclosing inspection");
assert!(matches!(failure, crate::job::CheckError::ErrorsDetected));
}
#[test]
fn inspect_configured_completes_and_reports_memory_usage() {
let mut pdf = Pdf::open(Cursor::new(
include_bytes!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../tests/fixtures/minimal.pdf"
))
.to_vec(),
))
.expect("minimal fixture parses");
let logger = QPDFLogger::create();
logger.set_warn(Some(PipelineHandle::new(crate::pipeline::Discard)));
let mut job = QPDFJob::new();
job.set_logger(logger);
job.config().show_npages();
job.configuration.report_memory_usage = true;
assert_eq!(
job.inspect_configured(&mut pdf)
.expect("configured inspection succeeds"),
JobExitCode::Success
);
}
#[test]
fn job_output_pipeline_exposes_its_qpdf_identifier() {
let pipeline = JobOutputPipeline(PipelineHandle::new(crate::pipeline::Discard));
assert_eq!(pipeline.identifier(), "qpdf job output");
}
#[test]
fn job_output_writer_forwards_bytes_and_flush() {
let mut writer = JobOutputWriter(PipelineHandle::new(crate::pipeline::Discard));
std::io::Write::write_all(&mut writer, b"job output").unwrap();
std::io::Write::flush(&mut writer).unwrap();
}
#[test]
fn write_qpdf_reports_verbose_auto_password_conversion() {
let tempdir = tempfile::tempdir().expect("temporary output directory");
let output = tempdir.path().join("output.pdf");
let input = Pdf::open(Cursor::new(
std::fs::read(
std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../tests/fixtures/minimal.pdf"),
)
.expect("committed minimal fixture"),
))
.expect("minimal fixture parses");
let bytes = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
let logger = QPDFLogger::create();
logger.set_info(Some(PipelineHandle::new(RecordingInfoSink {
bytes: std::sync::Arc::clone(&bytes),
})));
logger.set_warn(Some(logger.discard()));
logger.set_error(Some(logger.discard()));
let mut job = QPDFJob::new();
job.set_logger(logger);
job.set_output_file(&output)
.expect("output path is accepted");
job.set_verbose(true);
job.configuration
.writer
.set_encryption_parameters(crate::EncryptParams::v4_aes128(
"café".as_bytes().to_vec(),
b"owner".to_vec(),
));
let mut input = input;
job.write_qpdf(&mut input).expect("job write succeeds");
assert_eq!(job.get_exit_code(), JobExitCode::Success);
assert!(output.is_file());
assert!(
bytes.lock().unwrap().windows(
b"qpdf: automatically converting Unicode password to single-byte encoding as required for 40-bit or 128-bit encryption\n".len()
).any(|window| window == b"qpdf: automatically converting Unicode password to single-byte encoding as required for 40-bit or 128-bit encryption\n"),
"verbose writer output must include qpdf's auto-conversion info"
);
}
#[test]
fn write_qpdf_closes_retained_page_sources_before_replace_input() {
let primary_fixture = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../tests/fixtures/compat/one-page.pdf");
let secondary = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../tests/fixtures/compat/attachment-two-page.pdf");
let tempdir = tempfile::tempdir().expect("temporary replace-input directory");
let primary = tempdir.path().join("primary.pdf");
std::fs::copy(&primary_fixture, &primary).expect("copy primary fixture");
let json = serde_json::json!({
"inputFile": primary,
"replaceInput": "",
"pages": [
{"file": ".", "range": "1"},
{"file": secondary, "range": "1"}
]
})
.to_string();
let mut job = QPDFJob::new();
job.initialize_from_json(&json)
.expect("multi-source replace-input configuration");
let mut pdf = job
.create_qpdf()
.expect("create qpdf")
.expect("primary document");
assert_eq!(job.page_source_documents.len(), 2);
assert!(job
.page_source_documents
.iter()
.all(|source| !source.resolver.input_source_closed()));
job.write_qpdf(&mut pdf)
.expect("replace-input write succeeds");
assert!(job.page_source_documents.iter().all(|source| {
source.resolver.input_source_closed()
&& source
.input_source_control
.as_ref()
.is_none_or(|control| control.is_closed_for_test())
}));
assert!(primary.is_file(), "replace-input keeps the input path");
}
#[test]
fn write_qpdf_closes_retained_overlay_sources_before_replace_input() {
let primary_fixture = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../tests/fixtures/compat/one-page.pdf");
let tempdir = tempfile::tempdir().expect("temporary replace-input directory");
let primary = tempdir.path().join("primary.pdf");
std::fs::copy(&primary_fixture, &primary).expect("copy primary fixture");
let json = serde_json::json!({
"inputFile": primary,
"replaceInput": "",
"overlay": {"file": primary, "from": "1", "to": "1"}
})
.to_string();
let mut job = QPDFJob::new();
job.initialize_from_json(&json)
.expect("self-overlay replace-input configuration");
let mut pdf = job
.create_qpdf()
.expect("create qpdf")
.expect("primary document");
assert_eq!(job.overlay_sources.len(), 1);
assert!(!job.overlay_sources[0].source.resolver.input_source_closed());
job.write_qpdf(&mut pdf)
.expect("replace-input write succeeds");
let overlay = &job.overlay_sources[0].source;
assert!(overlay.resolver.input_source_closed());
assert!(overlay
.input_source_control
.as_ref()
.is_none_or(|control| control.is_closed_for_test()));
assert!(primary.is_file(), "replace-input keeps the input path");
}
#[test]
fn write_qpdf_emits_auto_password_notices_in_user_owner_order() {
let tempdir = tempfile::tempdir().expect("temporary output directory");
let output = tempdir.path().join("output.pdf");
let mut input = Pdf::open(Cursor::new(
std::fs::read(
std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../tests/fixtures/minimal.pdf"),
)
.expect("committed minimal fixture"),
))
.expect("minimal fixture parses");
let bytes = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
let logger = QPDFLogger::create();
let sink = PipelineHandle::new(RecordingInfoSink {
bytes: std::sync::Arc::clone(&bytes),
});
logger.set_info(Some(sink.clone()));
logger.set_warn(Some(logger.discard()));
logger.set_error(Some(sink));
let mut job = QPDFJob::new();
job.set_logger(logger);
job.set_output_file(&output)
.expect("output path is accepted");
job.set_verbose(true);
job.configuration.password_mode = PasswordMode::Auto;
job.configuration
.writer
.set_encryption_parameters(crate::EncryptParams::v4_aes128(
"日本".as_bytes().to_vec(),
"café".as_bytes().to_vec(),
));
job.write_qpdf(&mut input).expect("job write succeeds");
assert_eq!(job.get_exit_code(), JobExitCode::Success);
let output = bytes.lock().unwrap();
let warning = b"qpdf: WARNING: supplied password looks like a Unicode password with characters not allowed in passwords for 40-bit and 128-bit encryption; most readers will not be able to open this file with the supplied password. (Use --password-mode=bytes to suppress this warning and use the password anyway.)\n";
let info = b"qpdf: automatically converting Unicode password to single-byte encoding as required for 40-bit or 128-bit encryption\n";
let warning_at = output
.windows(warning.len())
.position(|window| window == warning);
let info_at = output.windows(info.len()).position(|window| window == info);
assert!(
warning_at
.is_some_and(|warning_at| info_at.is_some_and(|info_at| warning_at < info_at)),
"user warning must precede owner info in the shared logger stream"
);
}
#[test]
fn write_qpdf_propagates_verbose_auto_password_info_sink_error() {
let tempdir = tempfile::tempdir().expect("temporary output directory");
let output = tempdir.path().join("output.pdf");
let mut input = Pdf::open(Cursor::new(
std::fs::read(
std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../tests/fixtures/minimal.pdf"),
)
.expect("committed minimal fixture"),
))
.expect("minimal fixture parses");
let logger = QPDFLogger::create();
logger.set_info(Some(PipelineHandle::new(
crate::pipeline::test_support::NthWriteFailure::new(1),
)));
let mut job = QPDFJob::new();
job.set_logger(logger);
job.set_output_file(&output)
.expect("output path is accepted");
job.set_verbose(true);
job.configuration
.writer
.set_encryption_parameters(crate::EncryptParams::v4_aes128(
"café".as_bytes().to_vec(),
b"owner".to_vec(),
));
let error = job
.write_qpdf(&mut input)
.expect_err("info sink failure must propagate");
assert!(matches!(
error,
Error::System(message) if message == "sink write failure 1"
));
}
#[test]
fn write_qpdf_reserves_stdout_before_auto_password_diagnostic() {
let mut input = Pdf::open(Cursor::new(
std::fs::read(
std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../tests/fixtures/minimal.pdf"),
)
.expect("committed minimal fixture"),
))
.expect("minimal fixture parses");
let errors = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
let logger = QPDFLogger::create();
logger.set_error(Some(PipelineHandle::new(RecordingInfoSink {
bytes: std::sync::Arc::clone(&errors),
})));
logger.set_warn(Some(logger.discard()));
let mut job = QPDFJob::new();
job.set_logger(logger);
job.set_output_file("-").expect("stdout output is accepted");
job.set_verbose(true);
job.configuration.password_mode = PasswordMode::Auto;
job.configuration.writer.set_deterministic_id(true);
job.configuration
.writer
.set_encryption_parameters(crate::EncryptParams::v4_aes128(
"café".as_bytes().to_vec(),
b"owner".to_vec(),
));
let error = job
.write_qpdf(&mut input)
.expect_err("writer preflight error must be returned to the caller");
assert!(!error.to_string().is_empty());
let error = String::from_utf8_lossy(&errors.lock().unwrap()).into_owned();
assert!(
error.contains("deterministic") && !error.contains("called setSave"),
"stdout reservation must precede the diagnostic: {error:?}"
);
}
#[test]
fn apply_transformations_reserves_stdout_before_the_document_stage() {
let mut pdf = Pdf::open(Cursor::new(
std::fs::read(
std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../tests/fixtures/minimal.pdf"),
)
.expect("committed minimal fixture"),
))
.expect("minimal fixture parses");
let logger = QPDFLogger::create();
let mut job = QPDFJob::new();
job.set_logger(logger.clone());
job.set_output_file("-").expect("stdout output is accepted");
job.set_verbose(true);
job.apply_transformations(&mut pdf)
.expect("the create stage succeeds for the minimal fixture");
assert!(
logger
.get_save()
.expect("save pipeline")
.is_same(&logger.standard_output()),
"qpdf reserves stdout in checkConfiguration, which createQPDF runs \
before any transformation (QPDFJob.cc:428-431,614-626)"
);
assert!(
logger
.get_info()
.expect("info pipeline")
.is_same(&logger.standard_error()),
"reserving stdout must reroute info output to stderr so verbose \
transformations cannot consume the stream the PDF needs"
);
}
#[test]
fn password_interpretation_setters_reach_job_owned_opens() {
let mut job = QPDFJob::new();
job.set_password_mode(PasswordMode::HexBytes);
job.set_password_is_hex_key(true);
job.set_suppress_password_recovery(true);
let options = job.configured_open_options(b"75".to_vec());
assert_eq!(options.password_mode, PasswordMode::HexBytes);
assert!(options.password_is_hex_key);
assert!(options.suppress_password_recovery);
}
#[test]
fn write_qpdf_maps_direct_stdout_reservation_failure_to_job_error() {
let mut input = Pdf::open(Cursor::new(
std::fs::read(
std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../tests/fixtures/minimal.pdf"),
)
.expect("committed minimal fixture"),
))
.expect("minimal fixture parses");
let errors = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
let logger = QPDFLogger::create();
logger.set_error(Some(PipelineHandle::new(RecordingInfoSink {
bytes: std::sync::Arc::clone(&errors),
})));
logger.set_warn(Some(logger.discard()));
logger
.info(b"")
.expect("empty info write marks stdout as used");
let mut job = QPDFJob::new();
job.set_logger(logger);
job.set_output_file("-").expect("stdout output is accepted");
let error = job
.write_qpdf(&mut input)
.expect_err("stdout reservation failure must be returned to the caller");
assert!(!error.to_string().is_empty());
let error = String::from_utf8_lossy(&errors.lock().unwrap()).into_owned();
assert!(
error.contains(
"called setSave on standard output after standard output has already been used"
),
"direct write_qpdf must report the reservation failure: {error:?}"
);
}
#[test]
fn job_json_byte_entry_point_preserves_literal_high_bit_password_bytes() {
let mut json =
br#"{"inputFile":"input.pdf","outputFile":"output.pdf","password":""}"#.to_vec();
json.insert(json.len() - 2, 0x80);
let mut job = QPDFJob::new();
job.initialize_from_json_bytes(&json).unwrap();
assert_eq!(job.configuration.password, vec![0x80]);
}
#[test]
fn job_json_version_rejects_non_utf8_bytes_instead_of_replacing_them() {
let json = b"{\"inputFile\":\"input.pdf\",\"outputFile\":\"output.pdf\",\"forceVersion\":\"\xff\"}";
let error = QPDFJob::new()
.initialize_from_json_bytes(json)
.expect_err("non-UTF-8 job JSON version must not be lossy");
assert!(error.to_string().contains(".forceVersion"));
assert!(error.to_string().contains("UTF-8"));
}
#[test]
fn job_json_private_handlers_cover_qpdf_scalar_and_choice_shapes() {
let members = job_json_members(
&crate::json::Json::parse(
br#"{"empty":"","choice":"a","string":"text","number":1,"flag":false,"yes":"y"}"#,
)
.unwrap(),
);
assert!(job_json_string(&members, b"string").unwrap().is_some());
assert!(job_json_string(&members, b"missing").unwrap().is_none());
assert!(job_json_bare(&members, b"empty").unwrap());
assert!(!job_json_bare(&members, b"missing").unwrap());
assert!(job_json_bare(&members, b"string").is_err());
assert!(job_json_bare(&members, b"number").is_err());
assert_eq!(
job_json_choice(&members, b"choice", &["a", "b"], true).unwrap(),
Some("a".to_owned())
);
assert_eq!(
job_json_choice(&members, b"missing", &["a", "b"], true).unwrap(),
None
);
assert_eq!(
job_json_items(&crate::json::Json::parse(br#"["a","b"]"#).unwrap()).len(),
2
);
assert_eq!(
job_json_items(&crate::json::Json::parse(br#""a""#).unwrap()).len(),
1
);
assert!(job_json_required_string(&members, b"string", ".string").is_ok());
assert!(job_json_required_string(&members, b"missing", ".missing").is_err());
assert!(job_json_yn(&members, b"yes").unwrap().unwrap());
let empty_optional =
job_json_members(&crate::json::Json::parse(br#"{"choice":""}"#).unwrap());
assert_eq!(
job_json_choice(&empty_optional, b"choice", &["a", "b"], false).unwrap(),
Some(String::new())
);
let wrong_type =
job_json_members(&crate::json::Json::parse(br#"{"choice":false}"#).unwrap());
assert!(job_json_choice(&wrong_type, b"choice", &["a"], true).is_err());
let wrong_value =
job_json_members(&crate::json::Json::parse(br#"{"choice":"c"}"#).unwrap());
assert!(job_json_choice(&wrong_value, b"choice", &["a"], true).is_err());
let range_members =
job_json_members(&crate::json::Json::parse(br#"{"range":"1-2"}"#).unwrap());
assert!(job_json_range(range_members.get(b"range".as_slice()), ".range").is_ok());
assert!(job_json_range(None, ".range").is_ok());
let empty_range = job_json_members(&crate::json::Json::parse(br#"{"range":""}"#).unwrap());
assert_eq!(
job_json_range(empty_range.get(b"range".as_slice()), ".range")
.unwrap()
.resolve(3)
.unwrap(),
vec![1, 2, 3]
);
let bad_range_type =
job_json_members(&crate::json::Json::parse(br#"{"range":false}"#).unwrap());
assert!(job_json_range(bad_range_type.get(b"range".as_slice()), ".range").is_err());
let bad_range_syntax =
job_json_members(&crate::json::Json::parse(br#"{"range":"bad"}"#).unwrap());
assert!(job_json_range(bad_range_syntax.get(b"range".as_slice()), ".range").is_err());
let empty_overlay =
crate::json::Json::parse(br#"[{"file":"source.pdf","from":"","to":"","repeat":""}]"#)
.unwrap();
let mut overlays = Vec::new();
parse_job_overlay_specs(&mut overlays, &empty_overlay, OverlayKind::Overlay).unwrap();
assert_eq!(overlays.len(), 1);
assert_eq!(overlays[0].from.resolve(3).unwrap(), Vec::<u32>::new());
assert_eq!(overlays[0].to.resolve(3).unwrap(), Vec::<u32>::new());
assert_eq!(
overlays[0]
.repeat
.as_ref()
.expect("explicit empty repeat remains present")
.resolve(3)
.unwrap(),
Vec::<u32>::new()
);
let absent_overlay = crate::json::Json::parse(br#"[{"file":"source.pdf"}]"#).unwrap();
let mut overlays = Vec::new();
parse_job_overlay_specs(&mut overlays, &absent_overlay, OverlayKind::Overlay).unwrap();
assert_eq!(overlays[0].from.resolve(3).unwrap(), vec![1, 2, 3]);
assert_eq!(overlays[0].to.resolve(3).unwrap(), vec![1, 2, 3]);
assert!(overlays[0].repeat.is_none());
}
#[test]
fn job_json_private_parsers_cover_encryption_and_writer_choices() {
for value in ["preserve", "disable", "generate"] {
assert!(parse_object_stream_mode(value).is_ok());
}
assert!(parse_object_stream_mode("other").is_err());
for value in ["none", "generalized", "specialized", "all"] {
let level = parse_json_decode_level(value);
assert_eq!(json_decode_level_for_output(level).as_qpdf_str(), value);
}
assert_eq!(parse_json_version("1"), 1);
assert_eq!(parse_json_version("2"), 2);
assert_eq!(parse_json_version("latest"), 2);
assert_eq!(parse_json_version(""), 2);
assert!(parse_job_version(b"1.7.3", ".version").is_ok());
assert_eq!(
parse_job_version(b"invalid", ".version").unwrap(),
("invalid".to_string(), 0)
);
assert_eq!(QPDFJob::parse_collate("2").unwrap(), vec![2]);
assert_eq!(QPDFJob::parse_collate("0").unwrap(), vec![0]);
assert_eq!(QPDFJob::parse_collate("not-number").unwrap(), vec![0]);
for value in ["all", "annotate", "form", "assembly", "none"] {
let mut permissions = crate::PermissionsConfig::default();
job_json_modify_permission(value, &mut permissions).unwrap();
}
assert!(
job_json_modify_permission("invalid", &mut crate::PermissionsConfig::default())
.is_err()
);
for value in ["full", "low", "none"] {
let mut permissions = crate::PermissionsConfig::default();
job_json_print_permission(value, &mut permissions).unwrap();
}
assert!(
job_json_print_permission("invalid", &mut crate::PermissionsConfig::default()).is_err()
);
let encrypt_40 = crate::json::Json::parse(
br#"{"userPassword":"u","ownerPassword":"o","40bit":{"annotate":"y","extract":"n","modify":"none","print":"low"}}"#,
)
.unwrap();
assert!(parse_job_encrypt(&encrypt_40, true).is_ok());
let encrypt_128 = crate::json::Json::parse(
br#"{"userPassword":"u","ownerPassword":"o","128bit":{"accessibility":"y","annotate":"n","assemble":"y","cleartextMetadata":"","extract":"n","form":"y","modifyOther":"n","modify":"all","print":"full","forceV4":"","useAes":"n"}}"#,
)
.unwrap();
assert!(parse_job_encrypt(&encrypt_128, true).is_ok());
let encrypt_256 = crate::json::Json::parse(
br#"{"userPassword":"u","ownerPassword":"o","256bit":{"forceR5":"","allowInsecure":""}}"#,
)
.unwrap();
assert!(parse_job_encrypt(&encrypt_256, true).is_ok());
let encrypt_128_rc4 =
crate::json::Json::parse(br#"{"userPassword":"u","ownerPassword":"o","128bit":{}}"#)
.unwrap();
assert!(parse_job_encrypt(&encrypt_128_rc4, true).is_ok());
let encrypt_256_r6 =
crate::json::Json::parse(br#"{"userPassword":"u","ownerPassword":"o","256bit":{}}"#)
.unwrap();
assert!(parse_job_encrypt(&encrypt_256_r6, true).is_ok());
let insecure_256 =
crate::json::Json::parse(br#"{"userPassword":"u","ownerPassword":"","256bit":{}}"#)
.unwrap();
assert!(parse_job_encrypt(&insecure_256, true).is_err());
let allowed_insecure_256 = crate::json::Json::parse(
br#"{"userPassword":"u","ownerPassword":"","256bit":{"allowInsecure":""}}"#,
)
.unwrap();
assert!(parse_job_encrypt(&allowed_insecure_256, true).is_ok());
let missing_password = crate::json::Json::parse(br#"{"128bit":{}}"#).unwrap();
assert!(parse_job_encrypt(&missing_password, true).is_err());
let duplicate_key_length = crate::json::Json::parse(
br#"{"userPassword":"u","ownerPassword":"o","40bit":{},"128bit":{}}"#,
)
.unwrap();
assert!(parse_job_encrypt(&duplicate_key_length, true).is_err());
let no_key_length =
crate::json::Json::parse(br#"{"userPassword":"u","ownerPassword":"o"}"#).unwrap();
assert!(parse_job_encrypt(&no_key_length, true).is_err());
assert!(parse_job_encrypt(&encrypt_40, false).is_err());
}
#[test]
fn job_json_split_pages_rejects_an_i32_overflow_at_the_qpdf_boundary() {
let error = parse_job_split_pages(b"2147483648").expect_err("i32 overflow must fail");
assert_eq!(
error.to_string(),
".splitPages: invalid page count 2147483648"
);
}
#[test]
fn job_json_implicit_stdout_rejects_split_pages_before_writing() {
let mut job = QPDFJob::new();
job.initialize_from_json_partial(
r#"{"inputFile":"input.pdf","json":"2","splitPages":"1"}"#,
)
.unwrap();
let error = job
.check_configuration()
.expect_err("implicit JSON stdout must be visible to split validation");
assert_eq!(
error.to_string(),
"--split-pages may not be used when writing to standard output"
);
}
#[test]
fn job_json_compression_level_uses_qpdf_integer_prefix_conversion() {
assert_eq!(parse_job_compression_level(b" +9tail").unwrap(), 9);
assert_eq!(parse_job_compression_level(b"not-a-number").unwrap(), 0);
assert!(parse_job_compression_level(b"99999999999999999999").is_err());
}
#[test]
fn job_json_show_object_selector_preserves_qpdf_forms() {
assert_eq!(
parse_job_object_selector(b"trailer").unwrap(),
JobObjectSelector::Trailer
);
assert_eq!(
parse_job_object_selector(b"1").unwrap(),
JobObjectSelector::Object(ObjectRef::new(1, 0))
);
assert_eq!(
parse_job_object_selector(b"1,").unwrap(),
JobObjectSelector::Object(ObjectRef::new(1, 0))
);
assert_eq!(
parse_job_object_selector(b"-1").unwrap(),
JobObjectSelector::NoObject
);
assert_eq!(
parse_job_object_selector(b"").unwrap(),
JobObjectSelector::NoObject
);
assert_eq!(
parse_job_object_selector(b"1,65536").unwrap(),
JobObjectSelector::Null
);
assert!(parse_job_object_selector(b"999999999999999999999").is_err());
assert!(parse_job_object_selector(b"99999999999999999999999999999999999999999").is_err());
assert!(parse_job_object_selector(b"2147483648").is_err());
}
#[test]
fn job_collate_parser_matches_qpdf_parameter_semantics() {
assert_eq!(QPDFJob::parse_collate("").unwrap(), vec![1]);
assert_eq!(QPDFJob::parse_collate("2,3").unwrap(), vec![2, 3]);
assert_eq!(QPDFJob::parse_collate("2,,3").unwrap(), vec![2, 0, 3]);
assert_eq!(QPDFJob::parse_collate("0").unwrap(), vec![0]);
assert_eq!(QPDFJob::parse_collate("2abc").unwrap(), vec![2]);
assert_eq!(QPDFJob::parse_collate("abc").unwrap(), vec![0]);
assert_eq!(QPDFJob::parse_collate(" +2").unwrap(), vec![2]);
let leading_comma = QPDFJob::parse_collate(",2").unwrap_err();
assert!(leading_comma.to_string().contains("trailing comma"));
let trailing_comma = QPDFJob::parse_collate("2,").unwrap_err();
assert!(trailing_comma.to_string().contains("trailing comma"));
let underflow = QPDFJob::parse_collate("-1").unwrap_err();
assert!(underflow.to_string().contains("underflow converting -1"));
let overflow = QPDFJob::parse_collate("18446744073709551616").unwrap_err();
assert!(overflow
.to_string()
.contains("overflow converting 18446744073709551616"));
let narrowing = QPDFJob::parse_collate("4294967296").unwrap_err();
assert!(narrowing
.to_string()
.contains("integer out of range converting 4294967296"));
}
#[test]
fn job_json_private_parser_covers_full_handler_dispatch() {
let tempdir = tempfile::tempdir().unwrap();
let password_file = tempdir.path().join("password.txt");
std::fs::write(&password_file, b"file-password\n").unwrap();
let nested_job_file = tempdir.path().join("nested.json");
std::fs::write(&nested_job_file, b"{}").unwrap();
let mut root = serde_json::Map::new();
for (key, value) in [
("inputFile", "input.pdf"),
("outputFile", "output.pdf"),
("password", "password"),
("jsonInput", ""),
("qdf", ""),
("preserveUnreferenced", ""),
("newlineBeforeEndstream", ""),
("normalizeContent", "y"),
("streamData", "compress"),
("compressStreams", "y"),
("recompressFlate", ""),
("decodeLevel", "all"),
("decrypt", ""),
("deterministicId", ""),
("staticAesIv", ""),
("staticId", ""),
("noOriginalObjectIds", ""),
("copyEncryption", "donor.pdf"),
("encryptionFilePassword", "donor-password"),
("allowWeakCrypto", ""),
("progress", ""),
("verbose", ""),
("objectStreams", "generate"),
("minVersion", "1.4"),
("forceVersion", "1.7"),
("linearizePass1", "pass1.pdf"),
("linearize", ""),
("updateFromJson", "update.json"),
("collate", "2"),
("flattenAnnotations", "all"),
("checkLinearization", ""),
("jsonOutput", "latest"),
("externalizeInlineImages", ""),
("iiMinBytes", "100"),
("keepInlineImages", ""),
("optimizeImages", ""),
("jsonStreamPrefix", "stream"),
("jsonStreamData", "file"),
("testJsonSchema", ""),
("showEncryptionKey", ""),
("noWarn", ""),
("warningExit0", ""),
("check", ""),
("showEncryption", ""),
("removePageLabels", ""),
("preserveUnreferencedResources", ""),
("oiMinArea", "100"),
("oiMinHeight", "100"),
("oiMinWidth", "100"),
("ignoreXrefStreams", ""),
("passwordIsHexKey", ""),
("passwordMode", "auto"),
("suppressPasswordRecovery", ""),
("suppressRecovery", ""),
("compressionLevel", "1"),
("reportMemoryUsage", ""),
("isEncrypted", ""),
("requiresPassword", ""),
("filteredStreamData", ""),
("rawStreamData", ""),
("showXref", ""),
("showLinearization", ""),
("showObject", "trailer"),
("listAttachments", ""),
("showAttachment", "attachment.txt"),
("jobJsonFile", "nested.json"),
] {
root.insert(key.to_owned(), serde_json::json!(value));
}
root.insert(
"passwordFile".to_owned(),
serde_json::json!(password_file.to_string_lossy()),
);
root.insert("jsonKey".to_owned(), serde_json::json!(["qpdf", "pages"]));
root.insert(
"jsonObject".to_owned(),
serde_json::json!(["trailer", "1 0 R"]),
);
root.insert(
"encrypt".to_owned(),
serde_json::json!({
"userPassword": "u",
"ownerPassword": "o",
"128bit": {"useAes": "y"}
}),
);
root.insert(
"jobJsonFile".to_owned(),
serde_json::json!(nested_job_file.to_string_lossy()),
);
root.insert(
"pages".to_owned(),
serde_json::json!([{"file": "page.pdf", "password": "page-password", "range": "1"}]),
);
root.insert(
"overlay".to_owned(),
serde_json::json!({"file": "overlay.pdf", "from": "1", "to": "1", "repeat": "1"}),
);
root.insert(
"underlay".to_owned(),
serde_json::json!([{"file": "underlay.pdf"}]),
);
root.insert(
"addAttachment".to_owned(),
serde_json::json!([{
"file": "attachment.bin", "filename": "shown.bin", "key": "shown-key",
"mimetype": "application/octet-stream", "description": "description",
"creationdate": "D:20220131134246-05'00'", "moddate": "D:20220131134246-05'00'",
"replace": ""
}]),
);
root.insert(
"copyAttachmentsFrom".to_owned(),
serde_json::json!([{"file": "copy.pdf", "password": "copy-password", "prefix": "p-"}]),
);
root.insert(
"removeAttachment".to_owned(),
serde_json::json!(["old-key"]),
);
root.insert(
"setPageLabels".to_owned(),
serde_json::json!(["1:D", "2:a/2/prefix"]),
);
let json = serde_json::Value::Object(root).to_string();
let mut job = QPDFJob::new();
job.initialize_from_json_partial(&json)
.expect("all parsed qpdf job handlers should accept their valid shapes");
let mut latest = QPDFJob::new();
latest
.initialize_from_json_partial(r#"{"json":""}"#)
.unwrap();
let unknown = crate::json::Json::parse(br#"{"potato":""}"#).unwrap();
let error = validate_job_json_schema(&unknown).unwrap_err();
assert!(error.to_string().contains("qpdf: job json has errors:"));
}
#[test]
fn job_json_page_label_parser_covers_styles_and_failures() {
let parse = |specs: &[&str]| {
specs
.iter()
.map(|spec| parse_page_label_spec(spec.as_bytes()).unwrap())
.collect::<Vec<_>>()
};
let entries =
parse_job_page_labels(&parse(&["1:D", "2:a", "3:A", "4:r", "5:R", "6:"]), 6).unwrap();
assert_eq!(entries.len(), 6);
assert!(parse_page_label_spec(b"bad").is_err());
assert!(parse_page_label_spec(b"q:D").is_err());
assert!(parse_job_page_labels(&parse(&["2:D"]), 6).is_err());
assert!(parse_job_page_labels(&parse(&["1:D", "1:a"]), 6).is_err());
assert!(parse_job_page_labels(&parse(&["7:D"]), 6).is_err());
assert!(parse_page_label_spec(b"1:X").is_err());
assert!(parse_page_label_spec(b"1:D/foo").is_err());
assert!(parse_page_label_spec(b"1:D/0").is_err());
assert!(parse_page_label_spec(b"rx:D").is_err());
let relative =
parse_job_page_labels(&parse(&["1:D", "r2:a/2/prefix", "z:R//end"]), 6).unwrap();
assert_eq!(relative[1].0, 4);
assert_eq!(relative[2].0, 5);
}
#[test]
fn job_json_private_parser_covers_remaining_choices_and_validation_errors() {
for stream_data in ["compress", "preserve", "uncompress"] {
let mut job = QPDFJob::new();
job.initialize_from_json_partial(&format!(r#"{{"streamData":"{stream_data}"}}"#))
.unwrap();
}
for stream_data in ["none", "inline", "file"] {
let mut job = QPDFJob::new();
job.initialize_from_json_partial(&format!(r#"{{"jsonStreamData":"{stream_data}"}}"#))
.unwrap();
}
for resources in ["auto", "yes", "no"] {
let mut job = QPDFJob::new();
job.initialize_from_json_partial(&format!(
r#"{{"removeUnreferencedResources":"{resources}"}}"#
))
.unwrap();
}
let mut job = QPDFJob::new();
job.initialize_from_json_partial(r#"{"passwordMode":"invalid"}"#)
.expect_err("passwordMode choices must be known");
let mut job = QPDFJob::new();
job.initialize_from_json_partial(r#"{"addAttachment":[{"file":"/"}]}"#)
.expect_err("a root path has no attachment basename");
let mut job = QPDFJob::new();
job.initialize_from_json_partial(r#"{"overlay":[{}]}"#)
.expect_err("overlay file is required");
let mut job = QPDFJob::new();
job.initialize_from_json_partial(r#"{"overlay":{"file":"overlay.pdf"}}"#)
.unwrap();
let mut job = QPDFJob::new();
job.initialize_from_json_partial(r#"{"pages":[{}]}"#)
.expect_err("page file is required");
let mut job = QPDFJob::new();
job.initialize_from_json_partial(r#"{"pages":{"file":"page.pdf","range":"1-2"}}"#)
.unwrap();
let mut job = QPDFJob::new();
job.initialize_from_json_partial(r#"{"pages":[]}"#)
.expect_err("an empty pages array must finish with qpdf's no-specification error");
let mut job = QPDFJob::new();
job.initialize_from_json_partial(r#"{"jsonKey":[1]}"#)
.expect_err("jsonKey entries must be strings");
let mut job = QPDFJob::new();
job.initialize_from_json_partial(r#"{"jsonKey":["unknown"]}"#)
.expect_err("jsonKey choices must be known");
let mut job = QPDFJob::new();
job.initialize_from_json_partial(r#"{"jsonObject":[1]}"#)
.expect_err("jsonObject entries must be strings");
let mut job = QPDFJob::new();
job.initialize_from_json_partial(r#"{"jsonObject":["2147483648"]}"#)
.expect("jsonObject selector parsing is deferred until JSON output");
let mut job = QPDFJob::new();
job.initialize_from_json_partial(r#"{"removeAttachment":[1]}"#)
.expect_err("attachment names must be strings");
let mut job = QPDFJob::new();
job.initialize_from_json_partial(r#"{"setPageLabels":[1]}"#)
.expect_err("page labels must be strings");
let mut job = QPDFJob::new();
job.initialize_from_json_partial(r#"{"inputFile":"input.pdf","empty":""}"#)
.expect_err("empty and inputFile are mutually exclusive");
}
#[test]
fn job_json_file_rejects_recursive_includes() {
let tempdir = tempfile::tempdir().unwrap();
let first = tempdir.path().join("first.json");
let second = tempdir.path().join("second.json");
std::fs::write(
&first,
serde_json::json!({"jobJsonFile": second.display().to_string()}).to_string(),
)
.unwrap();
std::fs::write(
&second,
serde_json::json!({"jobJsonFile": first.display().to_string()}).to_string(),
)
.unwrap();
let json = serde_json::json!({"jobJsonFile": first.display().to_string()}).to_string();
let mut job = QPDFJob::new();
let error = job
.initialize_from_json_partial(&json)
.expect_err("recursive job JSON includes must be bounded");
assert!(error
.to_string()
.contains("recursive jobJsonFile reference"));
let mut non_dictionary_job = QPDFJob::new();
assert!(non_dictionary_job
.initialize_from_json_partial("[]")
.is_err());
let scalar_file = tempdir.path().join("scalar.json");
std::fs::write(&scalar_file, b"[]").unwrap();
let mut scalar_job = QPDFJob::new();
assert!(scalar_job
.initialize_from_json_partial(
&serde_json::json!({
"jobJsonFile": scalar_file.display().to_string()
})
.to_string()
)
.is_err());
}
#[test]
fn job_json_nested_dispatch_keeps_qpdf_shared_state_and_key_order() {
let tempdir = tempfile::tempdir().unwrap();
let nested = tempdir.path().join("nested.json");
std::fs::write(
&nested,
serde_json::json!({
"collate": "2,3",
"jsonStreamData": "file",
"rotate": "90:1"
})
.to_string(),
)
.unwrap();
let json = serde_json::json!({
"collate": "4",
"jobJsonFile": nested.display().to_string(),
"jsonOutput": "2",
"rotate": "180:1"
})
.to_string();
let mut job = QPDFJob::new();
job.initialize_from_json_partial(&json).unwrap();
assert_eq!(job.configuration.collate, Some(vec![4, 2, 3]));
assert_eq!(job.configuration.json_stream_data, JsonStreamData::File);
assert!(job.configuration.json_stream_data_set);
assert_eq!(job.configuration.rotations.len(), 1);
assert_eq!(job.configuration.rotations[b"1".as_slice()].angle, 180);
}
#[test]
fn job_json_rotation_keeps_qpdf_raw_range_and_relative_state() {
let mut job = QPDFJob::new();
job.initialize_from_json_partial(&serde_json::json!({"rotate": "+90:1-5,x3"}).to_string())
.unwrap();
let rotation = &job.configuration.rotations[b"1-5,x3".as_slice()];
assert_eq!(rotation.angle, 90);
assert!(rotation.relative);
assert_eq!(
crate::qutil::parse_numrange(b"1-5,x3", 5).unwrap(),
vec![1, 2, 4, 5]
);
}
#[test]
fn job_json_rotation_applies_qpdf_c_string_boundary_before_parsing() {
let mut job = QPDFJob::new();
job.initialize_from_json_partial(
&serde_json::json!({"rotate": "90:1\u{0000}junk"}).to_string(),
)
.unwrap();
assert!(job.configuration.rotations.contains_key(b"1".as_slice()));
assert!(!job
.configuration
.rotations
.contains_key(b"1\0junk".as_slice()));
}
#[test]
fn config_rotation_and_split_pages_store_qpdf_values() {
let mut job = QPDFJob::new();
{
let mut configuration = job.config();
configuration
.rotate(b"+90:1")
.expect("qpdf rotation parameter should parse");
configuration
.split_pages(b"2")
.expect("qpdf split-pages parameter should parse");
}
let rotation = &job.configuration.rotations[b"1".as_slice()];
assert_eq!(rotation.angle, 90);
assert!(rotation.relative);
assert_eq!(job.configuration.split_pages, Some(2));
}
#[test]
fn config_rotation_replaces_duplicate_ranges_in_lexical_order() {
let mut job = QPDFJob::new();
{
let mut configuration = job.config();
configuration
.rotate(b"90:1-3")
.expect("first rotation parameter should parse");
configuration
.rotate(b"+90:2")
.expect("relative rotation parameter should parse");
configuration
.rotate(b"90:1-3")
.expect("duplicate rotation parameter should parse");
}
let keys: Vec<&[u8]> = job
.configuration
.rotations
.keys()
.map(Vec::as_slice)
.collect();
assert_eq!(keys, [b"1-3".as_slice(), b"2".as_slice()]);
assert_eq!(job.configuration.rotations[b"1-3".as_slice()].angle, 90);
assert!(!job.configuration.rotations[b"1-3".as_slice()].relative);
assert_eq!(job.configuration.rotations[b"2".as_slice()].angle, 90);
assert!(job.configuration.rotations[b"2".as_slice()].relative);
}
#[test]
fn job_json_nested_dispatch_appends_attachment_operations() {
let tempdir = tempfile::tempdir().unwrap();
let nested = tempdir.path().join("nested.json");
std::fs::write(
&nested,
serde_json::json!({
"copyAttachmentsFrom": [{"file": "inner.pdf", "prefix": "inner-"}],
"removeAttachment": ["inner-key"]
})
.to_string(),
)
.unwrap();
let json = serde_json::json!({
"copyAttachmentsFrom": [{"file": "outer.pdf", "prefix": "outer-"}],
"jobJsonFile": nested.display().to_string(),
"removeAttachment": ["outer-key"]
})
.to_string();
let mut job = QPDFJob::new();
job.initialize_from_json_partial(&json).unwrap();
assert_eq!(
job.configuration
.attachments_to_copy
.iter()
.map(|entry| entry.path.as_path())
.collect::<Vec<_>>(),
[Path::new("outer.pdf"), Path::new("inner.pdf")]
);
assert_eq!(
job.configuration.attachments_to_remove,
[b"inner-key".to_vec(), b"outer-key".to_vec()]
);
}
#[test]
fn job_json_nested_dispatch_rejects_duplicate_output_files() {
let tempdir = tempfile::tempdir().unwrap();
let nested = tempdir.path().join("nested.json");
std::fs::write(
&nested,
serde_json::json!({"outputFile": "inner.pdf"}).to_string(),
)
.unwrap();
let json = serde_json::json!({
"jobJsonFile": nested.display().to_string(),
"outputFile": "outer.pdf"
})
.to_string();
let mut job = QPDFJob::new();
let error = job
.initialize_from_json_partial(&json)
.expect_err("qpdf accepts only one output file");
assert!(matches!(
error,
Error::Usage(usage) if usage.to_string() == "output file has already been given"
));
}
#[test]
fn job_json_encryption_status_and_copy_errors_use_job_boundaries() {
let fixture_root =
std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../tests/fixtures");
let encrypted = fixture_root.join("encrypted/v4-aes-128-r4.pdf");
let plaintext = fixture_root.join("minimal.pdf");
let cases = [
(
serde_json::json!({
"inputFile": encrypted.display().to_string(),
"isEncrypted": ""
})
.to_string(),
JobExitCode::Success,
),
(
serde_json::json!({
"inputFile": plaintext.display().to_string(),
"requiresPassword": ""
})
.to_string(),
JobExitCode::Error,
),
];
for (json, expected) in cases {
let mut job = QPDFJob::new();
let logger = QPDFLogger::create();
logger.set_info(Some(logger.discard()));
logger.set_warn(Some(logger.discard()));
logger.set_error(Some(logger.discard()));
job.set_logger(logger);
job.initialize_from_json_partial(&json).unwrap();
assert_eq!(job.run().unwrap(), expected);
}
let tempdir = tempfile::tempdir().unwrap();
let missing = tempdir.path().join("missing.pdf");
let mut missing_job = QPDFJob::new();
let logger = QPDFLogger::create();
logger.set_info(Some(logger.discard()));
logger.set_warn(Some(logger.discard()));
logger.set_error(Some(logger.discard()));
missing_job.set_logger(logger);
missing_job
.initialize_from_json_partial(
&serde_json::json!({
"inputFile": missing.display().to_string(),
"isEncrypted": ""
})
.to_string(),
)
.unwrap();
assert_eq!(missing_job.run().unwrap(), JobExitCode::Error);
let malformed = tempdir.path().join("malformed.pdf");
std::fs::write(&malformed, b"not a PDF").unwrap();
let mut malformed_job = QPDFJob::new();
let logger = QPDFLogger::create();
logger.set_info(Some(logger.discard()));
logger.set_warn(Some(logger.discard()));
logger.set_error(Some(logger.discard()));
malformed_job.set_logger(logger);
malformed_job
.initialize_from_json_partial(
&serde_json::json!({
"inputFile": malformed.display().to_string(),
"requiresPassword": ""
})
.to_string(),
)
.unwrap();
assert_eq!(malformed_job.run().unwrap(), JobExitCode::Error);
let mut copy_job = QPDFJob::new();
let logger = QPDFLogger::create();
logger.set_info(Some(logger.discard()));
logger.set_warn(Some(logger.discard()));
logger.set_error(Some(logger.discard()));
copy_job.set_logger(logger);
copy_job
.initialize_from_json_partial(
&serde_json::json!({
"inputFile": plaintext.display().to_string(),
"outputFile": tempdir.path().join("output.pdf").display().to_string(),
"copyEncryption": plaintext.display().to_string()
})
.to_string(),
)
.unwrap();
assert_eq!(copy_job.run().unwrap(), JobExitCode::Success);
}
#[test]
fn job_json_status_rejects_combined_encryption_queries() {
let input = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../tests/fixtures/minimal.pdf");
let mut job = QPDFJob::new();
job.initialize_from_json_partial(
&serde_json::json!({
"inputFile": input.display().to_string(),
"isEncrypted": "",
"requiresPassword": ""
})
.to_string(),
)
.unwrap();
let error = job
.run()
.expect_err("status queries are mutually exclusive");
assert!(error
.to_string()
.contains("--requires-password and --is-encrypted may not be given together"));
}
#[test]
fn job_json_show_linearization_reports_soft_warnings() {
let mut bytes = std::fs::read(
std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../tests/fixtures/compat/linearized-one-page.pdf"),
)
.unwrap();
let offset = bytes
.windows(3)
.position(|window| window == b"/N ")
.expect("linearized fixture has /N");
bytes[offset + 3] = b'Z';
let tempdir = tempfile::tempdir().unwrap();
let input = tempdir.path().join("linearized.pdf");
std::fs::write(&input, bytes).unwrap();
let mut job = QPDFJob::new();
let logger = QPDFLogger::create();
logger.set_info(Some(logger.discard()));
logger.set_warn(Some(logger.discard()));
logger.set_error(Some(logger.discard()));
job.set_logger(logger);
job.initialize_from_json_partial(
&serde_json::json!({
"inputFile": input.display().to_string(),
"showLinearization": ""
})
.to_string(),
)
.unwrap();
assert_eq!(job.run().unwrap(), JobExitCode::Warning);
}
#[test]
fn job_json_inspection_dispatch_covers_object_and_report_variants() {
let input = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../tests/fixtures/minimal.pdf");
for selector in ["trailer", "1", "-1", "1,65536"] {
let mut job = QPDFJob::new();
let logger = QPDFLogger::create();
logger.set_info(Some(logger.discard()));
logger.set_warn(Some(logger.discard()));
logger.set_error(Some(logger.discard()));
job.set_logger(logger);
job.initialize_from_json_partial(
&serde_json::json!({
"inputFile": input.display().to_string(),
"showObject": selector
})
.to_string(),
)
.unwrap();
assert_eq!(job.run().unwrap(), JobExitCode::Success);
}
let mut job = QPDFJob::new();
let logger = QPDFLogger::create();
logger.set_info(Some(logger.discard()));
logger.set_warn(Some(logger.discard()));
logger.set_error(Some(logger.discard()));
job.set_logger(logger);
job.initialize_from_json_partial(
&serde_json::json!({
"inputFile": input.display().to_string(),
"showNpages": "",
"showPages": "",
"showLinearization": "",
"showXref": "",
"listAttachments": ""
})
.to_string(),
)
.unwrap();
assert_eq!(job.run().unwrap(), JobExitCode::Success);
}
#[test]
fn job_input_setters_preserve_qpdf_configuration_boundaries() {
let mut job = QPDFJob::new();
job.set_input_file("input.pdf").unwrap();
assert_eq!(job.input_name(), "input.pdf");
assert!(job.set_input_file("second.pdf").is_err());
job.set_output_file("output.pdf").unwrap();
assert!(job.set_output_file("second-output.pdf").is_err());
job.set_password(b"password".to_vec());
let mut empty_job = QPDFJob::new();
empty_job
.initialize_from_json_partial(r#"{"empty":""}"#)
.unwrap();
assert!(empty_job.set_input_file("input.pdf").is_err());
let mut replace_job = QPDFJob::new();
replace_job
.initialize_from_json_partial(r#"{"replaceInput":""}"#)
.unwrap();
assert!(replace_job.set_output_file("output.pdf").is_err());
}
#[cfg(unix)]
#[test]
fn attachment_default_names_preserve_non_utf8_basename_bytes() {
let mut json = b"{\"file\":\"attachment-".to_vec();
json.push(0x80);
json.extend_from_slice(b".bin\"}");
let value = crate::json::Json::parse(&json).unwrap();
let options = parse_job_attachment(&value, ".addAttachment[0]").unwrap();
assert_eq!(options.key, b"attachment-\x80.bin");
assert_eq!(options.filename, b"attachment-\x80.bin");
}
#[cfg(target_os = "linux")]
#[test]
fn job_json_path_fields_preserve_non_utf8_bytes_in_configuration() {
use std::ffi::OsString;
use std::os::unix::ffi::{OsStrExt, OsStringExt};
use std::path::PathBuf;
fn non_utf8_path(directory: &std::path::Path, name: &[u8]) -> PathBuf {
let mut bytes = directory.as_os_str().as_bytes().to_vec();
bytes.push(b'/');
bytes.extend_from_slice(name);
PathBuf::from(OsString::from_vec(bytes))
}
fn append_member(json: &mut Vec<u8>, first: &mut bool, key: &[u8], value: &[u8]) {
if !*first {
json.push(b',');
}
*first = false;
json.push(b'"');
json.extend_from_slice(key);
json.extend_from_slice(b"\":\"");
json.extend_from_slice(value);
json.push(b'"');
}
fn append_file_object(
json: &mut Vec<u8>,
first: &mut bool,
key: &[u8],
path: &[u8],
suffix: &[u8],
) {
if !*first {
json.push(b',');
}
*first = false;
json.push(b'"');
json.extend_from_slice(key);
json.extend_from_slice(b"\":{\"file\":\"");
json.extend_from_slice(path);
json.extend_from_slice(b"\"");
json.extend_from_slice(suffix);
}
let directory = tempfile::tempdir().unwrap();
let input = non_utf8_path(directory.path(), b"input-\x80.pdf");
let output = non_utf8_path(directory.path(), b"output-\x80.pdf");
let password_file = non_utf8_path(directory.path(), b"password-\x80.txt");
let linearize_pass1 = non_utf8_path(directory.path(), b"pass1-\x80.tmp");
let update = non_utf8_path(directory.path(), b"update-\x80.json");
let nested = non_utf8_path(directory.path(), b"nested-\x80.json");
std::fs::write(&password_file, b"password\nignored\n").unwrap();
std::fs::write(&nested, b"{}").unwrap();
let input_bytes = input.as_os_str().as_bytes();
let output_bytes = output.as_os_str().as_bytes();
let password_file_bytes = password_file.as_os_str().as_bytes();
let linearize_pass1_bytes = linearize_pass1.as_os_str().as_bytes();
let update_bytes = update.as_os_str().as_bytes();
let nested_bytes = nested.as_os_str().as_bytes();
let mut json = b"{".to_vec();
let mut first = true;
append_member(&mut json, &mut first, b"inputFile", input_bytes);
append_member(&mut json, &mut first, b"outputFile", output_bytes);
append_member(&mut json, &mut first, b"copyEncryption", input_bytes);
append_member(&mut json, &mut first, b"passwordFile", password_file_bytes);
append_member(
&mut json,
&mut first,
b"linearizePass1",
linearize_pass1_bytes,
);
append_member(&mut json, &mut first, b"updateFromJson", update_bytes);
append_file_object(
&mut json,
&mut first,
b"pages",
input_bytes,
b",\"range\":\"1\"}",
);
append_file_object(&mut json, &mut first, b"overlay", input_bytes, b"}");
append_file_object(&mut json, &mut first, b"addAttachment", input_bytes, b"}");
append_file_object(
&mut json,
&mut first,
b"copyAttachmentsFrom",
input_bytes,
b"}",
);
append_member(&mut json, &mut first, b"jobJsonFile", nested_bytes);
json.push(b'}');
let mut job = QPDFJob::new();
job.initialize_from_json_partial_bytes(&json).unwrap();
assert_eq!(job.configuration.input_file.as_ref(), Some(&input));
assert_eq!(job.configuration.output_file.as_ref(), Some(&output));
assert_eq!(job.configuration.copy_encryption.as_ref(), Some(&input));
assert_eq!(job.configuration.password, b"password");
assert_eq!(
job.configuration.linearize_pass1.as_ref(),
Some(&linearize_pass1)
);
assert_eq!(job.configuration.update_from_json.as_ref(), Some(&update));
assert_eq!(job.configuration.page_specs[0].path, input);
assert_eq!(job.configuration.overlays[0].path, input);
assert_eq!(job.configuration.attachments_to_add[0].path, input);
assert_eq!(job.configuration.attachments_to_copy[0].path, input);
}
}