1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//! The document information dictionary (ISO 32000-2 §14.3.3).
use std::sync::Arc;
use laminapdf_cos::{Object, Result, Warning};
use crate::date::PdfDate;
use crate::document::{Document, object_number};
/// A date entry of the Info dictionary: the decoded string as stored, and the date when it
/// parses.
///
/// Added in 0.2.0.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct InfoDate {
/// The text string as stored (decoded to Unicode).
pub raw: String,
/// The parsed date; `None` when `raw` is not a valid PDF date ([`Warning::InvalidDate`]).
pub parsed: Option<PdfDate>,
}
/// The trailer's `/Info` dictionary: title, author and the other metadata entries, read
/// lazily. A field that is absent or not a string is `None`.
///
/// PDF 2.0 deprecates most of these in favour of XMP metadata (§14.3.2); reading XMP is a
/// later round.
///
/// Added in 0.2.0.
#[derive(Debug, Clone)]
pub struct Info<'d> {
doc: &'d Document,
dict: Arc<Object>,
object: Option<u32>,
}
impl<'d> Info<'d> {
pub(crate) fn new(doc: &'d Document) -> Result<Option<Self>> {
let Some(info) = doc.trailer().get("Info") else {
return Ok(None);
};
let object = object_number(info);
let dict = doc.read(info)?;
match dict {
Some(dict) if matches!(*dict, Object::Dict(_)) => Ok(Some(Self { doc, dict, object })),
// A missing object is `null` (§7.3.10): no Info, nothing to report.
Some(dict) if matches!(*dict, Object::Null) => Ok(None),
_ => {
doc.warn(Warning::InvalidEntry { object, key: "Info" })?;
Ok(None)
}
}
}
/// The text string at `key`, decoded (§7.9.2.2); `None` when absent or not a string.
///
/// # Errors
/// [`laminapdf_cos::Error::DataPending`] / [`laminapdf_cos::Error::Source`].
///
/// Added in 0.2.0.
pub fn text(&self, key: &str) -> Result<Option<String>> {
match self.dict.as_dict() {
Some(d) => self.doc.text_entry(d, key),
None => Ok(None),
}
}
/// Every key of the dictionary, as written, in the file's order: the standard ones and any
/// custom key a producer added.
///
/// Added in 0.2.0.
#[must_use]
pub fn keys(&self) -> Vec<String> {
self.dict
.as_dict()
.map(|d| {
d.iter()
.map(|(k, _)| String::from_utf8_lossy(k.as_bytes()).into_owned())
.collect()
})
.unwrap_or_default()
}
/// `/Title`.
///
/// # Errors
/// As [`Info::text`].
///
/// Added in 0.2.0.
pub fn title(&self) -> Result<Option<String>> {
self.text("Title")
}
/// `/Author`.
///
/// # Errors
/// As [`Info::text`].
///
/// Added in 0.2.0.
pub fn author(&self) -> Result<Option<String>> {
self.text("Author")
}
/// `/Subject`.
///
/// # Errors
/// As [`Info::text`].
///
/// Added in 0.2.0.
pub fn subject(&self) -> Result<Option<String>> {
self.text("Subject")
}
/// `/Keywords`.
///
/// # Errors
/// As [`Info::text`].
///
/// Added in 0.2.0.
pub fn keywords(&self) -> Result<Option<String>> {
self.text("Keywords")
}
/// `/Creator`: the application that created the original document.
///
/// # Errors
/// As [`Info::text`].
///
/// Added in 0.2.0.
pub fn creator(&self) -> Result<Option<String>> {
self.text("Creator")
}
/// `/Producer`: the application that converted it to PDF.
///
/// # Errors
/// As [`Info::text`].
///
/// Added in 0.2.0.
pub fn producer(&self) -> Result<Option<String>> {
self.text("Producer")
}
/// `/CreationDate`.
///
/// # Errors
/// As [`Info::date`].
///
/// Added in 0.2.0.
pub fn creation_date(&self) -> Result<Option<InfoDate>> {
self.date("CreationDate")
}
/// `/ModDate`.
///
/// # Errors
/// As [`Info::date`].
///
/// Added in 0.2.0.
pub fn mod_date(&self) -> Result<Option<InfoDate>> {
self.date("ModDate")
}
/// The date at `key`: the raw string, and its parsed form when valid. An invalid date
/// keeps the raw string and raises [`Warning::InvalidDate`].
///
/// # Errors
/// [`laminapdf_cos::Error::Strict`] in strict mode for an invalid date; as [`Info::text`].
///
/// Added in 0.2.0.
pub fn date(&self, key: &str) -> Result<Option<InfoDate>> {
let Some(raw) = self.text(key)? else {
return Ok(None);
};
let parsed = raw.parse::<PdfDate>().ok();
if parsed.is_none() {
// Warnings are deduplicated by entry too, so a bad /CreationDate does not hide a
// bad /ModDate.
let key = match key {
"CreationDate" => "CreationDate",
"ModDate" => "ModDate",
_ => "other",
};
self.doc.warn(Warning::InvalidDate {
object: self.object,
key,
})?;
}
Ok(Some(InfoDate { raw, parsed }))
}
}