Commit Graph

597 Commits

Author SHA1 Message Date
Jeremy Singer-Vine 28f4ebeb3c Use pdfium's *public* (not private) render method
Thanks to @mara004 for flagging here:
https://github.com/jsvine/pdfplumber/discussions/899#discussioncomment-6520928
2023-07-23 18:19:50 -04:00
Jeremy Singer-Vine 90742bdcbc Bump to v0.10.1 v0.10.1 2023-07-19 08:58:41 -04:00
Jeremy Singer-Vine 7e2893106e Add antialias parameter to Page.to_image(...)
... and associated methods. Thanks to @cmdlineluser for flagging.

For details, see:
https://github.com/jsvine/pdfplumber/discussions/899#discussioncomment-6464765
2023-07-19 08:56:18 -04:00
Jeremy Singer-Vine f3c628ab54 Fix bug-report link to docs/repairing.md 2023-07-16 23:01:35 -04:00
Jeremy Singer-Vine 00386ad8ca Merge pull request #936 from jsvine/develop
v0.10.0
v0.10.0
2023-07-16 18:35:42 -04:00
Jeremy Singer-Vine 28c0afc35c Fix missing trailing parens in CHANGELOG.md 2023-07-16 18:30:29 -04:00
Jeremy Singer-Vine ab9164ba19 Update CITATION.cff 2023-07-16 18:25:36 -04:00
Jeremy Singer-Vine 9aa57dece4 Update CHANGELOG.md 2023-07-16 18:23:36 -04:00
Jeremy Singer-Vine c9d24d50b0 Remove support for Python 3.7 2023-07-16 18:22:58 -04:00
Jeremy Singer-Vine 34070b4985 Update .github/workflows/tests.yml 2023-07-16 18:10:14 -04:00
Jeremy Singer-Vine 00efbf02df Update pandas version in requirements-dev 2023-07-16 18:08:42 -04:00
Jeremy Singer-Vine b5c268d756 Fix PNG size tests for other platforms 2023-07-16 17:51:53 -04:00
Jeremy Singer-Vine 10ae47d024 Fix tuple/Tuple typesig for earlier Python versions 2023-07-16 17:45:24 -04:00
Jeremy Singer-Vine fbc6fac02f Merge branch 'develop' of github.com:jsvine/pdfplumber into develop 2023-07-16 17:39:43 -04:00
Jeremy Singer-Vine 70c25babf3 Bump to v0.10.0 2023-07-16 17:38:34 -04:00
Jeremy Singer-Vine 0cc1047816 Update CHANGELOG.md for v0.10.0 2023-07-16 17:34:35 -04:00
Jeremy Singer-Vine de65bd8698 Update examples 2023-07-16 17:33:25 -04:00
Jeremy Singer-Vine 3ab6649c03 Add repair notes to docs and bug report template 2023-07-16 17:18:46 -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 df0e027fc7 Remove tox from setup.cfg 2023-07-16 15:41:44 -04:00
Jeremy Singer-Vine 788857ed03 Add Python 3.11 to supported versions 2023-07-16 15:40:52 -04:00
Jeremy Singer-Vine b0493732c4 Replace Wand with pypdfium2 for page.to_image(...)
This commit swaps out Wand (and its non-Python dependencies ImageMagick
and Ghostscript) for pypdfium2 for PageImage rendering. This has some
advantages:

- Less finicky: Wand often caused users problems, due to "MagickWand
  shared library not found" and "PolicyError: not authorized `PDF'"
  issues. By contrast, pypdfium2 seems (at least at first) to more
  self-contained and not require any system-tweaking.
- Faster: pypdfium2 appears to render images more quickly than Wand (see
  @cmdlineuser's tests in #899)
- More flexible: pypdfium2 appears to generate images with greater color
  depth; by default, pdfplumber quantizes those images so that they
  save/display compactly (in fact, with smaller file sizes than the
  previous code), this commit also adds parameters to retain all/more of
  the original, more detailed colors.

Thanks to @cmdlineuser in #899 for the suggestion.
2023-07-16 10:46:40 -04:00
Jeremy Singer-Vine ea5e2753df Move expanded notes on colors to docs/colors.md 2023-07-14 12:21:19 -04:00
Jeremy Singer-Vine 6d62054ffa Remove vestigial 'font' and 'name' properties 2023-07-14 12:01:28 -04:00
Jeremy Singer-Vine 97ca4b0bf3 Extract and handle patterns + (some) color spaces
Inspired by https://github.com/jsvine/pdfplumber/issues/828

The PDF reference allows for "colors" to be defined as a series of
numbers and/or (much less commonly) patterns.

(See p. 288 and section 4.6 here:
https://ghostscript.com/~robin/pdf_reference17.pdf)

This commit separates out the pattern component of colors into their own
attributes, `stroking_pattern` and `non_stroking_pattern` so that they
don't muddle the interpretation of standard colors' tuple-of-numbers
representation.

This commit also adds code that attempts to fetch the `ncs`/`scs` color
space of each object. Due to current limitations of pdfminer.six,
however, the only such color space immediately available is the `ncs`
(non-stroking color space) property of char objects.
2023-07-04 11:24:55 -04:00
Jeremy Singer-Vine 3772af60c7 Add Page.find_table(...) (#873)
Previously, `pdfplumber.Page` had these table-getting methods:

- `.find_tables(...)`
- `.extract_tables(...)`
- `.extract_table(...)`

For consistency/completeness's sake, this commit adds:

- `.find_table(...)`

... which, analogous to `.extract_table(...)`, returns the largest table
on the page.

Indeed, `.extract_table(...)` now uses `.find_table(...)` beneath the
hood.

Thanks to @pdille for the suggestion, here:
https://github.com/jsvine/pdfplumber/discussions/864#discussioncomment-5668209
2023-07-04 09:59:00 -04:00
Jeremy Singer-Vine 57d51bb5f4 Normalize color representation (#917)
This commit normalizes the type representation of `stroking_color` and
`non_stroking_color` values. Thanks to @dhdaines for pointing out this
inconsistency.

Previously, `pdfplumber` passed along `pdfminer.six`'s colors without
normalization. Due to quirks in `pdfminer.six`'s color handling, this
meant that those values could be floats, ints, lists, or tuples. This
commit normalizes all color values (when non-None) into n-tuples, where
(val,) represents grayscale colors, (val, val, val) represents RBG, and
(val, val, val, val) represents CMYK colors.

This should solve the consistency issue, although might cause breaking
changes to code that filters for non-tuple values — e.g., `[c for c in
page.chars if c == [1, 0 0]]`. Although breaking changes are unpleasant,
I think the tradeoff for longer-term consistency is worth it.
2023-07-03 22:59:05 -04:00
Jeremy Singer-Vine 0de6da97fd Fix bug for re-crops that use relative=True (#914)
When using relative=True for a re-crop, pdfplumber was passing the wrong
bounding box to the cropping function. This commit fixes that bug and
also refactors CroppedPage.__init__(...) for clarity and consistency's
sake.
2023-07-02 11:49:53 -04:00
Jeremy Singer-Vine 474f74c906 Add another test for use_text_flow 2023-07-01 18:41:58 -04:00
Jeremy Singer-Vine a032019a97 Rewrite char_begins_new_word for ease & edge cases
Main edge-case was with `use_text_flow` on text-lines that then
backtracked. But this rewrite also aims to make the logic more explicit
and easier to follow.
2023-07-01 18:40:11 -04:00
Jeremy Singer-Vine b1db5b840f Handle use_text_flow more consistently
As noted in #912, `use_text_flow` was not being handled consistently, as
characters and words were being re-sorted without checking first if this
parameter was set to `True`.
2023-07-01 18:36:59 -04:00
Jeremy Singer-Vine 5aaabcd038 Merge pull request #877 from RitchieP/patch-1
Update README.md
2023-05-08 22:27:42 +02:00
Ritchie Poh 8bf51217bc Update README.md
Fixed a small typo
2023-05-02 14:46:16 +08:00
Jeremy Singer-Vine ae676aeacd Add PNG-length alternatives to tests 2023-04-20 06:10:42 -04:00
Jeremy Singer-Vine 255eaaced7 Merge pull request #862 from jsvine/develop
v0.9.0
v0.9.0
2023-04-13 08:56:13 -04:00
Jeremy Singer-Vine 3e0f9d782e Rerun examples/notebooks for v0.9.0 (incl one fix)
(`san-jose-pd-firearm-report.ipynb` needed a one-param fix, after not
having been updated in a few versions.)
2023-04-13 08:41:11 -04:00
Jeremy Singer-Vine 1a8603bd1b Bump version to v0.9.0 2023-04-13 08:40:52 -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 f042d5c246 Remove line of code that does nothing 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 4b373977be Add .extract_text_lines & related .search params
Inspired by https://github.com/jsvine/pdfplumber/discussions/852,
it turns out that .search(...) gets us most of the way, already.

Added a few params (`main_group`, `return_groups`, `return_chars`) to
.search(...) to enable this, which also make that method more generally
flexible.
2023-04-13 08:13:16 -04:00
Jeremy Singer-Vine 79ec839ebc Label ordered_chars more explicitly as such 2023-04-13 08:13:16 -04:00
Jeremy Singer-Vine 6acd580ceb Make char_begins_new_word more explict & rigorous
This commit aims to fix two things:

- The semi-crypticness of the previous version of char_begins_new_word
- The inconsistency (vs. the rest of the approach) in how the method
  was comparing "top" to "bottom" for interline comparisons, instead of
  "top" to "top", as rightly and helpfully pointed out by @bellma-lilly
  in https://github.com/jsvine/pdfplumber/discussions/840

Based on the unit tests, this shouldn't change the output of
`pdfplumber` in the vast majority of use cases. It might affect some
output in edge-cases, for which I apologize for any inconvenience and
which I hope is balanced out by this more consistent approach's benefits
in the long run.
2023-04-13 08:13:16 -04:00
Jeremy Singer-Vine ebb93eae15 Simplify char_begins_new_word
Calculating the bounding boxes of the words is, upon reflection and
testing, not necessary. Instead, all we need is the latest character in
the current word.
2023-04-13 08:13:16 -04:00
Jeremy Singer-Vine 8368954eaa Handle whitespace-only and empty search results
With regular expressions, patterns made up entirely of optional groups
(e.g., r"(dfsdgfwerw)?") can return match objects that are, effectively,
empty. These were being treated as real search results, and throwing
errors in the process. Now they're being treated as non-results.

Separately but relatedly, whitespace-only searches were throwing errors.
This was due to (a) how PDFs generally represent whitespace (implicitly,
rather than explicit space characters), and (b) how pdfplumber
internally represents those spaces while performing layout analysis.
This caused search results to have no explicit bounding box, throwing
errors. Now, similarly to handling empty search results, we handle
all-whitespace search results by considering them to be non-results.
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
Jeremy Singer-Vine 1d5d646c04 Bump version to v0.8.1 v0.8.1 2023-04-08 10:47:49 -04:00
Jeremy Singer-Vine db136b77cc Fix x0>x1 error for rect fills, per new Pillow
Pillow 9.5 throws an error when you try to draw a rectangle where x0>x1
or top>bottom. For rects with small width or height but thick strokes,
we ended up hitting that error because we were trying to subtract the
stroke width from the area to be filled.
2023-04-08 10:43:48 -04:00
Han cf17ad1c42 Update README.md and example re. pts/points (#856)
Update ag-energy-roundup-curves.ipynb

Update the key name 'point' to 'pts' in the example to the latest version

* 💄

* Update ag-energy-roundup-curves.ipynb

💄
2023-04-07 19:27:47 -04:00
Jeremy Singer-Vine ee48b26099 Tiny tweak to README.md 2023-03-09 10:59:44 -05:00