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
use crate::OffsetDateTime;
use lopdf;
use PdfConformance;
use utils::random_character_string_32;
#[derive(Debug, Clone)]
pub struct XmpMetadata {
pub document_id: String,
pub rendition_class: Option<String>,
pub document_version: u32,
}
impl XmpMetadata {
pub fn new(rendition_class: Option<String>, document_version: u32)
-> Self
{
let document_id: String = random_character_string_32();
Self {
document_id: document_id,
rendition_class: rendition_class,
document_version: document_version,
}
}
pub(in types) fn into_obj<S>(self,
conformance: PdfConformance,
trapping: bool,
creation_date: OffsetDateTime,
modification_date: OffsetDateTime,
metadata_date: OffsetDateTime,
document_title: S)
-> lopdf::Object where S: Into<String> + ::std::fmt::Display
{
use lopdf::{Stream as LoStream, Dictionary as LoDictionary};
use lopdf::Object::*;
use std::iter::FromIterator;
let trapping = if trapping { "True" } else { "False" };
let instance_id = random_character_string_32();
let create_date = to_pdf_xmp_date(creation_date);
let modification_date = to_pdf_xmp_date(modification_date);
let metadata_date = to_pdf_xmp_date(metadata_date);
let pdf_x_version = conformance.get_identifier_string();
let document_version = self.document_version.to_string();
let document_id = self.document_id.to_string();
let rendition_class = match self.rendition_class {
Some(class) => class,
None => "".to_string(),
};
let xmp_metadata = format!(include_str!("../../../templates/catalog_xmp_metadata.txt"),
create_date, modification_date, metadata_date, document_title, document_id,
instance_id, rendition_class, document_version, pdf_x_version, trapping);
Stream(LoStream::new(LoDictionary::from_iter(vec![
("Type", "Metadata".into()),
("Subtype", "XML".into()), ]),
xmp_metadata.as_bytes().to_vec() ))
}
}
fn to_pdf_xmp_date(date: OffsetDateTime)
-> String
{
format!("D:{:04}-{:02}-{:02}T{:02}:{:02}:{:02}+00'00'",
date.year(),
date.month(),
date.day(),
date.hour(),
date.minute(),
date.second(),
)
}