use crate::object_handle::ObjectHandleIdentity;
use crate::pages::{
repair::{prepare_for_optimization_with_max_depth, PageTreeRoot},
resolve_inherited_handle_with_max_depth, DEFAULT_MAX_PAGE_TREE_DEPTH,
};
use crate::{Error, ObjectHandle, ObjectRef, Pdf, Result};
use std::collections::{BTreeMap, BTreeSet, HashSet};
use std::io::{Read, Seek};
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct RebuildResult {
pub new_kids: Vec<ObjectRef>,
pub ref_map: BTreeMap<ObjectRef, Vec<ObjectRef>>,
pub removed_pages: BTreeSet<ObjectRef>,
}
fn promote_inherited_value<R: Read + Seek>(
pdf: &mut Pdf<R>,
value: ObjectHandle,
) -> Result<ObjectHandle> {
value.try_dereference()?;
let non_scalar = value.as_array().is_some()
|| value.as_dictionary().is_some()
|| value.as_stream_dict().is_some();
if !value.is_direct() || !non_scalar {
return Ok(value);
}
if value.belongs_to_pdf(pdf.unique_id) {
pdf.mark_object_handle_dirty(&value)?;
}
let indirect = pdf.make_indirect_from_object_handle(value)?;
pdf.mark_object_handle_dirty(&indirect)?;
Ok(indirect)
}
fn install_inherited_value<R: Read + Seek>(
pdf: &mut Pdf<R>,
page: &ObjectHandle,
key: &[u8],
value: Option<&ObjectHandle>,
) -> Result<()> {
if page.try_has_key(key)? {
return Ok(());
}
let Some(value) = value else {
return Ok(());
};
page.replace_key(key, value.clone())?;
pdf.mark_object_handle_dirty(page)
}
fn resolve_inherited_for_page<R: Read + Seek>(
pdf: &mut Pdf<R>,
page_ref: ObjectRef,
page: &ObjectHandle,
key: &[u8],
max_depth: usize,
) -> Result<Option<ObjectHandle>> {
let value = resolve_inherited_handle_with_max_depth(pdf, page_ref, key, max_depth)?;
if page.try_has_key(key)? {
return Ok(None);
}
value
.map(|value| promote_inherited_value(pdf, value))
.transpose()
}
#[allow(
clippy::mutable_key_type,
reason = "ObjectHandleIdentity intentionally keys the canonical live allocation"
)]
fn collect_page_tree_nodes(
node: ObjectHandle,
nodes: &mut Vec<ObjectHandle>,
seen: &mut HashSet<ObjectHandleIdentity>,
depth: usize,
max_depth: usize,
) -> Result<()> {
if depth >= max_depth {
let location = node
.object_ref()
.map_or_else(|| "direct /Pages node".to_owned(), |r| r.to_string());
return Err(Error::Unsupported(format!(
"page tree depth exceeds maximum of {max_depth} at {location}"
)));
}
if !seen.insert(node.identity_key()) {
return Ok(());
}
node.try_dereference()?;
if !node.try_is_dictionary_of_type(b"Pages", b"")? {
return Ok(());
}
nodes.push(node.clone());
let kids = node.try_get_key(b"/Kids")?;
let Some(kid_count) = kids.try_array_len()? else {
return Ok(());
};
for index in 0..kid_count {
let Some(kid) = kids.try_array_item(index)? else {
continue; };
if kid.try_has_key(b"/Kids")? {
collect_page_tree_nodes(kid, nodes, seen, depth + 1, max_depth)?;
}
}
Ok(())
}
fn promote_page_tree_inheritable_values<R: Read + Seek>(
pdf: &mut Pdf<R>,
nodes: &[ObjectHandle],
) -> Result<()> {
let inheritable_keys = [
b"/CropBox".as_slice(),
b"/MediaBox".as_slice(),
b"/Resources".as_slice(),
b"/Rotate".as_slice(),
];
for node in nodes {
for key in inheritable_keys {
if !node.try_has_key(key)? {
continue;
}
let value = node.try_get_key(key)?;
promote_inherited_value(pdf, value)?;
}
}
Ok(())
}
fn page_tree_root_handle<R: Read + Seek>(
pdf: &mut Pdf<R>,
page_root: PageTreeRoot,
) -> Result<ObjectHandle> {
match page_root {
PageTreeRoot::Indirect(root_ref) => Ok(pdf.get_object_handle(root_ref)),
PageTreeRoot::Direct { catalog } => pdf.get_object_handle(catalog).try_get_key(b"/Pages"),
PageTreeRoot::DirectCatalog => pdf.root_handle()?.try_get_key(b"/Pages"),
}
}
fn remove_inheritable_keys_from_page_tree<R: Read + Seek>(
pdf: &mut Pdf<R>,
nodes: &[ObjectHandle],
) -> Result<()> {
let inheritable_keys = [
b"/CropBox".as_slice(),
b"/MediaBox".as_slice(),
b"/Resources".as_slice(),
b"/Rotate".as_slice(),
];
let structural_keys = [
b"/Type".as_slice(),
b"/Parent".as_slice(),
b"/Kids".as_slice(),
b"/Count".as_slice(),
];
for node in nodes {
let pages_object = node.object_ref().map_or_else(
|| "Pages object".to_owned(),
|object_ref| {
format!(
"Pages object: object {} {}",
object_ref.number, object_ref.generation
)
},
);
if node.try_has_key(b"/Parent")? {
for key in node.try_get_keys()? {
if !inheritable_keys.contains(&key.as_slice())
&& !structural_keys.contains(&key.as_slice())
{
pdf.push_warning(format!(
"({pages_object}): Unknown key {} in /Pages object is being discarded as a result of flattening the /Pages tree",
String::from_utf8_lossy(&key)
))?;
}
}
}
for key in inheritable_keys {
node.remove_key(key);
}
pdf.mark_object_handle_dirty(node)?;
}
Ok(())
}
pub fn rebuild_page_tree<R: Read + Seek>(
pdf: &mut Pdf<R>,
selected: &[ObjectRef],
) -> Result<RebuildResult> {
rebuild_page_tree_with_max_depth(pdf, selected, DEFAULT_MAX_PAGE_TREE_DEPTH)
}
#[allow(
clippy::mutable_key_type,
reason = "ObjectHandleIdentity intentionally keys the canonical live allocation"
)]
pub fn rebuild_page_tree_with_max_depth<R: Read + Seek>(
pdf: &mut Pdf<R>,
selected: &[ObjectRef],
max_depth: usize,
) -> Result<RebuildResult> {
let root_candidate = pdf.trailer_key_handle(b"Root");
if root_candidate.is_null() {
return Err(Error::Missing("/Root"));
}
pdf.root_handle()?;
let prepared =
prepare_for_optimization_with_max_depth(pdf, max_depth)?.ok_or(Error::Missing("/Pages"))?;
let page_root = prepared.root;
pdf.invalidate_page_list_cache();
let root = page_tree_root_handle(pdf, page_root)?;
let mut page_tree_nodes = Vec::new();
collect_page_tree_nodes(
root.clone(),
&mut page_tree_nodes,
&mut HashSet::new(),
0,
max_depth,
)?;
promote_page_tree_inheritable_values(pdf, &page_tree_nodes)?;
let original_pages = prepared.pages;
let mut new_kids: Vec<ObjectRef> = Vec::with_capacity(selected.len());
let mut ref_map: BTreeMap<ObjectRef, Vec<ObjectRef>> = BTreeMap::new();
let mut pending_leaves: Vec<ObjectHandle> = Vec::with_capacity(selected.len());
let mut materialized: BTreeSet<ObjectRef> = BTreeSet::new();
for &src in selected {
let page = pdf.get_object_handle(src);
page.try_dereference()?;
if !page.try_is_dictionary_of_type(b"Page", b"")? {
return Err(Error::Unsupported(format!(
"selected object {src} is not a /Page dictionary"
)));
}
let target = if materialized.insert(src) {
let inherited_cropbox =
resolve_inherited_for_page(pdf, src, &page, b"/CropBox", max_depth)?; let inherited_mediabox =
resolve_inherited_for_page(pdf, src, &page, b"/MediaBox", max_depth)?; let inherited_resources =
resolve_inherited_for_page(pdf, src, &page, b"/Resources", max_depth)?; let inherited_rotate =
resolve_inherited_handle_with_max_depth(pdf, src, b"/Rotate", max_depth)?;
install_inherited_value(pdf, &page, b"/CropBox", inherited_cropbox.as_ref())?;
install_inherited_value(pdf, &page, b"/MediaBox", inherited_mediabox.as_ref())?;
install_inherited_value(pdf, &page, b"/Resources", inherited_resources.as_ref())?;
install_inherited_value(pdf, &page, b"/Rotate", inherited_rotate.as_ref())?;
page.clone()
} else {
let copy = page.shallow_copy()?;
let promoted = pdf.make_indirect_from_object_handle(copy)?;
pdf.mark_object_handle_dirty(&promoted)?;
promoted
};
let target_ref = target
.object_ref()
.expect("prepared pages and duplicate promotion are indirect");
new_kids.push(target_ref);
ref_map.entry(src).or_default().push(target_ref);
pending_leaves.push(target);
}
if root.try_as_dictionary()?.is_none() {
return Err(Error::Unsupported(match page_root {
PageTreeRoot::Indirect(root_ref) => {
format!("document /Pages root {root_ref} is not a dictionary")
}
PageTreeRoot::Direct { .. } => {
"document catalog /Pages root is not a dictionary".into()
}
PageTreeRoot::DirectCatalog => {
"document catalog /Pages root is not a dictionary".into()
}
}));
} root.replace_key(b"/Type", ObjectHandle::name(b"Pages".to_vec()))?;
let kid_handles: Vec<_> = new_kids
.iter()
.map(|object_ref| pdf.get_object_handle(*object_ref))
.collect();
root.replace_key(b"/Kids", ObjectHandle::array(kid_handles))?;
let count = i64::try_from(new_kids.len()).unwrap_or(i64::MAX);
root.replace_key(b"/Count", ObjectHandle::integer(count))?;
remove_inheritable_keys_from_page_tree(pdf, &page_tree_nodes)?;
root.remove_key(b"/Parent");
pdf.mark_object_handle_dirty(&root)?;
for leaf in pending_leaves {
leaf.replace_key(b"/Parent", root.clone())?;
pdf.mark_object_handle_dirty(&leaf)?;
}
let removed_pages: BTreeSet<ObjectRef> = original_pages
.into_iter()
.filter(|p| !ref_map.contains_key(p))
.collect();
Ok(RebuildResult {
new_kids,
ref_map,
removed_pages,
})
}
#[cfg(test)]
mod tests {
use super::*;
use crate::job::check_bytes_for_test;
use crate::pages::page_refs;
use crate::pipeline::test_support::NthWriteFailure;
use crate::pipeline::PipelineHandle;
use crate::writer::write_qpdf_to_memory;
use crate::Pdf;
use std::io::Cursor;
use std::process::Command;
use std::rc::Rc;
fn build_nested_pdf() -> Vec<u8> {
let parts: Vec<(u32, String)> = vec![
(1, "<< /Type /Catalog /Pages 2 0 R >>".into()),
(2, "<< /Type /Pages /Kids [3 0 R] /Count 3 >>".into()),
(
3,
"<< /Type /Pages /Parent 2 0 R /Kids [4 0 R 5 0 R 6 0 R] /Count 3 \
/Rotate 90 /MediaBox [0 0 200 300] /Resources 9 0 R >>"
.into(),
),
(4, "<< /Type /Page /Parent 3 0 R /Contents 7 0 R >>".into()),
(
5,
"<< /Type /Page /Parent 3 0 R /Contents 8 0 R /MediaBox [0 0 400 500] >>".into(),
),
(6, "<< /Type /Page /Parent 3 0 R >>".into()),
];
let c1 = b"BT /F1 12 Tf 10 10 Td (Page1) Tj ET";
let c2 = b"BT /F1 12 Tf 10 10 Td (Page2) Tj ET";
let mut out: Vec<u8> = b"%PDF-1.5\n".to_vec();
let mut offs: BTreeMap<u32, u64> = BTreeMap::new();
for (n, s) in &parts {
offs.insert(*n, out.len() as u64);
out.extend_from_slice(format!("{n} 0 obj\n{s}\nendobj\n").as_bytes());
}
offs.insert(7, out.len() as u64);
out.extend_from_slice(format!("7 0 obj\n<< /Length {} >>\nstream\n", c1.len()).as_bytes());
out.extend_from_slice(c1);
out.extend_from_slice(b"\nendstream\nendobj\n");
offs.insert(8, out.len() as u64);
out.extend_from_slice(format!("8 0 obj\n<< /Length {} >>\nstream\n", c2.len()).as_bytes());
out.extend_from_slice(c2);
out.extend_from_slice(b"\nendstream\nendobj\n");
offs.insert(9, out.len() as u64);
out.extend_from_slice(
b"9 0 obj\n<< /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Helvetica >> >> >>\nendobj\n",
);
let xref_start = out.len() as u64;
let total = 10u32;
out.extend_from_slice(format!("xref\n0 {total}\n0000000000 65535 f \n").as_bytes());
for i in 1..total {
out.extend_from_slice(format!("{:010} 00000 n \n", offs[&i]).as_bytes());
}
out.extend_from_slice(
format!("trailer\n<< /Size {total} /Root 1 0 R >>\nstartxref\n{xref_start}\n%%EOF\n")
.as_bytes(),
);
out
}
fn build_deselected_branch_pdf() -> Vec<u8> {
let parts: Vec<(u32, String)> = vec![
(1, "<< /Type /Catalog /Pages 2 0 R >>".into()),
(2, "<< /Type /Pages /Kids [3 0 R 4 0 R] /Count 2 >>".into()),
(
3,
"<< /Type /Pages /Parent 2 0 R /Kids [5 0 R] /Count 1 \
/MediaBox [0 0 200 300] >>"
.into(),
),
(
4,
"<< /Type /Pages /Parent 2 0 R /Kids [6 0 R] /Count 1 /Rotate 180 >>".into(),
),
(5, "<< /Type /Page /Parent 3 0 R /Contents 7 0 R >>".into()),
(
6,
"<< /Type /Page /Parent 4 0 R /Contents 8 0 R /MediaBox [0 0 612 792] >>".into(),
),
];
let c1 = b"BT /F1 12 Tf 10 10 Td (Page1) Tj ET";
let c2 = b"BT /F1 12 Tf 10 10 Td (Page2) Tj ET";
let mut out: Vec<u8> = b"%PDF-1.5\n".to_vec();
let mut offs: BTreeMap<u32, u64> = BTreeMap::new();
for (n, s) in &parts {
offs.insert(*n, out.len() as u64);
out.extend_from_slice(format!("{n} 0 obj\n{s}\nendobj\n").as_bytes());
}
offs.insert(7, out.len() as u64);
out.extend_from_slice(format!("7 0 obj\n<< /Length {} >>\nstream\n", c1.len()).as_bytes());
out.extend_from_slice(c1);
out.extend_from_slice(b"\nendstream\nendobj\n");
offs.insert(8, out.len() as u64);
out.extend_from_slice(format!("8 0 obj\n<< /Length {} >>\nstream\n", c2.len()).as_bytes());
out.extend_from_slice(c2);
out.extend_from_slice(b"\nendstream\nendobj\n");
let xref_start = out.len() as u64;
let total = 9u32;
out.extend_from_slice(format!("xref\n0 {total}\n0000000000 65535 f \n").as_bytes());
for i in 1..total {
out.extend_from_slice(format!("{:010} 00000 n \n", offs[&i]).as_bytes());
}
out.extend_from_slice(
format!("trailer\n<< /Size {total} /Root 1 0 R >>\nstartxref\n{xref_start}\n%%EOF\n")
.as_bytes(),
);
out
}
fn build_direct_leaf_pdf() -> Vec<u8> {
let mut out = b"%PDF-1.4\n".to_vec();
let off1 = out.len() as u64;
out.extend_from_slice(b"1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n");
let off2 = out.len() as u64;
out.extend_from_slice(
b"2 0 obj\n<< /Type /Pages /Kids [<< /Type /Page /MediaBox [0 0 612 792] >>] /Count 1 >>\nendobj\n",
);
let xref_start = out.len() as u64;
out.extend_from_slice(b"xref\n0 3\n0000000000 65535 f \n");
out.extend_from_slice(format!("{off1:010} 00000 n \n").as_bytes());
out.extend_from_slice(format!("{off2:010} 00000 n \n").as_bytes());
out.extend_from_slice(
format!("trailer\n<< /Size 3 /Root 1 0 R >>\nstartxref\n{xref_start}\n%%EOF\n")
.as_bytes(),
);
out
}
fn open(bytes: Vec<u8>) -> Pdf<Cursor<Vec<u8>>> {
Pdf::open(Cursor::new(bytes)).expect("PDF should parse")
}
fn resolved_handle(pdf: &mut Pdf<Cursor<Vec<u8>>>, object_ref: ObjectRef) -> ObjectHandle {
let handle = pdf.get_object_handle(object_ref);
pdf.resolve(&handle).expect("resolve object");
handle
}
fn resolved_key(
pdf: &mut Pdf<Cursor<Vec<u8>>>,
object: &ObjectHandle,
key: &[u8],
) -> ObjectHandle {
let value = object.get_key(key);
pdf.resolve(&value).expect("resolve dictionary child");
value
}
fn integer_array(pdf: &mut Pdf<Cursor<Vec<u8>>>, value: ObjectHandle) -> Vec<i64> {
resolved_handle_value(pdf, value)
.as_array()
.expect("array")
.into_iter()
.map(|item| {
resolved_handle_value(pdf, item)
.as_integer()
.expect("integer array item")
})
.collect()
}
fn resolved_handle_value(pdf: &mut Pdf<Cursor<Vec<u8>>>, value: ObjectHandle) -> ObjectHandle {
pdf.resolve(&value).expect("resolve handle");
value
}
fn integer_array_key(
pdf: &mut Pdf<Cursor<Vec<u8>>>,
object: &ObjectHandle,
key: &[u8],
) -> Vec<i64> {
let value = resolved_key(pdf, object, key);
integer_array(pdf, value)
}
fn handle_of(pdf: &mut Pdf<Cursor<Vec<u8>>>, object_ref: ObjectRef) -> ObjectHandle {
resolved_handle(pdf, object_ref)
}
fn set_handle(pdf: &mut Pdf<Cursor<Vec<u8>>>, object_ref: ObjectRef, value: ObjectHandle) {
pdf.replace_object(object_ref, value)
.expect("set canonical object handle");
}
#[test]
fn rebuild_marks_qpdf_get_all_pages_observation() {
let mut pdf = open(build_nested_pdf());
assert!(!pdf.ever_called_get_all_pages());
rebuild_page_tree(&mut pdf, &[ObjectRef::new(4, 0)]).unwrap();
assert!(
pdf.ever_called_get_all_pages(),
"qpdf rebuild preparation enumerates pages through getAllPages"
);
}
#[test]
fn rebuild_invalidates_the_cached_page_list() {
let mut pdf = open(build_nested_pdf());
let before = crate::PageDocumentHelper::new(&mut pdf)
.get_all_pages()
.expect("initial page list");
assert_eq!(
before,
vec![
ObjectRef::new(4, 0),
ObjectRef::new(5, 0),
ObjectRef::new(6, 0)
]
);
rebuild_page_tree(&mut pdf, &[ObjectRef::new(5, 0)]).expect("page rebuild");
let after = crate::PageDocumentHelper::new(&mut pdf)
.get_all_pages()
.expect("page list after rebuild");
assert_eq!(after, vec![ObjectRef::new(5, 0)]);
}
#[allow(
clippy::mutable_key_type,
reason = "the test exercises the canonical live-allocation identity set"
)]
#[test]
fn collect_page_tree_nodes_handles_duplicate_and_malformed_handles() {
let pages = ObjectHandle::dictionary(vec![
(b"/Type".to_vec(), ObjectHandle::name(b"Pages".to_vec())),
(b"/Kids".to_vec(), ObjectHandle::integer(7)),
]);
let mut nodes = Vec::new();
let mut seen = HashSet::new();
collect_page_tree_nodes(pages.clone(), &mut nodes, &mut seen, 0, 8)
.expect("a malformed /Kids value is treated as an empty array");
assert_eq!(nodes.len(), 1);
collect_page_tree_nodes(pages, &mut nodes, &mut seen, 0, 8)
.expect("a repeated canonical handle is skipped");
assert_eq!(nodes.len(), 1);
let leaf = ObjectHandle::dictionary(vec![(
b"/Type".to_vec(),
ObjectHandle::name(b"Page".to_vec()),
)]);
collect_page_tree_nodes(leaf, &mut nodes, &mut seen, 0, 8)
.expect("a non-/Pages dictionary is ignored");
assert_eq!(nodes.len(), 1);
let direct_pages = ObjectHandle::dictionary(vec![(
b"/Type".to_vec(),
ObjectHandle::name(b"Pages".to_vec()),
)]);
let error = collect_page_tree_nodes(direct_pages, &mut nodes, &mut seen, 0, 0)
.expect_err("the depth bound must apply before dereferencing the node");
assert!(
matches!(error, Error::Unsupported(message) if message.contains("direct /Pages node"))
);
}
#[test]
fn empty_selection_rebuilds_an_empty_page_tree() {
let mut pdf = open(build_nested_pdf());
let result = rebuild_page_tree(&mut pdf, &[]).expect("qpdf accepts zero selected pages");
assert!(result.new_kids.is_empty());
assert_eq!(
result.removed_pages,
BTreeSet::from([
ObjectRef::new(4, 0),
ObjectRef::new(5, 0),
ObjectRef::new(6, 0),
])
);
let pages = resolved_handle(&mut pdf, ObjectRef::new(2, 0));
assert_eq!(pages.get_key(b"/Count").as_integer(), Some(0));
assert_eq!(
pages
.get_key(b"/Kids")
.as_array()
.expect("empty /Kids array")
.len(),
0
);
}
#[test]
fn selecting_a_pages_node_is_rejected() {
let mut pdf = open(build_nested_pdf());
let err = rebuild_page_tree(&mut pdf, &[ObjectRef::new(2, 0)]).unwrap_err();
assert!(
matches!(err, Error::Unsupported(_)),
"expected Unsupported for /Pages node, got {err:?}"
);
}
#[test]
fn count_and_kids_match_selection_order() {
let mut pdf = open(build_nested_pdf());
let sel = [ObjectRef::new(6, 0), ObjectRef::new(4, 0)];
let res = rebuild_page_tree(&mut pdf, &sel).unwrap();
assert_eq!(
res.new_kids,
vec![ObjectRef::new(6, 0), ObjectRef::new(4, 0)]
);
let root = handle_of(&mut pdf, ObjectRef::new(2, 0));
assert_eq!(
resolved_key(&mut pdf, &root, b"/Count").as_integer(),
Some(2)
);
let kids = resolved_key(&mut pdf, &root, b"/Kids")
.as_array()
.expect("root /Kids array");
assert_eq!(
kids.into_iter()
.map(|kid| kid.object_ref().expect("/Kids indirect page"))
.collect::<Vec<_>>(),
vec![ObjectRef::new(6, 0), ObjectRef::new(4, 0)]
);
}
#[test]
fn direct_catalog_root_stays_direct_after_rebuild_and_round_trips() {
let mut pdf = open(build_nested_pdf());
let root = handle_of(&mut pdf, ObjectRef::new(2, 0));
let direct_root = root
.shallow_copy()
.expect("/Pages dictionary should be shallow-copyable");
let catalog = handle_of(&mut pdf, ObjectRef::new(1, 0));
assert!(
catalog.as_dictionary().is_some(),
"catalog must be a dictionary"
);
catalog
.replace_key(b"/Pages", direct_root)
.expect("catalog /Pages replacement");
pdf.mark_object_handle_dirty(&catalog)
.expect("catalog mutation must be dirty");
let result =
rebuild_page_tree(&mut pdf, &[ObjectRef::new(4, 0), ObjectRef::new(4, 0)]).unwrap();
assert_eq!(result.new_kids.len(), 2);
let catalog = handle_of(&mut pdf, ObjectRef::new(1, 0));
let root = resolved_key(&mut pdf, &catalog, b"/Pages");
assert!(root.is_direct(), "catalog /Pages must remain direct");
assert_eq!(
resolved_key(&mut pdf, &root, b"/Count").as_integer(),
Some(2)
);
for page_ref in result.new_kids {
let page = handle_of(&mut pdf, page_ref);
let parent = resolved_key(&mut pdf, &page, b"/Parent");
assert!(
parent.is_same_object_as(&root),
"page /Parent must retain the direct root identity"
);
}
let out = write_qpdf_to_memory(&mut pdf, |_| {}).unwrap();
let mut reopened = Pdf::open(Cursor::new(out)).expect("direct-root output must parse");
assert_eq!(
crate::PageDocumentHelper::new(&mut reopened)
.get_all_pages()
.unwrap()
.len(),
2
);
}
#[test]
fn rebuild_does_not_synthesize_absent_rotate() {
let mut pdf = open(build_direct_leaf_pdf());
let prepared = crate::pages::repair::prepare_for_optimization(&mut pdf)
.expect("page preparation must succeed")
.expect("fixture has a page tree");
assert_eq!(prepared.pages, vec![ObjectRef::new(3, 0)]);
rebuild_page_tree(&mut pdf, &[ObjectRef::new(3, 0)])
.expect("flat page rebuild must succeed");
let page = handle_of(&mut pdf, ObjectRef::new(3, 0));
assert!(
!page.has_key(b"/Rotate"),
"qpdf leaves an absent inherited /Rotate absent"
);
}
#[test]
fn rebuild_preserves_inherited_indirect_handle_identity() {
let mut pdf = open(build_nested_pdf());
let page = pdf.get_object_handle(ObjectRef::new(4, 0));
let resources = pdf.get_object_handle(ObjectRef::new(9, 0));
rebuild_page_tree(&mut pdf, &[ObjectRef::new(4, 0)])
.expect("flat page rebuild must succeed");
let page_resources = page
.try_get_key(b"/Resources")
.expect("page /Resources lookup must succeed");
assert!(
page_resources.is_same_object_as(&resources),
"inherited /Resources must retain its live indirect handle"
);
}
#[test]
fn rebuild_promotes_a_catalog_alias_with_the_inherited_value_in_place() {
let mut pdf = Pdf::empty().expect("empty PDF");
let catalog = pdf.root_handle().expect("empty PDF catalog");
let pages = resolved_handle(&mut pdf, ObjectRef::new(2, 0));
let shared =
ObjectHandle::dictionary(vec![(b"/Marker".to_vec(), ObjectHandle::integer(7))]);
let page = pdf
.make_indirect_from_object_handle(ObjectHandle::dictionary(vec![
(b"/Type".to_vec(), ObjectHandle::name(b"Page".to_vec())),
(b"/Parent".to_vec(), pages.clone()),
]))
.expect("page object allocation");
pdf.mark_object_handle_dirty(&page)
.expect("page allocation must be dirty");
let page_ref = page.object_ref().expect("page object reference");
catalog
.replace_key(b"/Foo", shared.clone())
.expect("Catalog alias insertion");
pages
.replace_key(b"/Resources", shared.clone())
.expect("Pages alias insertion");
pages
.replace_key(b"/Kids", ObjectHandle::array(vec![page]))
.expect("Pages kids insertion");
pages
.replace_key(b"/Count", ObjectHandle::integer(1))
.expect("Pages count insertion");
pdf.mark_object_handle_dirty(&catalog)
.expect("Catalog mutation must be dirty");
pdf.mark_object_handle_dirty(&pages)
.expect("Pages mutation must be dirty");
rebuild_page_tree(&mut pdf, &[page_ref]).expect("page-selection rebuild");
let foo = catalog.try_get_key(b"/Foo").expect("Catalog alias lookup");
let page_resources = pdf
.get_object_handle(page_ref)
.try_get_key(b"/Resources")
.expect("page inherited resources lookup");
assert!(
shared.is_indirect(),
"qpdf promotion must update the aliased direct allocation in place"
);
assert!(
foo.is_indirect(),
"Catalog alias must become an indirect reference"
);
assert!(
page_resources.is_indirect(),
"inherited /Resources must become an indirect reference"
);
assert_eq!(
foo.object_ref(),
page_resources.object_ref(),
"Catalog /Foo and page /Resources must use the same object number"
);
assert!(
foo.is_same_object_as(&page_resources),
"Catalog /Foo and page /Resources must retain the same live object identity"
);
}
#[test]
fn rebuild_mutates_retained_page_handles_in_place() {
let mut pdf = open(build_nested_pdf());
let root = pdf.get_object_handle(ObjectRef::new(2, 0));
let page = pdf.get_object_handle(ObjectRef::new(4, 0));
rebuild_page_tree(&mut pdf, &[ObjectRef::new(4, 0)])
.expect("flat page rebuild must succeed");
assert_eq!(
root.try_get_key(b"/Count")
.expect("live root /Count lookup must succeed")
.as_integer(),
Some(1)
);
let kids = root
.try_get_key(b"/Kids")
.expect("live root /Kids lookup must succeed");
assert_eq!(
kids.try_array_item(0)
.expect("live root /Kids item lookup must succeed")
.expect("the rebuilt root must have one child")
.object_ref(),
Some(ObjectRef::new(4, 0))
);
assert!(
page.try_get_key(b"/Parent")
.expect("live page /Parent lookup must succeed")
.is_same_object_as(&root),
"reparenting must be visible through the retained page handle"
);
}
#[test]
fn rebuilt_root_drops_qpdf_inheritable_attributes_after_materializing_pages() {
let mut pdf = open(build_nested_pdf());
let root = handle_of(&mut pdf, ObjectRef::new(2, 0));
root.replace_key(
b"/MediaBox",
ObjectHandle::array(vec![
ObjectHandle::integer(0),
ObjectHandle::integer(0),
ObjectHandle::integer(612),
ObjectHandle::integer(792),
]),
)
.unwrap();
root.replace_key(
b"/CropBox",
ObjectHandle::array(vec![
ObjectHandle::integer(10),
ObjectHandle::integer(20),
ObjectHandle::integer(500),
ObjectHandle::integer(700),
]),
)
.unwrap();
root.replace_key(b"/Resources", ObjectHandle::dictionary(Vec::new()))
.unwrap();
root.replace_key(b"/Rotate", ObjectHandle::integer(180))
.unwrap();
pdf.mark_object_handle_dirty(&root).unwrap();
rebuild_page_tree(&mut pdf, &[ObjectRef::new(6, 0)])
.expect("flat page rebuild must succeed");
let root = handle_of(&mut pdf, ObjectRef::new(2, 0));
for key in [
b"/MediaBox".as_slice(),
b"/CropBox".as_slice(),
b"/Resources".as_slice(),
b"/Rotate".as_slice(),
] {
assert!(
!root.has_key(key),
"rebuilt /Pages root must not retain inheritable {key:?}"
);
}
let intermediate = handle_of(&mut pdf, ObjectRef::new(3, 0));
for key in [
b"/MediaBox".as_slice(),
b"/CropBox".as_slice(),
b"/Resources".as_slice(),
b"/Rotate".as_slice(),
] {
assert!(
!intermediate.has_key(key),
"orphaned intermediate /Pages must not retain inheritable {key:?}"
);
}
let page = handle_of(&mut pdf, ObjectRef::new(6, 0));
assert_eq!(
resolved_key(&mut pdf, &page, b"/CropBox").object_ref(),
Some(ObjectRef::new(10, 0)),
"the selected page must retain the root-inherited /CropBox handle"
);
}
#[test]
fn promotes_direct_inheritable_values_on_deselected_branches() {
let mut pdf = open(build_deselected_branch_pdf());
rebuild_page_tree(&mut pdf, &[ObjectRef::new(6, 0)])
.expect("page-tree rebuild must succeed");
let promoted = pdf
.get_all_objects()
.expect("canonical object enumeration must succeed")
.into_iter()
.filter_map(|handle| handle.as_array())
.any(|items| {
items.len() == 4
&& items
.iter()
.map(ObjectHandle::as_integer)
.collect::<Vec<_>>()
== vec![Some(0), Some(0), Some(200), Some(300)]
});
assert!(
promoted,
"qpdf promotes the deselected branch's direct /MediaBox before cleanup"
);
}
#[test]
fn rebuild_warns_for_unknown_keys_only_on_flattened_pages_nodes() {
let mut pdf = open(build_nested_pdf());
let root = handle_of(&mut pdf, ObjectRef::new(2, 0));
root.replace_key(b"/UserUnit", ObjectHandle::integer(1))
.unwrap();
pdf.mark_object_handle_dirty(&root).unwrap();
let intermediate = handle_of(&mut pdf, ObjectRef::new(3, 0));
intermediate
.replace_key(b"/UserUnit", ObjectHandle::integer(2))
.unwrap();
pdf.mark_object_handle_dirty(&intermediate).unwrap();
rebuild_page_tree(&mut pdf, &[ObjectRef::new(4, 0)])
.expect("page-tree rebuild must preserve qpdf warning behavior");
let diagnostics = pdf.repair_diagnostics();
let warnings: Vec<_> = diagnostics
.entries()
.iter()
.map(|entry| entry.message.as_str())
.filter(|message| message.contains("Unknown key /UserUnit"))
.collect();
assert_eq!(
warnings,
["(Pages object: object 3 0): Unknown key /UserUnit in /Pages object is being discarded as a result of flattening the /Pages tree"],
"only the flattened intermediate /Pages node should warn"
);
let root = handle_of(&mut pdf, ObjectRef::new(2, 0));
assert_eq!(
resolved_key(&mut pdf, &root, b"/UserUnit").as_integer(),
Some(1)
);
}
#[test]
fn rebuild_propagates_unknown_pages_warning_sink_failure() {
let mut pdf = open(build_nested_pdf());
let intermediate = handle_of(&mut pdf, ObjectRef::new(3, 0));
intermediate
.replace_key(b"/UserUnit", ObjectHandle::integer(2))
.unwrap();
pdf.mark_object_handle_dirty(&intermediate).unwrap();
let logger = crate::QPDFLogger::create();
logger.set_warn(Some(PipelineHandle::new(NthWriteFailure::new(1))));
pdf.set_logger(logger);
assert!(matches!(
rebuild_page_tree(&mut pdf, &[ObjectRef::new(4, 0)]),
Err(Error::System(ref message)) if message == "sink write failure 1"
));
assert!(pdf
.repair_diagnostics()
.entries()
.iter()
.any(|diagnostic| diagnostic.message.contains("Unknown key /UserUnit")));
}
#[test]
fn inherited_attrs_materialized_on_leaf() {
let mut pdf = open(build_nested_pdf());
rebuild_page_tree(&mut pdf, &[ObjectRef::new(4, 0)]).unwrap();
let leaf = handle_of(&mut pdf, ObjectRef::new(4, 0));
assert_eq!(
resolved_key(&mut pdf, &leaf, b"/Rotate").as_integer(),
Some(90)
);
let media_box = resolved_key(&mut pdf, &leaf, b"/MediaBox");
assert!(
media_box.is_indirect(),
"expected promoted inherited /MediaBox reference"
);
assert_eq!(integer_array(&mut pdf, media_box), vec![0, 0, 200, 300]);
assert_eq!(
resolved_key(&mut pdf, &leaf, b"/Resources").object_ref(),
Some(ObjectRef::new(9, 0))
);
assert_eq!(
resolved_key(&mut pdf, &leaf, b"/Parent").object_ref(),
Some(ObjectRef::new(2, 0))
);
}
#[test]
fn own_attribute_wins_over_inherited() {
let mut pdf = open(build_nested_pdf());
rebuild_page_tree(&mut pdf, &[ObjectRef::new(5, 0)]).unwrap();
let leaf = handle_of(&mut pdf, ObjectRef::new(5, 0));
assert_eq!(
integer_array_key(&mut pdf, &leaf, b"/MediaBox"),
vec![0, 0, 400, 500],
"own /MediaBox must win over inherited [0 0 200 300]"
);
assert_eq!(
resolved_key(&mut pdf, &leaf, b"/Rotate").as_integer(),
Some(90)
);
}
#[test]
fn duplicate_selection_yields_distinct_page_dicts_sharing_contents() {
let mut pdf = open(build_nested_pdf());
let res =
rebuild_page_tree(&mut pdf, &[ObjectRef::new(4, 0), ObjectRef::new(4, 0)]).unwrap();
assert_eq!(res.new_kids.len(), 2);
assert_eq!(res.new_kids[0], ObjectRef::new(4, 0)); let clone_ref = res.new_kids[1];
assert_ne!(clone_ref, ObjectRef::new(4, 0), "second slot is a clone");
assert_eq!(
res.ref_map.get(&ObjectRef::new(4, 0)),
Some(&vec![ObjectRef::new(4, 0), clone_ref])
);
let original = handle_of(&mut pdf, ObjectRef::new(4, 0));
let clone = handle_of(&mut pdf, clone_ref);
assert_eq!(
resolved_key(&mut pdf, &original, b"/Contents").object_ref(),
resolved_key(&mut pdf, &clone, b"/Contents").object_ref()
);
assert_eq!(
resolved_key(&mut pdf, &original, b"/Contents").object_ref(),
Some(ObjectRef::new(7, 0))
);
assert_eq!(
resolved_key(&mut pdf, &clone, b"/Rotate").as_integer(),
Some(90)
);
let root = handle_of(&mut pdf, ObjectRef::new(2, 0));
assert_eq!(
resolved_key(&mut pdf, &root, b"/Count").as_integer(),
Some(2)
);
}
#[test]
fn duplicate_selection_allocates_after_canonical_direct_promotion() {
let mut pdf = open(build_direct_leaf_pdf());
let prepared = crate::pages::repair::prepare_for_optimization(&mut pdf)
.expect("page preparation must succeed")
.expect("fixture has a page tree");
assert_eq!(prepared.pages, vec![ObjectRef::new(3, 0)]);
let result = rebuild_page_tree(&mut pdf, &[ObjectRef::new(3, 0), ObjectRef::new(3, 0)])
.expect("duplicate rebuild must succeed");
assert_eq!(
result.new_kids,
vec![ObjectRef::new(3, 0), ObjectRef::new(4, 0)]
);
let root = handle_of(&mut pdf, ObjectRef::new(2, 0));
let kids = resolved_key(&mut pdf, &root, b"/Kids")
.as_array()
.expect("rebuilt /Kids array");
assert_eq!(
kids.into_iter()
.map(|kid| kid.object_ref().expect("/Kids page reference"))
.collect::<Vec<_>>(),
vec![ObjectRef::new(3, 0), ObjectRef::new(4, 0)]
);
}
#[test]
fn subset_round_trips_to_valid_pdf() {
let mut pdf = open(build_nested_pdf());
rebuild_page_tree(&mut pdf, &[ObjectRef::new(4, 0), ObjectRef::new(6, 0)]).unwrap();
let out = write_qpdf_to_memory(&mut pdf, |_| {}).unwrap();
let mut pdf2 = Pdf::open(Cursor::new(out.clone())).expect("rebuilt PDF should parse");
let refs = page_refs(&mut pdf2).expect("page tree should walk");
assert_eq!(refs.len(), 2, "/Pages should enumerate exactly 2 leaves");
for page_ref in refs {
let leaf = handle_of(&mut pdf2, page_ref);
assert_eq!(
resolved_key(&mut pdf2, &leaf, b"/Rotate").as_integer(),
Some(90)
);
assert!(leaf.has_key(b"/MediaBox"));
}
check_bytes_for_test(out).expect("canonical qpdf check should run");
}
#[test]
fn duplicate_round_trips_with_correct_page_count() {
let mut pdf = open(build_nested_pdf());
rebuild_page_tree(
&mut pdf,
&[
ObjectRef::new(4, 0),
ObjectRef::new(4, 0),
ObjectRef::new(5, 0),
],
)
.unwrap();
let out = write_qpdf_to_memory(&mut pdf, |_| {}).unwrap();
let mut pdf2 = Pdf::open(Cursor::new(out)).expect("should parse");
let refs = page_refs(&mut pdf2).expect("walk");
assert_eq!(refs.len(), 3, "duplicate selection → 3 enumerated pages");
}
#[test]
fn rebuild_honors_repair_depth_limit() {
let mut pdf = open(build_nested_pdf());
let root = handle_of(&mut pdf, ObjectRef::new(2, 0));
let first_ref = pdf.get_object_handle(ObjectRef::new(10, 0));
root.replace_key(b"/Kids", ObjectHandle::array(vec![first_ref]))
.unwrap();
root.replace_key(b"/Count", ObjectHandle::integer(1))
.unwrap();
pdf.mark_object_handle_dirty(&root).unwrap();
let first = ObjectHandle::dictionary(vec![
(b"/Type".to_vec(), ObjectHandle::name(b"Pages".to_vec())),
(
b"/Parent".to_vec(),
pdf.get_object_handle(ObjectRef::new(2, 0)),
),
(
b"/Kids".to_vec(),
ObjectHandle::array(vec![pdf.get_object_handle(ObjectRef::new(11, 0))]),
),
(b"/Count".to_vec(), ObjectHandle::integer(1)),
]);
set_handle(&mut pdf, ObjectRef::new(10, 0), first);
let second = ObjectHandle::dictionary(vec![
(
b"/Parent".to_vec(),
pdf.get_object_handle(ObjectRef::new(10, 0)),
),
(
b"/Kids".to_vec(),
ObjectHandle::array(vec![pdf.get_object_handle(ObjectRef::new(4, 0))]),
),
(b"/Count".to_vec(), ObjectHandle::integer(1)),
]);
set_handle(&mut pdf, ObjectRef::new(11, 0), second);
let leaf = handle_of(&mut pdf, ObjectRef::new(4, 0));
leaf.replace_key(b"/Parent", pdf.get_object_handle(ObjectRef::new(10, 0)))
.unwrap();
leaf.replace_key(b"/Rotate", ObjectHandle::integer(0))
.unwrap();
leaf.replace_key(b"/Resources", ObjectHandle::dictionary(Vec::new()))
.unwrap();
leaf.replace_key(
b"/CropBox",
ObjectHandle::array(vec![
ObjectHandle::integer(0),
ObjectHandle::integer(0),
ObjectHandle::integer(200),
ObjectHandle::integer(300),
]),
)
.unwrap();
pdf.mark_object_handle_dirty(&leaf).unwrap();
let before_root = handle_of(&mut pdf, ObjectRef::new(2, 0)).unparse_resolved();
let before_second = handle_of(&mut pdf, ObjectRef::new(11, 0)).unparse_resolved();
let error = rebuild_page_tree_with_max_depth(&mut pdf, &[ObjectRef::new(4, 0)], 2)
.expect_err("repair must use the caller-supplied depth limit");
assert!(matches!(error, Error::Unsupported(_)), "got {error:?}");
assert_eq!(
handle_of(&mut pdf, ObjectRef::new(2, 0)).unparse_resolved(),
before_root
);
assert_eq!(
handle_of(&mut pdf, ObjectRef::new(11, 0)).unparse_resolved(),
before_second
);
}
#[test]
fn rebuild_cases_are_pinned_to_qpdf_11_9_page_probe() {
let version = match Command::new("qpdf").arg("--version").output() {
Ok(output) if output.status.success() => {
String::from_utf8_lossy(&output.stdout).into_owned()
}
Ok(_) | Err(_) => {
eprintln!("qpdf 11.9.0 unavailable; skipping page rebuild oracle probe");
return;
} };
if !version.starts_with("qpdf version 11.9.") {
eprintln!("qpdf 11.9.0 unavailable; skipping page rebuild oracle probe");
return;
}
let directory = tempfile::tempdir().expect("create qpdf page probe directory");
let input = directory.path().join("input.pdf");
let output = directory.path().join("output.pdf");
std::fs::write(&input, build_nested_pdf()).expect("write qpdf page probe input");
let flattened = Command::new("qpdf")
.arg(&input)
.arg("--pages")
.arg(&input)
.arg("1,1")
.arg("--")
.arg(&output)
.output()
.expect("run qpdf duplicate page probe");
assert!(
flattened.status.success(),
"qpdf duplicate page probe failed: {}",
String::from_utf8_lossy(&flattened.stderr) );
let pages = Command::new("qpdf")
.arg("--show-pages")
.arg(&output)
.output()
.expect("run qpdf page listing probe");
assert!(
pages.status.success(),
"qpdf page listing probe failed: {}",
String::from_utf8_lossy(&pages.stderr) );
let pages = String::from_utf8_lossy(&pages.stdout);
assert_eq!(
pages.matches("page ").count(),
2,
"qpdf duplicate selection must produce two page entries: {pages}"
);
let page = Command::new("qpdf")
.arg("--show-object=4")
.arg(&output)
.output()
.expect("show qpdf first page probe");
assert!(
page.status.success(),
"qpdf first-page probe failed: {}",
String::from_utf8_lossy(&page.stderr) );
let page = String::from_utf8_lossy(&page.stdout);
assert!(page.contains("/Rotate 90"), "qpdf first page: {page}");
assert!(page.contains("/MediaBox"), "qpdf first page: {page}");
assert!(page.contains("/Resources"), "qpdf first page: {page}");
let mut direct_leaf = open(build_direct_leaf_pdf());
rebuild_page_tree(&mut direct_leaf, &[ObjectRef::new(3, 0)])
.expect("direct leaf rebuild must succeed");
let direct_page = handle_of(&mut direct_leaf, ObjectRef::new(3, 0));
assert!(
!direct_page.has_key(b"/Rotate"),
"qpdf's direct-leaf probe has no inherited /Rotate to push"
);
}
#[test]
fn promote_inherited_value_promotes_direct_streams() {
let mut pdf = Pdf::empty().expect("empty PDF");
let stream = ObjectHandle::stream(
ObjectHandle::dictionary(Vec::new()),
Rc::new(b"stream".to_vec()),
);
let promoted = promote_inherited_value(&mut pdf, stream)
.expect("direct stream promotion must succeed");
assert!(promoted.is_indirect());
assert!(promoted.as_stream_dict().is_some());
}
#[test]
fn promote_inherited_value_preserves_a_same_identity_alias_in_place() {
let mut pdf = Pdf::empty().expect("empty PDF");
let value = ObjectHandle::dictionary(vec![(b"Marker".to_vec(), ObjectHandle::integer(7))]);
let alias = value.clone();
let promoted =
promote_inherited_value(&mut pdf, value).expect("first promotion must succeed");
assert!(promoted.is_indirect());
assert!(alias.is_indirect());
assert!(promoted.is_same_object_as(&alias));
}
}