64 Commits

Author SHA1 Message Date
Jeremy Singer-Vine 7e364e6193 Add Page.trimbox, .bleedbox, .artbox (#1313)
Thanks to @samuelbradshaw for the suggestion!
2025-06-11 23:26:26 -04:00
Jeremy Singer-Vine e15ed98a26 Fix bug w/ use_text_flow=True extractions (#1279)
... related to flows where text bounces between lines.

h/t @samuelbradshaw
2025-03-27 22:44:25 -04:00
Jeremy Singer-Vine f2ad9423fa Add another oss-fuzz test case, already fixed 2025-03-27 22:00:20 -04:00
Jeremy Singer-Vine 748ff31dd9 More broadly handle RecursionError, via oss-fuzz 2025-03-27 21:58:17 -04:00
Jeremy Singer-Vine 9148810a8f Fix unhandled None in do_PDFStream, via oss-fuzz 2025-03-27 21:57:21 -04:00
Jeremy Singer-Vine 43ccc5b260 Catch exceptions from pdfminer and malformed PDFs
... thanks to OSS-Fuzz and @ennamarie19

Cf.: https://github.com/google/oss-fuzz/pull/12949
2025-02-09 16:25:30 -05:00
Michal Stolarczyk c80c78dcf9 add a test to cover raise_unicode_errors parameter 2024-11-22 16:48:19 +01:00
Jeremy Singer-Vine 9025c3fc56 Fix object coordinates when mediabox != (0,0,...)
See issue #1181 for details.
2024-08-05 12:28:42 -04:00
Jeremy Singer-Vine 03a477f7f0 Add pdfplumber.open(unicode_norm=...)
Allows user to pre-normalize Unicode characters.

h/t @petermr + @agusluques in #905
2024-08-04 14:11:58 -04:00
QAN 76752c4a31 add ignore_char_properties arg in dedup_chars 2024-06-27 11:40:35 +02:00
Jeremy Singer-Vine 4daf0aa1b9 Handle PDFDocEncoding failures in decode_text
Fixes issue #1147
2024-06-11 16:21:45 -04:00
Jeremy Singer-Vine aaa35c9c6b Fix coordinates of annots on rotated pages
PDFs represent annotation coordinates independently from page rotation,
so annot coordinates have to be adjusted.
2024-06-11 14:52:03 -04:00
Jeremy Singer-Vine 850fd45b02 Add {line,char}_dir{,rotated,render} params
... and in the process, deeply rewrite the text extraction code to be
more flexible with regards to reading/writing non top-to-bottom,
left-to-right text.

Major kudos and thanks to @afriedman412 for the initial idea, initial
prototype, and inspiration for these new features.

Breaking change: word["direction"] is no longer {1,-1} but instead
{"ltr","rtl","ttb","btt"}.

Deprecated: Text-extraction parameters `horizontal_ltr` and
`vertical_ttb`.
2024-03-06 20:53:03 -05:00
Jeremy Singer-Vine 07d9997ee5 Standardize handling of cropbox
... fixing various issues with PageImage. Also adds
force_mediabox parameter to Page.to_image(...).

Thanks to @stefanw for flagging:
    https://github.com/jsvine/pdfplumber/issues/1054
2024-01-05 10:14:44 -05:00
Jeremy Singer-Vine fff846b073 Merge pull request #1041 from afriedman412/x_tolerance_ratio
Add `x_tolerance_ratio` param to `extract_text` and similar functions
2023-11-09 15:18:27 -05:00
Jeremy Singer-Vine 35ed9e0695 Merge pull request #963 from dhdaines/structure_tree
Support for PDF 1.3 logical structure
2023-11-09 15:08:09 -05:00
Andy Friedman 5a85ab7a0c add x_tolerance_ratio 2023-11-01 13:54:10 -04:00
Jeremy Singer-Vine 0bfffc2448 Fix Page.get_textmap caching for extra_attrs=[...]
... by preconverting list kwargs to tuples.
2023-10-27 10:53:17 -04:00
David Huggins-Daines ed707a7ec6 fix: respect use_text_flow in extract_text (fixes #982) 2023-09-06 15:51:50 -04:00
David Huggins-Daines 036044d92e fix: handle more MCR/OBJR madness 2023-09-05 23:47:23 -04:00
David Huggins-Daines 14f9a67c66 fix: complete coverage and fix handling of OBJR/MCR 2023-09-05 23:03:17 -04:00
David Huggins-Daines 8d485c38cd test: trivial synthetic pdf for completing structure coverage 2023-09-05 23:03:03 -04:00
David Huggins-Daines 3c83366d9d feat: extract structure tree from pages or documents 2023-08-19 09:49:48 -06:00
David Huggins-Daines 142fc90cc5 Support for marked content section IDs (#961) 2023-08-19 11:29:21 -04:00
Jeremy Singer-Vine 30a52cb0ed Add PDF.path & fix .to_image() for zipped files
These are related changes, since the previous approach of using
`PDF.stream.name` to distinguish between on-path and filelike-object PDFs
doesn't work for zipped files (and possibly other filelike-objects that
have a `name` property).
2023-07-29 14:30:00 -04:00
Jeremy Singer-Vine db6ae97bfb Add pdfplumber.repair(...) and .open(repair=True)
This commit adds convenience methods to repair PDFs on the fly and/or to
write repaired PDFs to disk.

Currently, this does so via Ghostscript using the method we've asked
many users to try by following the instructions at
https://superuser.com/questions/278562/how-can-i-fix-repair-a-corrupted-pdf-file

Now, hopefully, this saves folks a few steps.
2023-07-16 17:07:42 -04:00
Jeremy Singer-Vine 474f74c906 Add another test for use_text_flow 2023-07-01 18:41:58 -04:00
Jeremy Singer-Vine 86e935d681 By default, expand ligatures into their letters
Addresses issue #598
2023-04-13 08:13:16 -04:00
Jeremy Singer-Vine 6f6b465260 Add .curve_edges, use by default in table-detect.
Most of the groundwork was already there to add a PDF/Page.curve_edges
property. And, inspired, by
https://github.com/jsvine/pdfplumber/discussions/858 and related issues,
we now include 0/90/180/270-degree oriented curve segments into the
default table-detection strategy. As before, you can still switch to the
"lines_strict" to use only lines defined as such (rather than also using
rect and curve edges).
2023-04-13 08:13:16 -04:00
Jeremy Singer-Vine 9441ff7628 Handle bytes-typed fontnames (fixes #461 + #842)
Came across this bit of code, which helps to solve some of the mystery
in issues #461 and #842:

https://git.ghostscript.com/?p=mupdf.git;a=blob;f=source/pdf/pdf-font.c;h=6322cedf2c26cfb312c0c0878d7aff97b4c7470e;hb=HEAD#l774

Now, for every char's fontname, we:

- Check whether its a `str` or `byte`
    - If the latter, we check whether it's one of the well-known codes from
      the link above
        - If so, we use that (preserving the part, if present, before
          the `+`)
        - If not, we just cast to str
2023-04-13 08:13:16 -04:00
Shannon Shen 5193c26c31 Consider punctuation when extracting words (#682)
Adds an optional argument to the `extract_words` function that allows to enforce breaking tokens at punctuation. PR by @lolipopshock.
2022-07-16 09:58:10 -04:00
Jeremy Singer-Vine b21ae179fe Add tests for PR 581
We cannot test whether the file closed successfully, but we can test
that the code reaches that branch of the logic.
2022-01-13 23:21:43 -05:00
Jeremy Singer-Vine d235d4bbc4 Add experimental .extract_text(layout=True)
See the docstring in utils.words_to_layout for details on the
implementation.

Addresses issue #10 and related issues.
2021-11-02 23:12:17 -04:00
Jeremy Singer-Vine df98f9cf44 Handle utf-16-encoded annotations (#463)
Thanks to @tungph for the fix proposal.
2021-10-15 09:34:39 -04:00
bobluda 6cea72da22 Add test and pdf for mixed strategy table extraction 2021-07-11 23:59:27 +02:00
Jeremy Singer-Vine 83dd31f7dc Add test for extract_table(s) re. text extraction 2021-05-08 17:42:12 -04:00
Samkit Jain a17c5a72b6 Sort tables extracted on a page by their top position
Fixes #336
h/t @gqh1995 for reporting
2021-01-21 20:05:29 +05:30
Samkit Jain 9f0a1ad7cd Treat invalid/unparseable metadata values as warnings
Certain invalid values if parseable don't throw a warning and only unparseable (always invalid) throw
2020-11-26 21:58:49 +05:30
Samkit Jain 3030765036 Add test for issue #297
h/t @prgx-csmith01 for providing the PDF
2020-10-30 12:47:27 +05:30
Samkit Jain b132d45db5 Fix bug in dedup_chars() in which ._objects was accessed before assignment
Fixes #293
2020-10-20 17:02:07 +05:30
Jeremy Singer-Vine 04fd56ac40 Add utils/page.dedupe_chars(...) (#71 + #280)
h/t @xv44586 for the initial inspiration 👍

These new methods return a version of the chars/page with duplicate
chars — those sharing the same text, fontname, size, and positioning
(within `tolerance` x/y) as other characters — removed.
2020-10-03 12:06:48 -04:00
Jeremy Singer-Vine 84f91811df Rework .extract_words and add use_text_flow param
Taking @samkit-jain's smart question about sort-order into account, this
commit reworks `.extract_words`, making the sorting more explicit, and
adding an option to *not* sort, via `use_text_flow=True`, which follows
the original PDF's text flow, rather than presorting all characters top-down,
left-right.

To do so required rethinking the word-delineation logic,
which in turn motivated a shift toward a more object-oriented approach
(especially to keep track of settings without passing them from function
to function). Now `.extract_text`'s logic is encapsulated in a new class,
`TextExtractor`.
2020-08-30 18:36:03 -04:00
Jeremy Singer-Vine 917467a10c Remove test_issues.py:test_pr_77 to speed up tests
The test, responding to PR #77, was particularly slow and the
accompanying PDF somewhat large. Now that the fixes to the issues
identified in the PR are fairly tightly integrated into pdfplumber, we
can remove the tests with reasonable confidence, speeding up development
time.
2020-08-29 13:31:42 -04:00
Jeremy Singer-Vine a5e7d7fa52 Remove pandas from dev requirements and tests
Results in needing one more # pragma: nocover statement, but on a
simple line of code. See PR #253 for details and motivation.
2020-08-15 12:03:34 -04:00
Jeremy Singer-Vine cbc91c67d0 Add convert.py/.to_json/.to_csv & improve testcov
Moves most of the logic previously in cli.py to convert.py, for usage by
other submodules. Adds Container.to_json and Container.to_csv. Makes
adjustments/fixes to other parts of the library, based on edge-cases
encountered (such as infinite recursion in anntations).
2020-08-13 08:37:46 -04:00
Jeremy Singer-Vine d16aa13ca2 Fix utils.extract_words & .resize_object, + tests
- Fixes `.extract_words`, which had been returning incorrect results when `horizontal_ltr = False`
- Fixes `.resize_object`, which had been failing in various permutations
- Brings utils.py test coverage to 100%
2020-08-01 12:27:07 -04:00
Jeremy Singer-Vine aa03961291 Add .annots and .hyperlinks, replacing .annos
.annos was non-functional, based on a misunderstanding of how
Annotation objects were represented in the PDF object. Also shifts
language from "annos" to "annots" to mirror pdfminer's nomenclature.
2020-07-31 18:02:04 -04:00
Jeremy Singer-Vine 85f422d001 Fix bug in pdfminer.six's .resolve_all
See https://github.com/jsvine/pdfplumber/issues/203

Root cause of decimalization bug flagged in that issue was
pdfminer.six's .resolve_all method not recusively resolving items within
tuples. Now we define our own method, with some slight optimization
tweaks.
2020-04-29 08:31:39 -04:00
Jeremy Singer-Vine 6c2e474490 Update pdfminer.six/pillow reqs & bump to v0.5.15 2020-01-05 21:33:49 -05:00
Jeremy Singer-Vine 4ef679d6d7 Add test for PR 136 and bump version 2019-08-29 19:05:23 -04:00