use lopdf;
use crate::OffsetDateTime;
use {
IccProfileType, PdfConformance, XmpMetadata, DocumentInfo, IccProfile
};
use glob_defines::ICC_PROFILE_ECI_V2;
#[derive(Debug, Clone)]
pub struct PdfMetadata {
pub creation_date: OffsetDateTime,
pub modification_date: OffsetDateTime,
pub metadata_date: OffsetDateTime,
pub document_title: String,
pub author: String,
pub creator: String,
pub producer: String,
pub keywords: Vec<String>,
pub subject: String,
pub identifier: String,
pub trapping: bool,
pub document_version: u32,
pub conformance: PdfConformance,
pub xmp_metadata: XmpMetadata,
pub document_info: DocumentInfo,
pub target_icc_profile: Option<IccProfile>,
}
impl PdfMetadata {
pub fn new<S>(title: S, document_version: u32, trapping: bool, conformance: PdfConformance)
-> Self where S: Into<String>
{
let current_time = OffsetDateTime::now_utc();
Self {
creation_date: current_time.clone(),
modification_date: current_time.clone(),
metadata_date: current_time.clone(),
document_title: title.into(),
author: String::new(),
creator: String::new(),
producer: String::new(),
keywords: Vec::new(),
subject: String::new(),
identifier: String::new(),
trapping: trapping,
document_version: document_version,
conformance: conformance,
xmp_metadata: XmpMetadata::new(Some("default".into()), 1),
document_info: DocumentInfo::new(),
target_icc_profile: None,
}
}
pub fn into_obj(self)
-> (Option<lopdf::Object>, lopdf::Object, Option<IccProfile>)
{
let metadata = self.clone();
let xmp_obj = {
if self.conformance.must_have_xmp_metadata() {
Some(self.xmp_metadata.into_obj(&metadata))
} else {
None
}
};
let doc_info_obj = self.document_info.into_obj(&metadata);
let icc_profile = {
if self.conformance.must_have_icc_profile() {
match self.target_icc_profile {
Some(icc) => Some(icc),
None => Some(IccProfile::new(ICC_PROFILE_ECI_V2.to_vec(), IccProfileType::Cmyk)
.with_alternate_profile(false)
.with_range(true)),
}
} else {
None
}
};
(xmp_obj, doc_info_obj, icc_profile)
}
}