Write PDF bookmarks for section headings
This commit is contained in:
1 parent
863e7c1a81
commit
5f98c199a5
5 files changed
+146
-9
No files matched your search
@@ -627,6 +627,14 @@ pub struct PdfMetadata {
|
||||
pub author: Option<String>,
|
||||
pub subject: Option<String>,
|
||||
pub keywords: Option<String>,
|
||||
pub outline: Vec<PdfOutlineEntry>,
|
||||
}
|
||||
|
||||
/// One flat bookmark pointing at a page of the exported document.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct PdfOutlineEntry {
|
||||
pub title: String,
|
||||
pub page: usize,
|
||||
}
|
||||
|
||||
fn pdf_text(value: &str) -> String {
|
||||
@@ -919,6 +927,7 @@ fn write_pdf(
|
||||
}
|
||||
|
||||
let mut kids = Vec::new();
|
||||
let mut page_ids: Vec<usize> = Vec::new();
|
||||
for list in pages {
|
||||
let mut used = BTreeMap::new();
|
||||
for item in &list.items {
|
||||
@@ -983,6 +992,7 @@ fn write_pdf(
|
||||
.join(" ");
|
||||
format!(" /Annots [{refs}]")
|
||||
};
|
||||
page_ids.push(page_id);
|
||||
kids.push(format!("{page_id} 0 R"));
|
||||
objects.push(
|
||||
format!(
|
||||
@@ -1051,6 +1061,47 @@ fn write_pdf(
|
||||
)
|
||||
.into_bytes();
|
||||
|
||||
if !metadata.outline.is_empty() {
|
||||
let root_id = objects.len() + 1;
|
||||
objects.push(Vec::new());
|
||||
let item_ids: Vec<usize> = (0..metadata.outline.len())
|
||||
.map(|_| {
|
||||
objects.push(Vec::new());
|
||||
objects.len()
|
||||
})
|
||||
.collect();
|
||||
for (index, entry) in metadata.outline.iter().enumerate() {
|
||||
let page_id = page_ids
|
||||
.get(entry.page)
|
||||
.or_else(|| page_ids.last())
|
||||
.copied()
|
||||
.unwrap_or(0);
|
||||
let mut dict = format!(
|
||||
"<< /Title {} /Parent {} 0 R /Dest [{} 0 R /Fit]",
|
||||
pdf_text(&entry.title),
|
||||
root_id,
|
||||
page_id
|
||||
);
|
||||
if index > 0 {
|
||||
dict.push_str(&format!(" /Prev {} 0 R", item_ids[index - 1]));
|
||||
}
|
||||
if index + 1 < item_ids.len() {
|
||||
dict.push_str(&format!(" /Next {} 0 R", item_ids[index + 1]));
|
||||
}
|
||||
dict.push_str(" >>");
|
||||
objects[item_ids[index] - 1] = dict.into_bytes();
|
||||
}
|
||||
objects[root_id - 1] = format!(
|
||||
"<< /Type /Outlines /First {} 0 R /Last {} 0 R /Count {} >>",
|
||||
item_ids[0],
|
||||
item_ids[item_ids.len() - 1],
|
||||
item_ids.len()
|
||||
)
|
||||
.into_bytes();
|
||||
objects[0] =
|
||||
format!("<< /Type /Catalog /Pages 2 0 R /Outlines {root_id} 0 R >>").into_bytes();
|
||||
}
|
||||
|
||||
let info_ref = match metadata_object(metadata) {
|
||||
Some(object) => {
|
||||
objects.push(object.into_bytes());
|
||||
@@ -1152,6 +1203,52 @@ mod type1_pdf_tests {
|
||||
|
||||
#[test]
|
||||
#[test]
|
||||
#[test]
|
||||
fn outline_entries_become_pdf_bookmarks() {
|
||||
let pages = vec![
|
||||
DisplayList {
|
||||
width: 612 * 65536,
|
||||
height: 792 * 65536,
|
||||
items: Vec::new(),
|
||||
images: Vec::new(),
|
||||
},
|
||||
DisplayList {
|
||||
width: 612 * 65536,
|
||||
height: 792 * 65536,
|
||||
items: Vec::new(),
|
||||
images: Vec::new(),
|
||||
},
|
||||
];
|
||||
let metadata = PdfMetadata {
|
||||
title: Some("Book".into()),
|
||||
outline: vec![
|
||||
PdfOutlineEntry {
|
||||
title: "1 One".into(),
|
||||
page: 0,
|
||||
},
|
||||
PdfOutlineEntry {
|
||||
title: "2 Two".into(),
|
||||
page: 1,
|
||||
},
|
||||
],
|
||||
..PdfMetadata::default()
|
||||
};
|
||||
let pdf = String::from_utf8_lossy(&display_lists_to_pdf_with_cm_type1_and_metadata(
|
||||
&pages,
|
||||
612 * 65536,
|
||||
792 * 65536,
|
||||
PageOrigin::default(),
|
||||
&metadata,
|
||||
))
|
||||
.to_string();
|
||||
assert!(pdf.contains("/Type /Outlines"));
|
||||
assert!(pdf.contains("/Title (1 One)"));
|
||||
assert!(pdf.contains("/Title (2 Two)"));
|
||||
assert!(pdf.contains("/Count 2"));
|
||||
assert!(pdf.contains("/Dest ["));
|
||||
assert!(pdf.contains("/Outlines"));
|
||||
}
|
||||
|
||||
fn metadata_writes_the_pdf_info_dictionary() {
|
||||
let pages = vec![DisplayList {
|
||||
width: 612 * 65536,
|
||||
@@ -1164,6 +1261,7 @@ mod type1_pdf_tests {
|
||||
author: Some("Ada".into()),
|
||||
subject: None,
|
||||
keywords: None,
|
||||
outline: Vec::new(),
|
||||
};
|
||||
let pdf = String::from_utf8_lossy(&display_lists_to_pdf_with_cm_type1_and_metadata(
|
||||
&pages,
|
||||
|
||||
Reference in new issue
Block a user