-
Notifications
You must be signed in to change notification settings - Fork 683
/
test_repair.py
76 lines (60 loc) · 2.63 KB
/
test_repair.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/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)