How DPI Settings Control OCR Rendering and Screenshot Generation in LiteParse
The dpi field in LiteParseConfig determines the resolution of raster images used for both OCR text extraction and screenshot generation, defaulting to 150 DPI but configurable via CLI flags or code initialization.
LiteParse uses a unified rasterization pipeline to convert PDF pages into bitmap images for optical character recognition and visual snapshots. The DPI settings in LiteParse serve as the single control point that dictates image resolution across both the OCR merge module and the screenshot functionality. By adjusting this value, developers trade off between OCR accuracy and processing speed, or control the fidelity of exported PNG screenshots.
Where DPI Configuration Lives in LiteParse
LiteParse centralizes rasterization parameters in the LiteParseConfig struct defined in crates/liteparse/src/config.rs. The configuration exposes a single dpi field that controls rendering resolution for all bitmap operations:
pub struct LiteParseConfig {
…
/// DPI for rendering pages (used for OCR and screenshots).
pub dpi: f32,
…
}
The default implementation sets 150.0 DPI as the baseline value. This default strikes a balance between recognition quality and memory consumption, but users can override it programmatically or through command-line arguments.
How DPI Settings Drive OCR Rendering
When LiteParse encounters pages requiring OCR—either due to sparse native text or embedded image content—it invokes the render_pages_for_ocr function in crates/liteparse/src/ocr_merge.rs. This function passes the configured DPI directly to the PDFium rendering engine:
let bitmap = page_obj.render(dpi)?;
The page_obj.render(dpi) call forwards the resolution parameter to pdfium::Page::render in crates/pdfium/src/page.rs, which rasterizes the PDF page at the specified dots-per-inch. The resulting bitmap converts to an RGB8 image buffer that feeds directly into the OCR engine.
Higher DPI values yield finer glyph details, improving recognition accuracy for small fonts or dense document layouts. Conversely, lower values reduce memory allocation and CPU cycles during the OCR phase, though potentially at the cost of missing fine text details.
DPI Control for Screenshot Generation
The public screenshot helper in crates/liteparse/src/render.rs utilizes the identical DPI pipeline. When generating visual snapshots, LiteParse calls:
let bitmap = page.render(dpi)?;
This renders the page to a bitmap at the same resolution used for OCR, then encodes the image as PNG and writes it to the user-specified output path. The implementation logs the effective DPI during execution ([rust-bin] rendered page … at {dpi} DPI), providing visibility into the actual rendering resolution.
Because screenshots share the pdfium::Page::render code path with OCR preparation, visual snapshots maintain consistent fidelity with the text extraction pipeline.
Propagating DPI Values Through the CLI and Bindings
Both the Rust binary and language bindings expose the DPI setting through the --dpi flag or equivalent configuration option. In crates/liteparse/src/main.rs, the CLI parser captures the flag value and stores it in LiteParseConfig:
// CLI parsing stores user-provided DPI
cmd.dpi
When executing a screenshot command, the value flows from CLI argument → LiteParseConfig → render::screenshot → pdfium::Page::render:
liteparse screenshot input.pdf 1 --dpi 300 --output out.png
The Node.js, Python, and WASM bindings mirror this pattern, accepting a dpi option in their respective configuration objects and forwarding it to the core Rust library.
Why LiteParse Uses a Single DPI Setting
LiteParse treats OCR rendering and screenshot generation as identical rasterization steps. Both originate from a pdfium::Page object and invoke render(dpi). This architectural decision eliminates duplicated rendering logic and guarantees that OCR engines process images at the same visual fidelity that users see in exported screenshots.
The unified approach ensures consistency: when OCR extracts text from a page rendered at 300 DPI, a screenshot of that same page at 300 DPI will contain the identical pixel representation of the underlying content.
Summary
- Configuration location: The
dpifield inLiteParseConfig(crates/liteparse/src/config.rs) controls resolution, defaulting to 150. - OCR impact: Higher values in
ocr_merge.rsimprove text recognition accuracy for small fonts; lower values reduce resource consumption. - Screenshot parity: The
screenshotfunction inrender.rsuses the same DPI parameter to ensure visual consistency with OCR data. - Propagation path: CLI flags and language bindings feed into
LiteParseConfig, which passes values topdfium::Page::renderincrates/pdfium/src/page.rs. - Unified pipeline: A single DPI setting prevents divergence between extracted text coordinates and visual snapshots.
Frequently Asked Questions
What is the default DPI in LiteParse?
LiteParse defaults to 150.0 DPI as defined in the Default implementation of LiteParseConfig in crates/liteparse/src/config.rs. This value provides adequate resolution for standard document OCR while minimizing memory overhead during batch processing.
How does increasing DPI affect OCR accuracy?
Increasing the DPI setting improves OCR accuracy by producing higher-resolution bitmaps that preserve fine details in small fonts and complex glyphs. The render_pages_for_ocr function in crates/liteparse/src/ocr_merge.rs passes this value to PDFium, ensuring the OCR engine receives more pixel data per character. However, values above 300 DPI yield diminishing returns for standard documents while significantly increasing memory usage and processing time.
Can I set different DPI values for OCR and screenshots?
No. LiteParse intentionally uses a single dpi field in LiteParseConfig to govern both operations. The screenshot function in crates/liteparse/src/render.rs and the OCR pipeline in crates/liteparse/src/ocr_merge.rs both call page.render(dpi), ensuring that text extraction and visual snapshots remain pixel-aligned. If you require different resolutions, you must create separate parser instances with distinct configurations.
How do I change the DPI when using the Node.js or Python bindings?
All language bindings expose the dpi option through their configuration constructors. In Node.js, pass it during instantiation: new LiteParse({ dpi: 300 }). In Python, use the optional parameter when creating the parser instance or via the CLI: python -m liteparse screenshot sample.pdf 1 --dpi 250 --output page1.png. The bindings propagate this value to the underlying LiteParseConfig struct in the Rust core.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →