Files
pdfplumber/tests/test_repair.py
T
Jeremy Singer-Vine 48cab3f73e Change default .repair(...) setting h/t @Laubeee
Now passes "default" rather than "prepress" to Ghostscript's
`-dPDFSETTINGS` parameter.

Also makes that setting modifiable via `.repair(setting=...)`, where the
value is one of `"default"`, `"prepress"`, `"printer"`, or `"ebook"`.

See #874 and https://ghostscript.com/docs/9.54.0/VectorDevices.htm
2024-07-31 18:16:00 -04:00

77 lines
2.6 KiB
Python

#!/usr/bin/env python
import os
import shutil
import tempfile
import unittest
import pytest
import pdfplumber
HERE = os.path.abspath(os.path.dirname(__file__))
class Test(unittest.TestCase):
def test_from_issue_932(self):
path = os.path.join(HERE, "pdfs/malformed-from-issue-932.pdf")
with pdfplumber.open(path) as pdf:
page = pdf.pages[0]
char = page.chars[0]
assert char["bottom"] > page.height
with pdfplumber.open(path, repair=True) as pdf:
page = pdf.pages[0]
char = page.chars[0]
assert char["bottom"] < page.height
with pdfplumber.repair(path) as repaired:
with pdfplumber.open(repaired) as pdf:
page = pdf.pages[0]
char = page.chars[0]
assert char["bottom"] < page.height
def test_other_repair_inputs(self):
path = os.path.join(HERE, "pdfs/malformed-from-issue-932.pdf")
with pdfplumber.open(open(path, "rb"), repair=True) as pdf:
page = pdf.pages[0]
char = page.chars[0]
assert char["bottom"] < page.height
def test_bad_repair_path(self):
path = os.path.join(HERE, "pdfs/abc.xyz")
with pytest.raises(Exception):
with pdfplumber.open(path, repair=True):
pass
def test_repair_to_file(self):
path = os.path.join(HERE, "pdfs/malformed-from-issue-932.pdf")
with tempfile.NamedTemporaryFile("wb") as out:
pdfplumber.repair(path, outfile=out.name)
with pdfplumber.open(out.name) as pdf:
page = pdf.pages[0]
char = page.chars[0]
assert char["bottom"] < page.height
def test_repair_setting(self):
path = os.path.join(HERE, "pdfs/malformed-from-issue-932.pdf")
with tempfile.NamedTemporaryFile("wb") as out:
pdfplumber.repair(path, outfile=out.name)
size_default = os.stat(out.name).st_size
with tempfile.NamedTemporaryFile("wb") as out:
pdfplumber.repair(path, outfile=out.name, setting="prepress")
size_prepress = os.stat(out.name).st_size
assert size_default > size_prepress
def test_repair_password(self):
path = os.path.join(HERE, "pdfs/password-example.pdf")
with pdfplumber.open(path, repair=True, password="test") as pdf:
assert len(pdf.pages[0].chars)
def test_repair_custom_path(self):
path = os.path.join(HERE, "pdfs/malformed-from-issue-932.pdf")
with pdfplumber.open(path, repair=True, gs_path=shutil.which("gs")) as pdf:
assert len(pdf.pages[0].chars)