Skip to content
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file. The format
## [Unreleased]

### Fixed
- Preserve readable Ghostscript diagnostics when a failed repair emits non-UTF-8 bytes, instead of raising a `UnicodeDecodeError` while decoding the error message.
- Initialize PDFium's form environment in `get_page_image` so that filled AcroForm field content is included when rendering pages via `Page.to_image()`. ([#1367](https://github.com/jsvine/pdfplumber/issues/1367))
- Fix `make venv`, which created the virtual environment at `venv/` but then installed into `${VENV}` (default `.venv/`), causing the target to fail on a fresh checkout (h/t @soodoku). ([ec96f72](https://github.com/jsvine/pdfplumber/commit/ec96f72))
- Include the wrapped exception's class name in `PdfminerException`'s message when `pdfminer.six` raises an exception without one (e.g. `PDFPasswordIncorrect`), which previously surfaced as a blank error message.
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,7 @@ Many thanks to the following users who've contributed ideas, features, and fixes
- [Sebastian Cao](https://github.com/cycsmail)
- [Kaspar Naraghi](https://github.com/kaninaba94)
- [Siddharth Gaur](https://github.com/siddharthgaur1)
- [@hyperammonemia](https://github.com/hyperammonemia)

## Contributing

Expand Down
2 changes: 1 addition & 1 deletion pdfplumber/repair.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def _repair(
stdout, stderr = proc.communicate(stdin.read() if stdin else None)

if proc.returncode:
raise Exception(f"{stderr.decode('utf-8')}")
raise Exception(stderr.decode("utf-8", errors="replace"))

return BytesIO(stdout)

Expand Down
27 changes: 27 additions & 0 deletions tests/test_repair.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import shutil
import tempfile
import unittest
from io import BytesIO
from unittest.mock import patch

import pytest

Expand All @@ -12,6 +14,31 @@


class Test(unittest.TestCase):
def test_repair_failure_diagnostics(self):
diagnostics = [
(b"Ghostscript: invalid input \xae", "Ghostscript: invalid input \ufffd"),
("Ghostscript: \u00e9chec".encode("utf-8"), "Ghostscript: \u00e9chec"),
]
for stderr, expected in diagnostics:
with self.subTest(stderr=stderr):
with patch("subprocess.Popen") as popen:
popen.return_value.returncode = 1
popen.return_value.communicate.return_value = (b"", stderr)
with pytest.raises(Exception) as exc:
pdfplumber.repair(BytesIO(b"input"), gs_path="ghostscript")
assert type(exc.value) is Exception
assert str(exc.value) == expected

def test_repair_success_with_non_utf8_diagnostics(self):
with patch("subprocess.Popen") as popen:
popen.return_value.returncode = 0
popen.return_value.communicate.return_value = (
b"repaired PDF bytes",
b"Ghostscript warning \xae",
)
with pdfplumber.repair(BytesIO(b"input"), gs_path="ghostscript") as result:
assert result.read() == b"repaired PDF bytes"

def test_from_issue_932(self):
path = os.path.join(HERE, "pdfs/malformed-from-issue-932.pdf")
with pdfplumber.open(path) as pdf:
Expand Down