Files
pdfplumber/tests/test_repair.py
T
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

59 lines
1.9 KiB
Python

#!/usr/bin/env python
import os
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_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)