Write PDF metadata from hypersetup
This commit is contained in:
1 parent
8686b9a35e
commit
863e7c1a81
5 files changed
+213
-8
No files matched your search
@@ -620,6 +620,55 @@ fn page_content(
|
||||
content
|
||||
}
|
||||
|
||||
/// Document information written into the PDF Info dictionary.
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
||||
pub struct PdfMetadata {
|
||||
pub title: Option<String>,
|
||||
pub author: Option<String>,
|
||||
pub subject: Option<String>,
|
||||
pub keywords: Option<String>,
|
||||
}
|
||||
|
||||
fn pdf_text(value: &str) -> String {
|
||||
if value.chars().all(|character| {
|
||||
(' '..='~').contains(&character)
|
||||
&& character != '('
|
||||
&& character != ')'
|
||||
&& character != '\\'
|
||||
}) {
|
||||
format!("({value})")
|
||||
} else {
|
||||
let mut text = String::from("<FEFF");
|
||||
for unit in value.encode_utf16() {
|
||||
text.push_str(&format!("{unit:04X}"));
|
||||
}
|
||||
text.push('>');
|
||||
text
|
||||
}
|
||||
}
|
||||
|
||||
fn metadata_object(metadata: &PdfMetadata) -> Option<String> {
|
||||
let mut fields = Vec::new();
|
||||
if let Some(title) = &metadata.title {
|
||||
fields.push(format!("/Title {}", pdf_text(title)));
|
||||
}
|
||||
if let Some(author) = &metadata.author {
|
||||
fields.push(format!("/Author {}", pdf_text(author)));
|
||||
}
|
||||
if let Some(subject) = &metadata.subject {
|
||||
fields.push(format!("/Subject {}", pdf_text(subject)));
|
||||
}
|
||||
if let Some(keywords) = &metadata.keywords {
|
||||
fields.push(format!("/Keywords {}", pdf_text(keywords)));
|
||||
}
|
||||
if fields.is_empty() {
|
||||
return None;
|
||||
}
|
||||
fields.push("/Producer (texrra route-D)".to_string());
|
||||
fields.push("/Creator (texrra route-D)".to_string());
|
||||
Some(format!("<< {} >>", fields.join(" ")))
|
||||
}
|
||||
|
||||
/// Write several display lists as a multi-page PDF at the default page origin.
|
||||
pub fn display_lists_to_pdf(pages: &[DisplayList], page_height: i64) -> Vec<u8> {
|
||||
display_lists_to_pdf_with_origin(pages, page_height, PageOrigin::default())
|
||||
@@ -659,6 +708,7 @@ pub fn display_lists_to_pdf_with_paper(
|
||||
page_height,
|
||||
origin,
|
||||
FontBackend::TrueType,
|
||||
&PdfMetadata::default(),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -676,6 +726,25 @@ pub fn display_lists_to_pdf_with_cm_type1(
|
||||
page_height,
|
||||
origin,
|
||||
FontBackend::ComputerModern,
|
||||
&PdfMetadata::default(),
|
||||
)
|
||||
}
|
||||
|
||||
/// Write PDFs with Computer Modern Type1 outlines and a document Info dictionary.
|
||||
pub fn display_lists_to_pdf_with_cm_type1_and_metadata(
|
||||
pages: &[DisplayList],
|
||||
page_width: i64,
|
||||
page_height: i64,
|
||||
origin: PageOrigin,
|
||||
metadata: &PdfMetadata,
|
||||
) -> Vec<u8> {
|
||||
write_pdf(
|
||||
pages,
|
||||
page_width,
|
||||
page_height,
|
||||
origin,
|
||||
FontBackend::ComputerModern,
|
||||
metadata,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -685,6 +754,7 @@ fn write_pdf(
|
||||
page_height: i64,
|
||||
origin: PageOrigin,
|
||||
backend: FontBackend,
|
||||
metadata: &PdfMetadata,
|
||||
) -> Vec<u8> {
|
||||
let width = page_width;
|
||||
let height = page_height;
|
||||
@@ -981,6 +1051,14 @@ fn write_pdf(
|
||||
)
|
||||
.into_bytes();
|
||||
|
||||
let info_ref = match metadata_object(metadata) {
|
||||
Some(object) => {
|
||||
objects.push(object.into_bytes());
|
||||
format!(" /Info {} 0 R", objects.len())
|
||||
}
|
||||
None => String::new(),
|
||||
};
|
||||
|
||||
let mut pdf: Vec<u8> = b"%PDF-1.4\n".to_vec();
|
||||
let mut offsets = Vec::new();
|
||||
for (index, object) in objects.iter().enumerate() {
|
||||
@@ -998,8 +1076,9 @@ fn write_pdf(
|
||||
}
|
||||
pdf.extend_from_slice(
|
||||
format!(
|
||||
"trailer\n<< /Size {} /Root 1 0 R >>\nstartxref\n{}\n%%EOF\n",
|
||||
"trailer\n<< /Size {} /Root 1 0 R{} >>\nstartxref\n{}\n%%EOF\n",
|
||||
objects.len() + 1,
|
||||
info_ref,
|
||||
xref
|
||||
)
|
||||
.as_bytes(),
|
||||
@@ -1072,6 +1151,48 @@ mod type1_pdf_tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[test]
|
||||
fn metadata_writes_the_pdf_info_dictionary() {
|
||||
let pages = vec![DisplayList {
|
||||
width: 612 * 65536,
|
||||
height: 792 * 65536,
|
||||
items: Vec::new(),
|
||||
images: Vec::new(),
|
||||
}];
|
||||
let metadata = PdfMetadata {
|
||||
title: Some("Plain Title".into()),
|
||||
author: Some("Ada".into()),
|
||||
subject: None,
|
||||
keywords: None,
|
||||
};
|
||||
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("/Title (Plain Title)"));
|
||||
assert!(pdf.contains("/Author (Ada)"));
|
||||
assert!(pdf.contains("/Producer (texrra route-D)"));
|
||||
assert!(pdf.contains("/Info"));
|
||||
|
||||
let unicode = PdfMetadata {
|
||||
title: Some("Ωmega".into()),
|
||||
..PdfMetadata::default()
|
||||
};
|
||||
let pdf = String::from_utf8_lossy(&display_lists_to_pdf_with_cm_type1_and_metadata(
|
||||
&pages,
|
||||
612 * 65536,
|
||||
792 * 65536,
|
||||
PageOrigin::default(),
|
||||
&unicode,
|
||||
))
|
||||
.to_string();
|
||||
assert!(pdf.contains("/Title <FEFF03A9"));
|
||||
}
|
||||
|
||||
fn type1_backend_embeds_computer_modern_outlines() {
|
||||
let pdf = write(&[hello_list()]);
|
||||
let text = String::from_utf8_lossy(&pdf);
|
||||
|
||||
Reference in new issue
Block a user