pub mod adapters;
#[cfg(feature = "cli")]
pub mod cli;
mod geometry;
mod metrics;
mod metrics_data;
mod native_text;
mod overlay;
mod overlay_font;
use std::fs::File;
use std::io::BufWriter;
use std::path::Path;
use lopdf::Document;
pub use adapters::Adapter;
pub use metrics::FontMetrics;
#[derive(Debug, Clone, PartialEq)]
pub struct TextBox {
pub text: String,
pub x: f64,
pub y: f64,
pub width: f64,
pub height: f64,
pub confidence: f64,
pub rotation: f64,
}
impl TextBox {
pub fn new(text: impl Into<String>, x: f64, y: f64, width: f64, height: f64) -> TextBox {
TextBox {
text: text.into(),
x,
y,
width,
height,
confidence: 1.0,
rotation: 0.0,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct ParsedPage {
pub page_number: u32,
pub boxes: Vec<TextBox>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Options {
pub min_confidence: f64,
pub font: FontMetrics,
pub sanitize_unsupported: bool,
}
impl Default for Options {
fn default() -> Options {
Options {
min_confidence: 0.0,
font: FontMetrics::default(),
sanitize_unsupported: true,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Stats {
pub pages: usize,
pub ocr_pages: Vec<u32>,
pub items_drawn: usize,
}
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("{path}: {source}")]
Io {
path: String,
#[source]
source: std::io::Error,
},
#[error("not a readable PDF: {0}")]
Pdf(#[from] lopdf::Error),
#[error("encrypted PDFs are not supported")]
Encrypted,
#[error("not valid JSON: {0}")]
Json(#[from] serde_json::Error),
#[error("unknown adapter {0:?}: expected 'liteparse'")]
UnknownAdapter(String),
#[error("unknown font {0:?}: expected 'helv', 'tiro', or 'cour'")]
UnknownFont(String),
}
pub fn write_text_layer(
input: impl AsRef<Path>,
pages: &[ParsedPage],
output: impl AsRef<Path>,
options: &Options,
) -> Result<Stats, Error> {
let input = input.as_ref();
let output = output.as_ref();
let pdf = std::fs::read(input).map_err(|source| io_error(input, source))?;
let (mut document, stats) = overlay_document(&pdf, pages, options)?;
drop(pdf);
let mut file = BufWriter::new(File::create(output).map_err(|source| io_error(output, source))?);
document.save_to(&mut file).map_err(|source| io_error(output, source))?;
file.into_inner()
.map_err(|error| io_error(output, error.into_error()))?;
Ok(stats)
}
pub fn make_searchable_pdf(
input: impl AsRef<Path>,
source: impl AsRef<Path>,
output: impl AsRef<Path>,
adapter: Adapter,
options: &Options,
) -> Result<Stats, Error> {
let pages = adapter.parse_file(source)?;
write_text_layer(input, &pages, output, options)
}
pub fn add_text_layer(pdf: &[u8], pages: &[ParsedPage], options: &Options) -> Result<(Vec<u8>, Stats), Error> {
let (mut document, stats) = overlay_document(pdf, pages, options)?;
let mut output = Vec::with_capacity(pdf.len() + pdf.len() / 8);
document
.save_to(&mut output)
.map_err(|source| io_error(Path::new("<memory>"), source))?;
Ok((output, stats))
}
fn overlay_document(pdf: &[u8], pages: &[ParsedPage], options: &Options) -> Result<(Document, Stats), Error> {
let mut document = Document::load_mem(pdf)?;
if document.trailer.has(b"Encrypt") || document.was_encrypted() {
return Err(Error::Encrypted);
}
document.catalog()?;
let stats = overlay::Overlay::new(&mut document, options).run(&mut document, pages)?;
Ok((document, stats))
}
pub(crate) fn io_error(path: &Path, source: std::io::Error) -> Error {
Error::Io {
path: path.display().to_string(),
source,
}
}