J-F-Liu / lopdf

A Rust library for PDF document manipulation.
MIT License
1.64k stars 176 forks source link

`.replace_text` does not work as intended. #223

Open jymchng opened 1 year ago

jymchng commented 1 year ago
L105    pdf.replace_text(2, "jdoe123@mycompany.net", "hello WORLD").unwrap();
L106    dbg!(pdf.extract_text(&[2]).unwrap());

Logs

[src\redact.rs:106] pdf.extract_text(&[2]).unwrap() = "For example, john.doe@example.com, jdoe123@mycompany.net, \nalice_123+test@gmail.co.uk, and jane\n-\ndoe@my\n-\nuniversity.edu all match this pattern, \nand are therefore considered valid email addresses.\n \n \n"

Apparently, directly replacing text in a page doesn't work?

jymchng commented 1 year ago

@J-F-Liu Hi J-F-Liu, just thinking about this replace_text method that returns a Result<()> - it means there is a contract between the caller and callee such that if replace_text indeed does replace the text in the .pdf, it returns an Ok(()), else it returns an Err variant.

For this function, particularly on Line 138, it seems that the function does nothing when the encoding is not within the pre-defined 'able-to-parse' encodings ("Tf" or "Tj"), the match arm _ => {} evaluates to an empty scope. Would it be better to return an Err so that the caller knows it is not getting what the function promises to do because it is unable to parse any other type of encodings?

jymchng commented 1 year ago

217

J-F-Liu commented 1 year ago

Yes, text processing is not implemented completely.

jymchng commented 7 months ago

@J-F-Liu Hi Liu, do you think this issue can be fixed?

Heinenen commented 3 months ago

Theoretically this can be fixed, but sadly, extracting text from a PDF is hard. The solution for #125 may lay a first foundation for solving this issue, as it will allow to (sometimes) extract the text from the PDF. However, this is only half of the solution, we would still need to implement putting the replacement text back into the PDF.