Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added extract_text_by_page() #73

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2090,6 +2090,25 @@ pub fn print_metadata(doc: &Document) {
dlog!("Type: {:?}", get_pages(&doc).get(b"Type").and_then(|x| x.as_name()).unwrap());
}

pub fn extract_text_by_page<P: std::convert::AsRef<std::path::Path>>(path: P, page: u32) -> Result<String, OutputError> {
let mut s = String::new();
{
let mut output = PlainTextOutput::new(&mut s);
let doc = Document::load(path)?;
output_doc_page(&doc, &mut output, page)?;
}
return Ok(s);
}

pub fn extract_text_from_mem_by_page(buffer: &[u8], page: u32) -> Result<String, OutputError> {
let mut s = String::new();
{
let mut output = PlainTextOutput::new(&mut s);
let doc = Document::load_mem(buffer)?;
output_doc_page(&doc, &mut output, page)?;
}
return Ok(s);
}
/// Extract the text from a pdf at `path` and return a `String` with the results
pub fn extract_text<P: std::convert::AsRef<std::path::Path>>(path: P) -> Result<String, OutputError> {
let mut s = String::new();
Expand Down Expand Up @@ -2154,3 +2173,35 @@ pub fn output_doc(doc: &Document, output: &mut dyn OutputDev) -> Result<(), Outp
}
Ok(())
}

pub fn output_doc_page(doc: &Document, output: &mut dyn OutputDev, page: u32) -> Result<(), OutputError> {
if let Ok(_) = doc.trailer.get(b"Encrypt") {
eprintln!("Encrypted documents are not currently supported: See https://github.com/J-F-Liu/lopdf/issues/168")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this was copied from an older version of output_doc?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, its just to pass the page number wanted and to remove the for loop that runs through each page. I needed access to the text per page in a set of documents for a vector DB I'm writing right now. Also, I was wondering if I could write a prelude module for this because currently the Document struct from lopdf is not accessible unless the user adds the same version to their own Cargo.toml. If the user does not import the crate and call output_doc_page() with a reference to the loaded Document struct, then they are forced to call extract_text_by_page which forces a read from file every time they want to read a page. So if I made a prelude module to include the appropriate crates as accessible to the user, it would resolve the issue. I'll submit that in a different pull request if you want.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe accept a reference to a document instead of path will be better? Since if we have multiple page want to extract from same document, does't require load the document every time.

}
let empty_resources = &Dictionary::new();

let pages = doc.get_pages();
let mut p = Processor::new();
if let Some(dict) = pages.get(&page) {
let page_num = dict.0;
let page_dict = doc.get_object(*dict).unwrap().as_dict().unwrap();
dlog!("page {} {:?}", page_num, page_dict);
// XXX: Some pdfs lack a Resources directory
let resources = get_inherited(doc, page_dict, b"Resources").unwrap_or(empty_resources);
dlog!("resources {:?}", resources);

// pdfium searches up the page tree for MediaBoxes as needed
let media_box: Vec<f64> = get_inherited(doc, page_dict, b"MediaBox").expect("MediaBox");
let media_box = MediaBox { llx: media_box[0], lly: media_box[1], urx: media_box[2], ury: media_box[3] };

let art_box = get::<Option<Vec<f64>>>(&doc, page_dict, b"ArtBox")
.map(|x| (x[0], x[1], x[2], x[3]));

output.begin_page(page_num, &media_box, art_box)?;

p.process_stream(&doc, doc.get_page_content(*dict).unwrap(), resources,&media_box, output, page_num)?;

output.end_page()?;
}
Ok(())
}