mirror of
https://github.com/jsvine/pdfplumber.git
synced 2026-08-29 16:40:24 +08:00
db6ae97bfb
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.
59 lines
1.9 KiB
Python
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)
|