use crate::analysis::pipeline::{Analysis, analyze};
use crate::backend::{PageRenderer, PdfFile};
use crate::config::AnalyzeOptions;
use crate::model::Meta;
use crate::render::{HtmlOptions, HtmlOutput, render};
use anyhow::Result;
pub struct Paper {
pdf: PdfFile,
source: Option<String>,
}
impl Paper {
pub fn from_bytes(data: Vec<u8>) -> Result<Self> {
Ok(Self {
pdf: PdfFile::open(data)?,
source: None,
})
}
pub fn with_source(mut self, source: impl Into<String>) -> Self {
self.source = Some(source.into());
self
}
pub fn page_count(&self) -> usize {
self.pdf.page_count()
}
pub fn analyze(&self) -> Analysis {
self.analyze_with(AnalyzeOptions::default())
}
pub fn analyze_with(&self, opts: AnalyzeOptions) -> Analysis {
let (raw, fonts) = self.pdf.extract(opts.jobs);
let meta = Meta {
source: self.source.clone(),
..Default::default()
};
let mut analysis = analyze(raw, &fonts, meta, opts);
if analysis.doc.meta.title.is_none() {
analysis.doc.meta.title = self.pdf.info_title();
}
analysis
}
pub fn renderer(&self) -> PageRenderer<'_> {
self.pdf.renderer()
}
pub fn to_html(&self, analysis: &Analysis, opts: &HtmlOptions) -> HtmlOutput {
render(analysis, &self.pdf, opts)
}
}
pub fn convert(data: Vec<u8>, opts: &HtmlOptions) -> Result<HtmlOutput> {
let paper = Paper::from_bytes(data)?;
let analysis = paper.analyze();
Ok(paper.to_html(&analysis, opts))
}