use std::io::Write;
use clap::Parser;
use pdf_stamp::{
FontSource, FreshnessCheck, InvisibleMetadata, Options, Placement, Recipient, VisibleWatermark,
personalise,
};
#[derive(Copy, Clone, Debug, PartialEq, clap::ValueEnum)]
enum PlacementArg {
Footer,
Diagonal,
}
#[derive(Parser, Debug)]
#[command(author, version, about)]
struct Args {
#[arg(short, long)]
input: std::path::PathBuf,
#[arg(short, long)]
output: String,
#[arg(long, default_value = "")]
name: String,
#[arg(long)]
identifier: String,
#[arg(long)]
email: Option<String>,
#[arg(long, value_enum, default_value_t = PlacementArg::Footer)]
placement: PlacementArg,
#[arg(long)]
template: Option<String>,
#[arg(long)]
font_size: Option<f32>,
#[arg(long, conflicts_with = "no_embed_font")]
font: Option<std::path::PathBuf>,
#[arg(long)]
no_embed_font: bool,
#[arg(long)]
no_visible: bool,
#[arg(long)]
freshness_url: Option<String>,
#[arg(long, requires = "freshness_url")]
doc_id: Option<String>,
#[arg(long, requires = "freshness_url")]
doc_version: Option<String>,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let args = Args::parse();
let recipient = Recipient {
name: args.name,
identifier: args.identifier,
email: args.email,
};
let freshness = match (args.freshness_url, args.doc_id, args.doc_version) {
(Some(url), Some(id), Some(version)) => Some(FreshnessCheck::new(url, id, version)),
_ => None,
};
let font = match &args.font {
Some(path) => Some(std::fs::read(path)?),
None => None,
};
let visible = (!args.no_visible).then(|| {
let mut wm = match args.placement {
PlacementArg::Footer => VisibleWatermark::footer(),
PlacementArg::Diagonal => VisibleWatermark {
placement: Placement::Diagonal,
..VisibleWatermark::default()
},
};
if let Some(t) = args.template {
wm.template = t;
}
if let Some(s) = args.font_size {
wm.font_size = s;
}
wm.font = match font {
Some(bytes) => FontSource::Supplied(std::sync::Arc::new(bytes)),
None if args.no_embed_font => FontSource::None,
None => FontSource::Bundled,
};
wm
});
let options = Options {
visible,
invisible: InvisibleMetadata {
recipient_id: true,
recipient_email: recipient.email.is_some(),
stamped_at: true,
},
freshness,
stamp_creation_date: false,
};
let bytes = std::fs::read(&args.input)?;
let out = personalise(&bytes, &recipient, &options)?;
if args.output == "-" {
std::io::stdout().write_all(&out)?;
} else {
std::fs::write(&args.output, &out)?;
eprintln!(
"Stamped {} bytes for {} into {}",
out.len(),
recipient.identifier,
args.output
);
}
Ok(())
}