Skip to main content

read_pdf

Function read_pdf 

Source
pub async fn read_pdf(pdf_path: impl AsRef<Path>) -> Result<Vec<String>>
Expand description

Read text from a PDF file

This function converts a PDF document to images (using pdftoppm from poppler-utils) and performs OCR on each page. Each page is treated as a separate image.

§Arguments

  • pdf_path - Path to the PDF file

§Returns

Returns a Result<Vec<String>> where each element contains the recognized text from the corresponding page.

§Requirements

This function requires pdftoppm from the poppler-utils package to be installed:

  • Ubuntu/Debian: sudo apt-get install poppler-utils
  • macOS: brew install poppler
  • Fedora/RHEL: sudo dnf install poppler-utils

§Example

use monocr_onnx::read_pdf;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let pages = read_pdf("document.pdf").await?;
    for (i, text) in pages.iter().enumerate() {
        println!("=== Page {} ===\n{}", i + 1, text);
    }
    Ok(())
}