pmaupin / pdfrw

pdfrw is a pure Python library that reads and writes PDFs
Other
1.86k stars 271 forks source link

How do I load an existing pdf implementation and replace strings in the document without changing the original layout #247

Open 2622594863 opened 3 months ago

2622594863 commented 3 months ago

How do I load an existing pdf implementation and replace strings in the document without changing the original layout

hoehermann commented 3 months ago

You may be looking for something like https://github.com/JoshData/pdf-redactor.

sl2c commented 2 months ago

Preserving layout, a.k.a. reflow, is a non-trivial operation. Take a look at pdfrwx whose classes provide full support for stream decompression (all PDF stream filters are supported) and parsing, which transforms a PDF stream into an abstract syntax tree (AST; see examples). After that you can focus on what exactly you want to do with the text by working directly with the AST. Once the AST has been edited, you can re-encoded it as stream and save the file.