1pub mod adapters;
37#[cfg(feature = "cli")]
38pub mod cli;
39mod geometry;
40mod metrics;
41mod metrics_data;
42mod native_text;
43mod overlay;
44mod overlay_font;
45
46use std::fs::File;
47use std::io::BufWriter;
48use std::path::Path;
49
50use lopdf::Document;
51
52pub use adapters::Adapter;
53pub use metrics::FontMetrics;
54
55#[derive(Debug, Clone, PartialEq)]
60pub struct TextBox {
61 pub text: String,
62 pub x: f64,
63 pub y: f64,
64 pub width: f64,
65 pub height: f64,
66 pub confidence: f64,
68 pub rotation: f64,
71}
72
73impl TextBox {
74 pub fn new(text: impl Into<String>, x: f64, y: f64, width: f64, height: f64) -> TextBox {
76 TextBox {
77 text: text.into(),
78 x,
79 y,
80 width,
81 height,
82 confidence: 1.0,
83 rotation: 0.0,
84 }
85 }
86}
87
88#[derive(Debug, Clone, PartialEq)]
90pub struct ParsedPage {
91 pub page_number: u32,
93 pub boxes: Vec<TextBox>,
94}
95
96#[derive(Debug, Clone, PartialEq)]
98pub struct Options {
99 pub min_confidence: f64,
101 pub font: FontMetrics,
103 pub sanitize_unsupported: bool,
106}
107
108impl Default for Options {
109 fn default() -> Options {
110 Options {
111 min_confidence: 0.0,
112 font: FontMetrics::default(),
113 sanitize_unsupported: true,
114 }
115 }
116}
117
118#[derive(Debug, Clone, PartialEq, Eq)]
120pub struct Stats {
121 pub pages: usize,
123 pub ocr_pages: Vec<u32>,
125 pub items_drawn: usize,
127}
128
129#[derive(Debug, thiserror::Error)]
130pub enum Error {
131 #[error("{path}: {source}")]
132 Io {
133 path: String,
134 #[source]
135 source: std::io::Error,
136 },
137 #[error("not a readable PDF: {0}")]
138 Pdf(#[from] lopdf::Error),
139 #[error("encrypted PDFs are not supported")]
140 Encrypted,
141 #[error("not valid JSON: {0}")]
142 Json(#[from] serde_json::Error),
143 #[error("unknown adapter {0:?}: expected 'liteparse'")]
144 UnknownAdapter(String),
145 #[error("unknown font {0:?}: expected 'helv', 'tiro', or 'cour'")]
146 UnknownFont(String),
147}
148
149pub fn write_text_layer(
154 input: impl AsRef<Path>,
155 pages: &[ParsedPage],
156 output: impl AsRef<Path>,
157 options: &Options,
158) -> Result<Stats, Error> {
159 let input = input.as_ref();
160 let output = output.as_ref();
161 let pdf = std::fs::read(input).map_err(|source| io_error(input, source))?;
162 let (mut document, stats) = overlay_document(&pdf, pages, options)?;
163
164 drop(pdf);
166
167 let mut file = BufWriter::new(File::create(output).map_err(|source| io_error(output, source))?);
168 document.save_to(&mut file).map_err(|source| io_error(output, source))?;
169 file.into_inner()
170 .map_err(|error| io_error(output, error.into_error()))?;
171
172 Ok(stats)
173}
174
175pub fn make_searchable_pdf(
178 input: impl AsRef<Path>,
179 source: impl AsRef<Path>,
180 output: impl AsRef<Path>,
181 adapter: Adapter,
182 options: &Options,
183) -> Result<Stats, Error> {
184 let pages = adapter.parse_file(source)?;
185 write_text_layer(input, &pages, output, options)
186}
187
188pub fn add_text_layer(pdf: &[u8], pages: &[ParsedPage], options: &Options) -> Result<(Vec<u8>, Stats), Error> {
190 let (mut document, stats) = overlay_document(pdf, pages, options)?;
191 let mut output = Vec::with_capacity(pdf.len() + pdf.len() / 8);
192 document
193 .save_to(&mut output)
194 .map_err(|source| io_error(Path::new("<memory>"), source))?;
195
196 Ok((output, stats))
197}
198
199fn overlay_document(pdf: &[u8], pages: &[ParsedPage], options: &Options) -> Result<(Document, Stats), Error> {
200 let mut document = Document::load_mem(pdf)?;
201
202 if document.trailer.has(b"Encrypt") || document.was_encrypted() {
205 return Err(Error::Encrypted);
206 }
207
208 document.catalog()?;
210
211 let stats = overlay::Overlay::new(&mut document, options).run(&mut document, pages)?;
212
213 Ok((document, stats))
214}
215
216pub(crate) fn io_error(path: &Path, source: std::io::Error) -> Error {
217 Error::Io {
218 path: path.display().to_string(),
219 source,
220 }
221}