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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
import pytest
from pikepdf import _qpdf as qpdf
import os
import platform
import shutil
from contextlib import suppress
def test_minimum_qpdf_version():
assert qpdf.qpdf_version() >= '6.0.0'
def test_open_pdf(resources):
pdf = qpdf.PDF.open(resources / 'graph.pdf')
assert '1.3' <= pdf.pdf_version <= '1.7'
assert pdf.root['/Pages']['/Count'].as_int() == 1
def test_open_pdf_password(resources):
pdf = qpdf.PDF.open(resources / 'graph-encrypted.pdf', password='owner')
assert pdf.root['/Pages']['/Count'].as_int() == 1
def test_open_pdf_wrong_password(resources):
with pytest.raises(qpdf.PasswordError):
qpdf.PDF.open(resources / 'graph-encrypted.pdf', password='wrong')
def test_open_pdf_password_encoding(resources):
with pytest.raises(qpdf.PasswordError):
qpdf.PDF.open(resources / 'graph-encrypted.pdf', password=b'\x01\xfe')
def test_open_pdf_no_password_but_needed(resources):
with pytest.raises(qpdf.PasswordError):
qpdf.PDF.open(resources / 'graph-encrypted.pdf')
def test_stream(resources):
with (resources / 'graph.pdf').open('rb') as stream:
pdf = qpdf.PDF.open(stream)
assert pdf.root.Pages.Count == 1
def test_no_text_stream(resources):
with pytest.raises(TypeError):
with (resources / 'graph.pdf').open('r') as stream:
qpdf.PDF.open(stream)
def test_attr_access(resources):
pdf = qpdf.PDF.open(resources / 'graph.pdf')
assert int(pdf.root.Pages.Count) == 1
def test_create_pdf(outdir):
pdf = qpdf.PDF.new()
font = pdf.make_indirect(
qpdf.Object.parse(b"""
<<
/Type /Font
/Subtype /Type1
/Name /F1
/BaseFont /Helvetica
/Encoding /WinAnsiEncoding
>>"""))
width, height = 100, 100
image_data = b"\xff\x7f\x00" * (width * height)
image = qpdf.Object.Stream(pdf, image_data)
image.stream_dict = qpdf.Object.parse(b"""
<<
/Type /XObject
/Subtype /Image
/ColorSpace /DeviceRGB
/BitsPerComponent 8
/Width 100
/Height 100
>>""")
rfont = {'/F1': font}
xobj = {'/Im1': image}
resources = {
'/Font': rfont,
'/XObject': xobj
}
mediabox = [0, 0, 612, 792]
stream = b"""
BT /F1 24 Tf 72 720 Td (Hi there) Tj ET
q 144 0 0 144 234 324 cm /Im1 Do Q
"""
contents = qpdf.Object.Stream(pdf, stream)
page_dict = {
'/Type': qpdf.Object.Name('/Page'),
'/MediaBox': mediabox,
'/Contents': contents,
'/Resources': resources
}
qpdf_page_dict = page_dict
page = pdf.make_indirect(qpdf_page_dict)
pdf.pages.append(page)
pdf.save(outdir / 'hi.pdf')
def test_copy_semantics(resources):
pdf = qpdf.PDF.open(resources / 'graph.pdf')
# Ensure that we can name a reference to a child object and view the
# changes from the parent
page = pdf.pages[0]
mediabox = page['/MediaBox']
assert mediabox[2].decode() != 0
mediabox[2] = 0
assert page['/MediaBox'][2] == mediabox[2]
def test_save_stream(resources, outdir):
from io import BytesIO
pdf = qpdf.PDF.open(resources / 'graph.pdf')
pdf.save(outdir / 'nostream.pdf', static_id=True)
bio = BytesIO()
pdf.save(bio, static_id=True)
bio.seek(0)
with (outdir / 'nostream.pdf').open('rb') as saved_file:
saved_file_contents = saved_file.read()
assert saved_file_contents == bio.read()
def test_copy_page_keepalive(resources, outdir):
from shutil import copy
copy(resources / 'sandwich.pdf', outdir / 'sandwich.pdf')
src = qpdf.PDF.open(outdir / 'sandwich.pdf')
pdf = qpdf.PDF.open(resources / 'graph.pdf')
pdf.pages.append(src.pages[0])
del src
src = None
(outdir / 'sandwich.pdf').unlink()
pdf.save(outdir / 'out.pdf')
|