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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
Common Use Cases
================
pydyf has been created for WeasyPrint and many common use cases can thus be
found in `its repository`_.
.. _its repository: https://github.com/Kozea/WeasyPrint
Draw rectangles and lines
-------------------------
.. code-block:: python
import pydyf
document = pydyf.PDF()
draw = pydyf.Stream()
# Draw a first rectangle
# With the border in dash style
# The dash line is 2 points full, 1 point empty
# And the dash line begins with 2 full points
draw.rectangle(100, 100, 50, 70)
draw.set_dash([2, 1], 0)
draw.stroke()
# Draw a second rectangle
# The dash is reset to a full line
# The line width is set
# Move the bottom-left corner to (80, 80)
# Fill the rectangle
draw.rectangle(50, 50, 20, 40)
draw.set_dash([], 0)
draw.set_line_width(10)
draw.set_matrix(1, 0, 0, 1, 80, 80)
draw.fill()
# Add the stream with the two rectangles into the document
document.add_object(draw)
# Add a page to the document containing the draw
document.add_page(pydyf.Dictionary({
'Type': '/Page',
'Parent': document.pages.reference,
'Contents': draw.reference,
'MediaBox': pydyf.Array([0, 0, 200, 200]),
}))
# Write to document.pdf
with open('document.pdf', 'wb') as f:
document.write(f)
Add some color
--------------
.. code-block:: python
import pydyf
document = pydyf.PDF()
draw = pydyf.Stream()
# Set the color for nonstroking and stroking operations
# Red for nonstroking an green for stroking
draw.set_color_rgb(1.0, 0.0, 0.0)
draw.set_color_rgb(0.0, 1.0, 0.0, stroke=True)
draw.rectangle(100, 100, 50, 70)
draw.set_dash([2, 1], 0)
draw.stroke()
draw.rectangle(50, 50, 20, 40)
draw.set_dash([], 0)
draw.set_line_width(10)
draw.set_matrix(1, 0, 0, 1, 80, 80)
draw.fill()
document.add_object(draw)
document.add_page(pydyf.Dictionary({
'Type': '/Page',
'Parent': document.pages.reference,
'Contents': draw.reference,
'MediaBox': pydyf.Array([0, 0, 200, 200]),
}))
with open('document.pdf', 'wb') as f:
document.write(f)
Display image
-------------
.. code-block:: python
import pydyf
document = pydyf.PDF()
extra = pydyf.Dictionary({
'Type': '/XObject',
'Subtype': '/Image',
'Width': 197,
'Height': 101,
'ColorSpace': '/DeviceRGB',
'BitsPerComponent': 8,
'Filter': '/DCTDecode',
})
image = open('logo.jpg', 'rb').read()
xobject = pydyf.Stream([image], extra=extra)
document.add_object(xobject)
image = pydyf.Stream()
image.push_state()
image.set_matrix(100, 0, 0, 100, 100, 100)
image.draw_x_object('Im1')
image.pop_state()
document.add_object(image)
# Put the image in the resources of the PDF
document.add_page(pydyf.Dictionary({
'Type': '/Page',
'Parent': document.pages.reference,
'MediaBox': pydyf.Array([0, 0, 200, 200]),
'Resources': pydyf.Dictionary({
'ProcSet': pydyf.Array(['/PDF', '/ImageB']),
'XObject': pydyf.Dictionary({'Im1': xobject.reference}),
}),
'Contents': image.reference,
}))
with open('document.pdf', 'wb') as f:
document.write(f)
Display text
------------
.. code-block:: python
import pydyf
document = pydyf.PDF()
# Define the font
font = pydyf.Dictionary({
'Type': '/Font',
'Subtype': '/Type1',
'Name': '/F1',
'BaseFont': '/Helvetica',
'Encoding': '/MacRomanEncoding',
})
document.add_object(font)
# Set the font use for the text
# Move to where to display the text
# And display it
text = pydyf.Stream()
text.begin_text()
text.set_font_size('F1', 20)
text.set_text_matrix(1, 0, 0, 1, 10, 90)
text.show_text(pydyf.String('Bœuf grillé & café'.encode('macroman')))
text.end_text()
document.add_object(text)
# Put the font in the resources of the PDF
document.add_page(pydyf.Dictionary({
'Type': '/Page',
'Parent': document.pages.reference,
'MediaBox': pydyf.Array([0, 0, 200, 200]),
'Contents': text.reference,
'Resources': pydyf.Dictionary({
'ProcSet': pydyf.Array(['/PDF', '/Text']),
'Font': pydyf.Dictionary({'F1': font.reference}),
})
}))
with open('document.pdf', 'wb') as f:
document.write(f)
Add metadata
------------
.. code-block:: python
import datetime
import pydyf
document = pydyf.PDF()
document.info['Author'] = pydyf.String('Jane Doe')
document.info['Creator'] = pydyf.String('pydyf')
document.info['Keywords'] = pydyf.String('some keywords')
document.info['Producer'] = pydyf.String('The producer')
document.info['Subject'] = pydyf.String('An example PDF')
document.info['Title'] = pydyf.String('A PDF containing metadata')
now = datetime.datetime.now()
document.info['CreationDate'] = pydyf.String(now.strftime('D:%Y%m%d%H%M%S'))
document.add_page(
pydyf.Dictionary(
{
'Type': '/Page',
'Parent': document.pages.reference,
'MediaBox': pydyf.Array([0, 0, 200, 200]),
}
)
)
# 550 bytes PDF
with open('metadata.pdf', 'wb') as f:
document.write(f)
Display inline QR-code image
----------------------------
.. code-block:: python
import pydyf
import qrcode
# Create a QR code image
image = qrcode.make('Some data here')
raw_data = image.tobytes()
width = image.size[0]
height = image.size[1]
document = pydyf.PDF()
stream = pydyf.Stream(compress=True)
stream.push_state()
x = 0
y = 0
stream.set_matrix(width, 0, 0, height, x, y)
# Add the 1-bit grayscale image inline in the PDF
stream.inline_image(width, height, 'Gray', 1, raw_data)
stream.pop_state()
document.add_object(stream)
# Put the image in the resources of the PDF
document.add_page(
pydyf.Dictionary(
{
'Type': '/Page',
'Parent': document.pages.reference,
'MediaBox': pydyf.Array([0, 0, 400, 400]),
'Resources': pydyf.Dictionary(
{
'ProcSet': pydyf.Array(
['/PDF', '/ImageB', '/ImageC', '/ImageI']
),
}
),
'Contents': stream.reference,
}
)
)
# 909 bytes PDF
with open('qrcode.pdf', 'wb') as f:
document.write(f, compress=True)
|