forked from Kozea/WeasyPrint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_pdf.py
718 lines (616 loc) · 21.4 KB
/
test_pdf.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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
"""Test PDF-related code, including metadata, bookmarks and hyperlinks."""
import hashlib
import io
import re
from codecs import BOM_UTF16_BE
import pytest
from weasyprint import Attachment
from weasyprint.document import Document, DocumentMetadata
from weasyprint.text.fonts import FontConfiguration
from weasyprint.urls import path2url
from .testing_utils import FakeHTML, assert_no_logs, capture_logs, resource_path
# Top and right positions in points, rounded to the default float precision of
# 6 digits, a rendered by pydyf
TOP = round(297 * 72 / 25.4, 6)
RIGHT = round(210 * 72 / 25.4, 6)
@assert_no_logs
@pytest.mark.parametrize('zoom', (1, 1.5, 0.5))
def test_page_size_zoom(zoom):
pdf = FakeHTML(string='<style>@page{size:3in 4in').write_pdf(zoom=zoom)
width, height = int(216 * zoom), int(288 * zoom)
assert f'/MediaBox [0 0 {width} {height}]'.encode() in pdf
@assert_no_logs
def test_bookmarks_1():
pdf = FakeHTML(string='''
<h1>a</h1> #
<h4>b</h4> ####
<h3>c</h3> ###
<h2>d</h2> ##
<h1>e</h1> #
''').write_pdf()
# a
# |_ b
# |_ c
# L_ d
# e
assert re.findall(b'/Count ([0-9-]*)', pdf)[-1] == b'5'
assert re.findall(b'/Title \\((.*)\\)', pdf) == [
b'a', b'b', b'c', b'd', b'e']
@assert_no_logs
def test_bookmarks_2():
pdf = FakeHTML(string='<body>').write_pdf()
assert b'Outlines' not in pdf
@assert_no_logs
def test_bookmarks_3():
pdf = FakeHTML(string='<h1>a nbsp…</h1>').write_pdf()
assert re.findall(b'/Title <(\\w*)>', pdf) == [
b'feff006100a0006e0062007300702026']
@assert_no_logs
def test_bookmarks_4():
pdf = FakeHTML(string='''
<style>
h1, h2, h3, span { height: 90pt; margin: 0 0 10pt 0 }
</style>
<h1>1</h1>
<h1>2</h1>
<h2 style="position: relative; left: 20pt">3</h2>
<h2>4</h2>
<h3>5</h3>
<span style="display: block; page-break-before: always"></span>
<h2>6</h2>
<h1>7</h1>
<h2>8</h2>
<h3>9</h3>
<h1>10</h1>
<h2>11</h2>
''').write_pdf()
# 1
# 2
# |_ 3
# |_ 4
# | L_ 5
# L_ 6
# 7
# L_ 8
# L_ 9
# 10
# L_ 11
assert re.findall(b'/Title \\((.*)\\)', pdf) == [
str(i).encode() for i in range(1, 12)]
counts = re.findall(b'/Count ([0-9-]*)', pdf)
counts.pop(0) # Page count
outlines = counts.pop()
assert outlines == b'11'
assert counts == [
b'0', b'4', b'0', b'1', b'0', b'0', b'2', b'1', b'0', b'1', b'0']
@assert_no_logs
def test_bookmarks_5():
pdf = FakeHTML(string='''
<h2>1</h2> level 1
<h4>2</h4> level 2
<h2>3</h2> level 1
<h3>4</h3> level 2
<h4>5</h4> level 3
''').write_pdf()
# 1
# L_ 2
# 3
# L_ 4
# L_ 5
assert re.findall(b'/Title \\((.*)\\)', pdf) == [
str(i).encode() for i in range(1, 6)]
counts = re.findall(b'/Count ([0-9-]*)', pdf)
counts.pop(0) # Page count
outlines = counts.pop()
assert outlines == b'5'
assert counts == [b'1', b'0', b'2', b'1', b'0']
@assert_no_logs
def test_bookmarks_6():
pdf = FakeHTML(string='''
<h2>1</h2> h2 level 1
<h4>2</h4> h4 level 2
<h3>3</h3> h3 level 2
<h5>4</h5> h5 level 3
<h1>5</h1> h1 level 1
<h2>6</h2> h2 level 2
<h2>7</h2> h2 level 2
<h4>8</h4> h4 level 3
<h1>9</h1> h1 level 1
''').write_pdf()
# 1
# |_ 2
# L_ 3
# L_ 4
# 5
# |_ 6
# L_ 7
# L_ 8
# 9
assert re.findall(b'/Title \\((.*)\\)', pdf) == [
str(i).encode() for i in range(1, 10)]
counts = re.findall(b'/Count ([0-9-]*)', pdf)
counts.pop(0) # Page count
outlines = counts.pop()
assert outlines == b'9'
assert counts == [b'3', b'0', b'1', b'0', b'3', b'0', b'1', b'0', b'0']
@assert_no_logs
def test_bookmarks_7():
# Reference for the next test. zoom=1
pdf = FakeHTML(string='<h2>a</h2>').write_pdf()
assert re.findall(b'/Title \\((.*)\\)', pdf) == [b'a']
dest, = re.findall(b'/Dest \\[(.*)\\]', pdf)
y = round(float(dest.strip().split()[-2]))
pdf = FakeHTML(string='<h2>a</h2>').write_pdf(zoom=1.5)
assert re.findall(b'/Title \\((.*)\\)', pdf) == [b'a']
dest, = re.findall(b'/Dest \\[(.*)\\]', pdf)
assert round(float(dest.strip().split()[-2])) == 1.5 * y
@assert_no_logs
def test_bookmarks_8():
pdf = FakeHTML(string='''
<h1>a</h1>
<h2>b</h2>
<h3>c</h3>
<h2 style="bookmark-state: closed">d</h2>
<h3>e</h3>
<h4>f</h4>
<h1>g</h1>
''').write_pdf()
# a
# |_ b
# | |_ c
# |_ d (closed)
# | |_ e
# | |_ f
# g
assert re.findall(b'/Title \\((.*)\\)', pdf) == [
b'a', b'b', b'c', b'd', b'e', b'f', b'g']
counts = re.findall(b'/Count ([0-9-]*)', pdf)
counts.pop(0) # Page count
outlines = counts.pop()
assert outlines == b'5'
assert counts == [b'3', b'1', b'0', b'-2', b'1', b'0', b'0']
@assert_no_logs
def test_bookmarks_9():
pdf = FakeHTML(string='''
<h1 style="bookmark-label: 'h1 on page ' counter(page)">a</h1>
''').write_pdf()
counts = re.findall(b'/Count ([0-9-]*)', pdf)
outlines = counts.pop()
assert outlines == b'1'
assert re.findall(b'/Title \\((.*)\\)', pdf) == [b'h1 on page 1']
@assert_no_logs
def test_bookmarks_10():
pdf = FakeHTML(string='''
<style>
div:before, div:after {
content: '';
bookmark-level: 1;
bookmark-label: 'x';
}
</style>
<div>a</div>
''').write_pdf()
# x
# x
counts = re.findall(b'/Count ([0-9-]*)', pdf)
outlines = counts.pop()
assert outlines == b'2'
assert re.findall(b'/Title \\((.*)\\)', pdf) == [b'x', b'x']
@assert_no_logs
def test_bookmarks_11():
pdf = FakeHTML(string='''
<div style="display:inline; white-space:pre;
bookmark-level:1; bookmark-label:'a'">
a
a
a
</div>
<div style="bookmark-level:1; bookmark-label:'b'">
<div>b</div>
<div style="break-before:always">c</div>
</div>
''').write_pdf()
# a
# b
counts = re.findall(b'/Count ([0-9-]*)', pdf)
outlines = counts.pop()
assert outlines == b'2'
assert re.findall(b'/Title \\((.*)\\)', pdf) == [b'a', b'b']
@assert_no_logs
def test_bookmarks_12():
pdf = FakeHTML(string='''
<div style="bookmark-level:1; bookmark-label:contents">a</div>
''').write_pdf()
# a
counts = re.findall(b'/Count ([0-9-]*)', pdf)
outlines = counts.pop()
assert outlines == b'1'
assert re.findall(b'/Title \\((.*)\\)', pdf) == [b'a']
@assert_no_logs
def test_bookmarks_13():
pdf = FakeHTML(string='''
<div style="bookmark-level:1; bookmark-label:contents;
text-transform:uppercase">a</div>
''').write_pdf()
# a
counts = re.findall(b'/Count ([0-9-]*)', pdf)
outlines = counts.pop()
assert outlines == b'1'
assert re.findall(b'/Title \\((.*)\\)', pdf) == [b'a']
@assert_no_logs
def test_bookmarks_14():
pdf = FakeHTML(string='''
<h1>a</h1>
<h1> b c d </h1>
<h1> e
f </h1>
<h1> g <span> h </span> i </h1>
''').write_pdf()
assert re.findall(b'/Count ([0-9-]*)', pdf)[-1] == b'4'
assert re.findall(b'/Title \\((.*)\\)', pdf) == [
b'a', b'b c d', b'e f', b'g h i']
@assert_no_logs
def test_bookmarks_15():
# Regression test for https://github.com/Kozea/WeasyPrint/issues/1815
pdf = FakeHTML(string='''
<style>@page { size: 10pt 10pt }</style>
<h1>a</h1>
''').write_pdf()
assert re.findall(b'/Count ([0-9-]*)', pdf)[-1] == b'1'
assert re.findall(b'/Title \\((.*)\\)', pdf) == [b'a']
assert b'/XYZ 0 10 0' in pdf
@assert_no_logs
def test_links_none():
pdf = FakeHTML(string='<body>').write_pdf()
assert b'Annots' not in pdf
@assert_no_logs
def test_links():
pdf = FakeHTML(string='''
<style>
body { margin: 0; font-size: 10pt; line-height: 2 }
p { display: block; height: 90pt; margin: 0 0 10pt 0 }
img { width: 30pt; vertical-align: top }
</style>
<p><a href="https://weasyprint.org"><img src=pattern.png></a></p>
<p style="padding: 0 10pt"><a
href="#lipsum"><img style="border: solid 1pt"
src=pattern.png></a></p>
<p id=hello>Hello, World</p>
<p id=lipsum>
<a style="display: block; page-break-before: always; height: 30pt"
href="#hel%6Co"></a>a
</p>
''', base_url=resource_path('<inline HTML>')).write_pdf()
uris = re.findall(b'/URI \\((.*)\\)', pdf)
types = re.findall(b'/S (/\\w*)', pdf)
subtypes = re.findall(b'/Subtype (/\\w*)', pdf)
rects = [
[float(number) for number in match.split()] for match in re.findall(
b'/Rect \\[([\\d\\.]+ [\\d\\.]+ [\\d\\.]+ [\\d\\.]+)\\]', pdf)]
# 30pt wide (like the image), 20pt high (like line-height)
assert uris.pop(0) == b'https://weasyprint.org'
assert subtypes.pop(0) == b'/Link'
assert types.pop(0) == b'/URI'
assert rects.pop(0) == [0, TOP, 30, TOP - 20]
# The image itself: 30*30pt
assert uris.pop(0) == b'https://weasyprint.org'
assert subtypes.pop(0) == b'/Link'
assert types.pop(0) == b'/URI'
assert rects.pop(0) == [0, TOP, 30, TOP - 30]
# 32pt wide (image + 2 * 1pt of border), 20pt high
assert subtypes.pop(0) == b'/Link'
assert b'/Dest (lipsum)' in pdf
link = re.search(
b'\\(lipsum\\) \\[\\d+ 0 R /XYZ ([\\d\\.]+ [\\d\\.]+ [\\d\\.]+)]',
pdf).group(1)
assert [float(number) for number in link.split()] == [0, TOP, 0]
assert rects.pop(0) == [10, TOP - 100, 10 + 32, TOP - 100 - 20]
# The image itself: 32*32pt
assert subtypes.pop(0) == b'/Link'
assert rects.pop(0) == [10, TOP - 100, 10 + 32, TOP - 100 - 32]
# 100% wide (block), 30pt high
assert subtypes.pop(0) == b'/Link'
assert b'/Dest (hello)' in pdf
link = re.search(
b'\\(hello\\) \\[\\d+ 0 R /XYZ ([\\d\\.]+ [\\d\\.]+ [\\d\\.]+)]',
pdf).group(1)
assert [float(number) for number in link.split()] == [0, TOP - 200, 0]
assert rects.pop(0) == [0, TOP, RIGHT, TOP - 30]
@assert_no_logs
def test_sorted_links():
# Regression test for https://github.com/Kozea/WeasyPrint/issues/1352
pdf = FakeHTML(string='''
<p id="zzz">zzz</p>
<p id="aaa">aaa</p>
<a href="#zzz">z</a>
<a href="#aaa">a</a>
''', base_url=resource_path('<inline HTML>')).write_pdf()
assert b'(zzz) [' in pdf.split(b'(aaa) [')[-1]
@assert_no_logs
def test_relative_links_no_height():
# 100% wide (block), 0pt high
pdf = FakeHTML(
string='<a href="../lipsum" style="display: block"></a>a',
base_url='https://weasyprint.org/foo/bar/').write_pdf()
assert b'/S /URI\n/URI (https://weasyprint.org/foo/lipsum)'
assert f'/Rect [0 {TOP} {RIGHT} {TOP}]'.encode() in pdf
@assert_no_logs
def test_relative_links_missing_base():
# Relative URI reference without a base URI
pdf = FakeHTML(
string='<a href="../lipsum" style="display: block"></a>a',
base_url=None).write_pdf()
assert b'/S /URI\n/URI (../lipsum)'
assert f'/Rect [0 {TOP} {RIGHT} {TOP}]'.encode() in pdf
@assert_no_logs
def test_relative_links_missing_base_link():
# Relative URI reference without a base URI: not supported for -weasy-link
with capture_logs() as logs:
pdf = FakeHTML(
string='<div style="-weasy-link: url(../lipsum)">',
base_url=None).write_pdf()
assert b'/Annots' not in pdf
assert len(logs) == 1
assert 'WARNING: Ignored `-weasy-link: url(../lipsum)`' in logs[0]
assert 'Relative URI reference without a base URI' in logs[0]
@assert_no_logs
def test_relative_links_internal():
# Internal URI reference without a base URI: OK
pdf = FakeHTML(
string='<a href="#lipsum" id="lipsum" style="display: block"></a>a',
base_url=None).write_pdf()
assert b'/Dest (lipsum)' in pdf
link = re.search(
b'\\(lipsum\\) \\[\\d+ 0 R /XYZ ([\\d\\.]+ [\\d\\.]+ [\\d\\.]+)]',
pdf).group(1)
assert [float(number) for number in link.split()] == [0, TOP, 0]
rect = re.search(
b'/Rect \\[([\\d\\.]+ [\\d\\.]+ [\\d\\.]+ [\\d\\.]+)\\]',
pdf).group(1)
assert [float(number) for number in rect.split()] == [0, TOP, RIGHT, TOP]
@assert_no_logs
def test_relative_links_anchors():
pdf = FakeHTML(
string='<div style="-weasy-link: url(#lipsum)" id="lipsum"></div>a',
base_url=None).write_pdf()
assert b'/Dest (lipsum)' in pdf
link = re.search(
b'\\(lipsum\\) \\[\\d+ 0 R /XYZ ([\\d\\.]+ [\\d\\.]+ [\\d\\.]+)]',
pdf).group(1)
assert [float(number) for number in link.split()] == [0, TOP, 0]
rect = re.search(
b'/Rect \\[([\\d\\.]+ [\\d\\.]+ [\\d\\.]+ [\\d\\.]+)\\]',
pdf).group(1)
assert [float(number) for number in rect.split()] == [0, TOP, RIGHT, TOP]
@assert_no_logs
def test_relative_links_different_base():
pdf = FakeHTML(
string='<a href="/test/lipsum"></a>a',
base_url='https://weasyprint.org/foo/bar/').write_pdf()
assert b'https://weasyprint.org/test/lipsum' in pdf
@assert_no_logs
def test_relative_links_same_base():
pdf = FakeHTML(
string='<a id="test" href="/foo/bar/#test"></a>a',
base_url='https://weasyprint.org/foo/bar/').write_pdf()
assert b'/Dest (test)' in pdf
@assert_no_logs
def test_missing_links():
with capture_logs() as logs:
pdf = FakeHTML(string='''
<style> a { display: block; height: 15pt } </style>
<a href="#lipsum"></a>
<a href="#missing" id="lipsum"></a>
<a href=""></a>a
''', base_url=None).write_pdf()
assert b'/Dest (lipsum)' in pdf
assert len(logs) == 1
link = re.search(
b'\\(lipsum\\) \\[\\d+ 0 R /XYZ ([\\d\\.]+ [\\d\\.]+ [\\d\\.]+)]',
pdf).group(1)
assert [float(number) for number in link.split()] == [0, TOP - 15, 0]
rect = re.search(
b'/Rect \\[([\\d\\.]+ [\\d\\.]+ [\\d\\.]+ [\\d\\.]+)\\]',
pdf).group(1)
assert [float(number) for number in rect.split()] == [
0, TOP, RIGHT, TOP - 15]
assert 'ERROR: No anchor #missing for internal URI reference' in logs[0]
@assert_no_logs
def test_anchor_multiple_pages():
pdf = FakeHTML(string='''
<style> a { display: block; break-after: page } </style>
<div id="lipsum">
<a href="#lipsum"></a>
<a href="#lipsum"></a>
<a href="#lipsum"></a>
</div>
''', base_url=None).write_pdf()
first_page, = re.findall(b'/Kids \\[(\\d+) 0 R', pdf)
assert b'/Names [(lipsum) [' + first_page in pdf
@assert_no_logs
def test_embed_gif():
assert b'/Filter /DCTDecode' not in FakeHTML(
base_url=resource_path('dummy.html'),
string='<img src="pattern.gif">').write_pdf()
@assert_no_logs
def test_embed_jpeg():
# JPEG-encoded image, embedded in PDF:
assert b'/Filter /DCTDecode' in FakeHTML(
base_url=resource_path('dummy.html'),
string='<img src="blue.jpg">').write_pdf()
@assert_no_logs
def test_embed_image_once():
# Image repeated multiple times, embedded once
assert FakeHTML(
base_url=resource_path('dummy.html'),
string='''
<img src="blue.jpg">
<div style="background: url(blue.jpg)"></div>
<img src="blue.jpg">
<div style="background: url(blue.jpg) no-repeat"></div>
''').write_pdf().count(b'/Filter /DCTDecode') == 1
@assert_no_logs
def test_embed_images_from_pages():
page1, = FakeHTML(
base_url=resource_path('dummy.html'),
string='<img src="blue.jpg">').render().pages
page2, = FakeHTML(
base_url=resource_path('dummy.html'),
string='<img src="not-optimized.jpg">').render().pages
document = Document(
(page1, page2), metadata=DocumentMetadata(),
font_config=FontConfiguration(), url_fetcher=None).write_pdf()
assert document.count(b'/Filter /DCTDecode') == 2
@assert_no_logs
def test_document_info():
pdf = FakeHTML(string='''
<meta name=author content="I Me & Myself">
<title>Test document</title>
<h1>Another title</h1>
<meta name=generator content="Human after all">
<meta name=keywords content="html ,\tcss,
pdf,css">
<meta name=description content="Blah… ">
<meta name=dcterms.created content=2011-04-21T23:00:00Z>
<meta name=dcterms.modified content=2013-07-21T23:46+01:00>
''').write_pdf()
assert b'/Author (I Me & Myself)' in pdf
assert b'/Title (Test document)' in pdf
assert (
b'/Creator <feff00480075006d0061006e00a00061'
b'006600740065007200a00061006c006c>') in pdf
assert b'/Keywords (html, css, pdf)' in pdf
assert b'/Subject <feff0042006c0061006820260020>' in pdf
assert b'/CreationDate (D:20110421230000Z)' in pdf
assert b"/ModDate (D:20130721234600+01'00)" in pdf
@assert_no_logs
def test_embedded_files_attachments(tmp_path):
absolute_tmp_path = tmp_path / 'some_file.txt'
absolute_data = b'12345678'
absolute_tmp_path.write_bytes(absolute_data)
absolute_url = path2url(absolute_tmp_path)
assert absolute_url.startswith('file://')
relative_tmp_path = tmp_path / 'äöü.txt'
relative_data = b'abcdefgh'
relative_tmp_path.write_bytes(relative_data)
pdf = FakeHTML(
string=f'''
<title>Test document</title>
<meta charset="utf-8">
<link
rel="attachment"
title="some file attachment äöü"
href="data:,hi%20there">
<link rel="attachment" href="{absolute_url}">
<link rel="attachment" href="{relative_tmp_path.name}">
<h1>Heading 1</h1>
<h2>Heading 2</h2>
''',
base_url=tmp_path,
).write_pdf(
attachments=[
Attachment('data:,oob attachment', description='Hello'),
'data:,raw URL',
io.BytesIO(b'file like obj')
]
)
assert f'<{hashlib.md5(b"hi there").hexdigest()}>'.encode() in pdf
assert b'/F ()' in pdf
assert b'/UF (attachment.bin)' in pdf
name = BOM_UTF16_BE + 'some file attachment äöü'.encode('utf-16-be')
assert b'/Desc <' + name.hex().encode() + b'>' in pdf
assert hashlib.md5(absolute_data).hexdigest().encode() in pdf
assert absolute_tmp_path.name.encode() in pdf
assert hashlib.md5(relative_data).hexdigest().encode() in pdf
name = BOM_UTF16_BE + 'some file attachment äöü'.encode('utf-16-be')
assert b'/Desc <' + name.hex().encode() + b'>' in pdf
assert hashlib.md5(b'oob attachment').hexdigest().encode() in pdf
assert b'/Desc (Hello)' in pdf
assert hashlib.md5(b'raw URL').hexdigest().encode() in pdf
assert hashlib.md5(b'file like obj').hexdigest().encode() in pdf
assert b'/EmbeddedFiles' in pdf
assert b'/Outlines' in pdf
@assert_no_logs
def test_attachments_data():
pdf = FakeHTML(string='''
<title>Test document 2</title>
<meta charset="utf-8">
<link rel="attachment" href="data:,some data">
''').write_pdf()
md5 = f'<{hashlib.md5(b"some data").hexdigest()}>'.encode()
assert md5 in pdf
assert b'EmbeddedFiles' in pdf
@assert_no_logs
def test_attachments_data_with_anchor():
pdf = FakeHTML(string='''
<title>Test document 2</title>
<meta charset="utf-8">
<link rel="attachment" href="data:,some data">
<h1 id="title">Title</h1>
<a href="#title">example</a>
''').write_pdf()
md5 = f'<{hashlib.md5(b"some data").hexdigest()}>'.encode()
assert md5 in pdf
assert b'EmbeddedFiles' in pdf
@assert_no_logs
def test_attachments_no_href():
with capture_logs() as logs:
pdf = FakeHTML(string='''
<title>Test document 2</title>
<meta charset="utf-8">
<link rel="attachment">
''').write_pdf()
assert b'Names' not in pdf
assert b'Outlines' not in pdf
assert len(logs) == 1
assert 'Missing href' in logs[0]
@assert_no_logs
def test_attachments_none():
pdf = FakeHTML(string='''
<title>Test document 3</title>
<meta charset="utf-8">
<h1>Heading</h1>
''').write_pdf()
assert b'Names' not in pdf
assert b'Outlines' in pdf
@assert_no_logs
def test_attachments_none_empty():
pdf = FakeHTML(string='''
<title>Test document 3</title>
<meta charset="utf-8">
''').write_pdf()
assert b'Names' not in pdf
assert b'Outlines' not in pdf
@assert_no_logs
def test_annotations():
pdf = FakeHTML(string='''
<title>Test document</title>
<meta charset="utf-8">
<a
rel="attachment"
href="data:,some data"
download>A link that lets you download an attachment</a>
''').write_pdf()
assert hashlib.md5(b'some data').hexdigest().encode() in pdf
assert b'/FileAttachment' in pdf
assert b'/EmbeddedFiles' not in pdf
@pytest.mark.parametrize('style, media, bleed, trim', (
('bleed: 30pt; size: 10pt',
[-30, -30, 40, 40],
[-10, -10, 20, 20],
[0, 0, 10, 10]),
('bleed: 15pt 3pt 6pt 18pt; size: 12pt 15pt',
[-18, -15, 15, 21],
[-10, -10, 15, 21],
[0, 0, 12, 15]),
))
@assert_no_logs
def test_bleed(style, media, bleed, trim):
pdf = FakeHTML(string='''
<title>Test document</title>
<style>@page { %s }</style>
<body>test
''' % style).write_pdf()
assert f'/MediaBox {str(media).replace(",", "")}'.encode() in pdf
assert f'/BleedBox {str(bleed).replace(",", "")}'.encode() in pdf
assert f'/TrimBox {str(trim).replace(",", "")}'.encode() in pdf