use crate::OffsetDateTime;
use lopdf;
use crate::utils::random_character_string_32;
use crate::PdfMetadata;
#[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,
rendition_class,
document_version,
}
}
pub(crate) fn into_obj(self, m: &PdfMetadata) -> lopdf::Object {
use lopdf::Object::*;
use lopdf::{Dictionary as LoDictionary, Stream as LoStream};
let trapping = if m.trapping { "True" } else { "False" };
let instance_id = random_character_string_32();
let create_date = to_pdf_xmp_date(&m.creation_date);
let modification_date = to_pdf_xmp_date(&m.modification_date);
let metadata_date = to_pdf_xmp_date(&m.metadata_date);
let pdf_x_version = m.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!("../assets/catalog_xmp_metadata.txt"),
create = create_date,
modify = modification_date,
mdate = metadata_date,
title = m.document_title,
id = document_id,
instance = instance_id,
class = rendition_class,
version = document_version,
pdfx = pdf_x_version,
trapping = trapping,
creator = m.creator,
subject = m.subject,
keywords = m.keywords.join(","),
identifier = m.identifier,
producer = m.producer
);
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(),
)
}