xhtml2pdf 0.2.27__tar.gz → 0.2.29__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (116) hide show
  1. {xhtml2pdf-0.2.27/xhtml2pdf.egg-info → xhtml2pdf-0.2.29}/PKG-INFO +6 -1
  2. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/pyproject.toml +15 -1
  3. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/tests/test_asian_font_support.py +40 -0
  4. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/tests/test_block_box.py +71 -0
  5. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/tests/test_cli.py +29 -1
  6. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/tests/test_css_parser.py +2 -2
  7. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/tests/test_flex.py +14 -0
  8. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/tests/test_fonts.py +26 -0
  9. xhtml2pdf-0.2.29/tests/test_httpconfig.py +65 -0
  10. xhtml2pdf-0.2.29/tests/test_line_box.py +100 -0
  11. xhtml2pdf-0.2.29/tests/test_line_height.py +56 -0
  12. xhtml2pdf-0.2.29/tests/test_list_margins.py +64 -0
  13. xhtml2pdf-0.2.29/tests/test_margin_boxes.py +268 -0
  14. xhtml2pdf-0.2.29/tests/test_opacity.py +109 -0
  15. xhtml2pdf-0.2.29/tests/test_opentype.py +226 -0
  16. xhtml2pdf-0.2.29/tests/test_page_break.py +70 -0
  17. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/tests/test_parser.py +3 -3
  18. xhtml2pdf-0.2.29/tests/test_print.py +449 -0
  19. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/tests/test_reportlab_api.py +78 -2
  20. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/tests/test_security_limits.py +52 -12
  21. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/tests/test_signatures.py +19 -13
  22. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/tests/test_tables.py +44 -6
  23. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/tests/test_tags.py +50 -0
  24. xhtml2pdf-0.2.29/tests/test_text_decoration.py +87 -0
  25. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/tests/test_utils.py +72 -0
  26. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/tests/test_watermarks.py +1 -1
  27. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/xhtml2pdf/__init__.py +1 -1
  28. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/xhtml2pdf/builders/block.py +37 -7
  29. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/xhtml2pdf/builders/flex.py +64 -19
  30. xhtml2pdf-0.2.29/xhtml2pdf/builders/margin_boxes.py +303 -0
  31. xhtml2pdf-0.2.29/xhtml2pdf/builders/opacity.py +76 -0
  32. xhtml2pdf-0.2.29/xhtml2pdf/builders/pdfx.py +384 -0
  33. xhtml2pdf-0.2.29/xhtml2pdf/builders/preflight.py +358 -0
  34. xhtml2pdf-0.2.29/xhtml2pdf/builders/printmarks.py +185 -0
  35. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/xhtml2pdf/builders/signs.py +24 -28
  36. xhtml2pdf-0.2.29/xhtml2pdf/builders/strings.py +110 -0
  37. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/xhtml2pdf/builders/watermarks.py +54 -5
  38. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/xhtml2pdf/config/httpconfig.py +1 -1
  39. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/xhtml2pdf/context.py +151 -13
  40. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/xhtml2pdf/default.py +23 -1
  41. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/xhtml2pdf/document.py +56 -1
  42. xhtml2pdf-0.2.29/xhtml2pdf/opentype.py +156 -0
  43. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/xhtml2pdf/paragraph.py +6 -2
  44. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/xhtml2pdf/parser.py +55 -14
  45. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/xhtml2pdf/pisa.py +24 -0
  46. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/xhtml2pdf/properties.py +19 -1
  47. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/xhtml2pdf/reportlab_paragraph.py +177 -56
  48. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/xhtml2pdf/tables.py +2 -0
  49. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/xhtml2pdf/tags.py +8 -4
  50. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/xhtml2pdf/util.py +231 -12
  51. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/xhtml2pdf/w3c/css.py +4 -0
  52. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/xhtml2pdf/w3c/cssParser.py +48 -2
  53. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/xhtml2pdf/w3c/cssSpecial.py +14 -6
  54. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/xhtml2pdf/w3c/generated_content.py +12 -2
  55. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/xhtml2pdf/xhtml2pdf_reportlab.py +132 -6
  56. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29/xhtml2pdf.egg-info}/PKG-INFO +6 -1
  57. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/xhtml2pdf.egg-info/SOURCES.txt +17 -0
  58. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/xhtml2pdf.egg-info/requires.txt +6 -0
  59. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/LICENSE.txt +0 -0
  60. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/MANIFEST.in +0 -0
  61. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/README.rst +0 -0
  62. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/setup.cfg +0 -0
  63. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/tests/test_bidi.py +0 -0
  64. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/tests/test_block_margins.py +0 -0
  65. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/tests/test_block_width.py +0 -0
  66. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/tests/test_css_media_rule.py +0 -0
  67. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/tests/test_css_variables.py +0 -0
  68. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/tests/test_document.py +0 -0
  69. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/tests/test_files.py +0 -0
  70. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/tests/test_flex_flowable.py +0 -0
  71. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/tests/test_flex_layout.py +0 -0
  72. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/tests/test_font_isolation.py +0 -0
  73. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/tests/test_frame_align.py +0 -0
  74. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/tests/test_frame_pages.py +0 -0
  75. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/tests/test_generated_content.py +0 -0
  76. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/tests/test_http.py +0 -0
  77. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/tests/test_inline_block.py +0 -0
  78. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/tests/test_inline_box.py +0 -0
  79. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/tests/test_insert_pdf.py +0 -0
  80. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/tests/test_optional_dependencies.py +0 -0
  81. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/tests/test_page_numbering.py +0 -0
  82. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/tests/test_page_padding.py +0 -0
  83. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/tests/test_paragraph.py +0 -0
  84. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/tests/test_paragraph_height.py +0 -0
  85. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/tests/test_pdf.py +0 -0
  86. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/tests/test_position.py +0 -0
  87. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/tests/test_reportlab.py +0 -0
  88. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/tests/test_reportlab_paragraph.py +0 -0
  89. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/tests/test_resource_policy.py +0 -0
  90. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/tests/test_right_to_left_font_support.py +0 -0
  91. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/tests/test_selectors.py +0 -0
  92. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/tests/test_special_css.py +0 -0
  93. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/tests/test_table_border_radius.py +0 -0
  94. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/tests/test_table_css.py +0 -0
  95. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/tests/test_update_face_name.py +0 -0
  96. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/tests/test_word_breaking.py +0 -0
  97. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/tests/test_wsgi.py +0 -0
  98. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/xhtml2pdf/__main__.py +0 -0
  99. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/xhtml2pdf/_bidi_data.py +0 -0
  100. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/xhtml2pdf/bidi.py +0 -0
  101. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/xhtml2pdf/builders/__init__.py +0 -0
  102. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/xhtml2pdf/builders/flex_layout.py +0 -0
  103. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/xhtml2pdf/builders/inserts.py +0 -0
  104. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/xhtml2pdf/builders/position.py +0 -0
  105. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/xhtml2pdf/charts.py +0 -0
  106. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/xhtml2pdf/config/__init__.py +0 -0
  107. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/xhtml2pdf/config/resources.py +0 -0
  108. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/xhtml2pdf/files.py +0 -0
  109. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/xhtml2pdf/pdf.py +0 -0
  110. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/xhtml2pdf/w3c/__init__.py +0 -0
  111. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/xhtml2pdf/w3c/cssDOMElementInterface.py +0 -0
  112. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/xhtml2pdf/w3c/css_variables.py +0 -0
  113. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/xhtml2pdf/wsgi.py +0 -0
  114. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/xhtml2pdf.egg-info/dependency_links.txt +0 -0
  115. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/xhtml2pdf.egg-info/entry_points.txt +0 -0
  116. {xhtml2pdf-0.2.27 → xhtml2pdf-0.2.29}/xhtml2pdf.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: xhtml2pdf
3
- Version: 0.2.27
3
+ Version: 0.2.29
4
4
  Summary: PDF generator using HTML and CSS
5
5
  Author-email: Dirk Holtwick <dirk.holtwick@gmail.com>
6
6
  Maintainer-email: Luis Zarate <luisza14@gmail.com>, Timo Brembeck <opensource@timo.brembeck.email>
@@ -48,14 +48,19 @@ Requires-Dist: svglib>=1.2.1
48
48
  Provides-Extra: signing
49
49
  Requires-Dist: pyHanko>=0.37.0; extra == "signing"
50
50
  Requires-Dist: pyhanko-certvalidator>=0.32.1; extra == "signing"
51
+ Provides-Extra: fonts
52
+ Requires-Dist: fonttools>=4.40; extra == "fonts"
53
+ Requires-Dist: brotli>=1.0.9; extra == "fonts"
51
54
  Provides-Extra: all
52
55
  Requires-Dist: xhtml2pdf[signing]; extra == "all"
56
+ Requires-Dist: xhtml2pdf[fonts]; extra == "all"
53
57
  Provides-Extra: pycairo
54
58
  Requires-Dist: reportlab[pycairo]<6,>=4.4.9; extra == "pycairo"
55
59
  Provides-Extra: renderpm
56
60
  Requires-Dist: xhtml2pdf[pycairo]; extra == "renderpm"
57
61
  Provides-Extra: test
58
62
  Requires-Dist: xhtml2pdf[signing]; extra == "test"
63
+ Requires-Dist: xhtml2pdf[fonts]; extra == "test"
59
64
  Requires-Dist: tomli>=2.0.1; python_version < "3.11" and extra == "test"
60
65
  Requires-Dist: tox; extra == "test"
61
66
  Requires-Dist: coverage>=5.3; extra == "test"
@@ -68,9 +68,15 @@ signing = [
68
68
  "pyHanko>=0.37.0",
69
69
  "pyhanko-certvalidator>=0.32.1",
70
70
  ]
71
+ # OpenType (CFF), WOFF and WOFF2 fonts in @font-face; brotli reads WOFF2.
72
+ fonts = [
73
+ "fonttools>=4.40",
74
+ "brotli>=1.0.9",
75
+ ]
71
76
  # Every optional feature that needs nothing outside Python.
72
77
  all = [
73
78
  "xhtml2pdf[signing]",
79
+ "xhtml2pdf[fonts]",
74
80
  ]
75
81
  pycairo = [
76
82
  "reportlab[pycairo]>=4.4.9,<6",
@@ -84,6 +90,7 @@ renderpm = [
84
90
  ]
85
91
  test = [
86
92
  "xhtml2pdf[signing]",
93
+ "xhtml2pdf[fonts]",
87
94
  "tomli>=2.0.1; python_version<'3.11'",
88
95
  "tox",
89
96
  "coverage>=5.3"
@@ -123,7 +130,7 @@ legacy_tox_ini = """
123
130
  envlist = py{3.10,3.11,3.12,3.13,3.14}-rl{4,5}, py3.10-rlmin
124
131
 
125
132
  [testenv]
126
- extras = signing
133
+ extras = signing, fonts
127
134
  deps =
128
135
  coverage[toml]
129
136
  rl4: reportlab>=4.4.9,<5
@@ -325,6 +332,13 @@ Hink = "Hink"
325
332
  myu20 = "myu20"
326
333
  opf = "opf"
327
334
  PNGs = "PNGs"
335
+ # pypdf's StreamObject.flate_encode, named after the Flate filter.
336
+ flate_encode = "flate_encode"
337
+ # The TrueType glyph location table.
338
+ loca = "loca"
328
339
 
329
340
  [tool.typos.default.extend-words]
330
341
  splitted = "splitted"
342
+ # The PDF Flate filter (FlateDecode) and the stream key /DecodeParms.
343
+ Flate = "Flate"
344
+ Parms = "Parms"
@@ -126,6 +126,45 @@ class AsianFontSupportTests(TestCase):
126
126
  "Not all asian fonts detected in the PDF file!",
127
127
  )
128
128
 
129
+ def test_asian_font_cmap_matches_its_collection(self):
130
+ """
131
+ Every CJK face is written with a CMap of its own character collection.
132
+
133
+ ReportLab pairs MSung-Light (Adobe-CNS1) with UniGB-UCS2-H, which maps
134
+ to Adobe-GB1; Poppler drops a font with that pair and shows no text.
135
+ """
136
+ orderings = {
137
+ "UniGB": "GB1",
138
+ "UniCNS": "CNS1",
139
+ "UniJIS": "Japan1",
140
+ "UniKS": "Korea1",
141
+ }
142
+ with io.BytesIO() as pdf_file:
143
+ pisaDocument(src=self.HTML_CONTENT, dest=pdf_file)
144
+ pdf_file.seek(0)
145
+ reader = PdfReader(pdf_file)
146
+ found = {}
147
+ for page in reader.pages:
148
+ for ref in page["/Resources"]["/Font"].values():
149
+ font = ref.get_object()
150
+ if font.get("/Subtype") != "/Type0":
151
+ continue
152
+ descendant = font["/DescendantFonts"][0].get_object()
153
+ info = descendant["/CIDSystemInfo"].get_object()
154
+ found[str(font["/BaseFont"])[1:]] = (
155
+ str(font["/Encoding"])[1:],
156
+ str(info["/Ordering"]),
157
+ )
158
+
159
+ self.assertIn("MSung-Light", found)
160
+ self.assertEqual(("UniCNS-UCS2-H", "CNS1"), found["MSung-Light"])
161
+ mismatched = {
162
+ name: pair
163
+ for name, pair in found.items()
164
+ if orderings.get(pair[0].split("-")[0]) != pair[1]
165
+ }
166
+ self.assertEqual({}, mismatched)
167
+
129
168
  def test_get_default_asian_font(self):
130
169
  """Tests if we can successfully extract the default asian fonts"""
131
170
  DEFAULT_ASIAN_FONT = get_default_asian_font()
@@ -151,6 +190,7 @@ class AsianFontSupportTests(TestCase):
151
190
  "HeiseiMin-W3": ("jpn", "UniJIS-UCS2-H"),
152
191
  "HeiseiKakuGo-W5": ("jpn", "UniJIS-UCS2-H"),
153
192
  "STSong-Light": ("chs", "UniGB-UCS2-H"),
193
+ # The wrong CMap for this face; set_asian_fonts writes UniCNS.
154
194
  "MSung-Light": ("cht", "UniGB-UCS2-H"),
155
195
  "HYSMyeongJo-Medium": ("kor", "UniKS-UCS2-H"),
156
196
  "HYGothic-Medium": ("kor", "UniKS-UCS2-H"),
@@ -269,6 +269,77 @@ class PageTestCase(TestCase):
269
269
  reader = PdfReader(io.BytesIO(pdf))
270
270
  self.assertIn("Chapter", reader.pages[0].extract_text())
271
271
 
272
+ def test_a_long_table_in_a_box_runs_on_across_pages(self) -> None:
273
+ """A box's background or padding does not shrink the table to a page."""
274
+ rows = "".join(
275
+ f"<tr><td>row {i}</td><td>value {i}</td></tr>" for i in range(200)
276
+ )
277
+ content = f"<p>Intro.</p><table>{rows}</table>"
278
+
279
+ def pages(html: str) -> list[str]:
280
+ dest = io.BytesIO()
281
+ pisa.CreatePDF(f"<html><body>{html}</body></html>", dest=dest)
282
+ return [
283
+ p.extract_text() for p in PdfReader(io.BytesIO(dest.getvalue())).pages
284
+ ]
285
+
286
+ plain = pages(f"<div>{content}</div>")
287
+ self.assertEqual(3, len(plain))
288
+ for style in ("background: #ffffff", "padding: 0 20px"):
289
+ with self.subTest(style=style):
290
+ self.assertEqual(plain, pages(f'<div style="{style}">{content}</div>'))
291
+ nested = pages(
292
+ '<div style="padding: 0 20px">'
293
+ f'<div style="border: 1px solid black">{content}</div></div>'
294
+ )
295
+ self.assertEqual(3, len(nested))
296
+ self.assertIn("row 199", nested[-1])
297
+
298
+ def test_the_bottom_padding_does_not_go_on_alone(self) -> None:
299
+ """
300
+ Content that fits at the foot, with no room for the padding under it,
301
+ does not leave a slice of empty box on the next page.
302
+ """
303
+
304
+ def pages(html: str) -> list[list[str]]:
305
+ dest = io.BytesIO()
306
+ pisa.CreatePDF(html, dest=dest)
307
+ return [
308
+ p.extract_text().split()
309
+ for p in PdfReader(io.BytesIO(dest.getvalue())).pages
310
+ ]
311
+
312
+ note = '<div style="padding: 7pt 9pt; background: #eee">{}</div><p>after</p>'
313
+ # The frame of an A4 page is 785.2pt; the note's text ends 18.25pt in.
314
+ spacer = '<div style="height: 764pt"></div>'
315
+ boxes, _, _ = render(spacer + note.format("<p style='margin: 0'>note</p>"))
316
+ # One box, on page two, whole: a browser moves it too.
317
+ self.assertEqual([2], [b.page for b in boxes if b.style.backColor])
318
+ self.assertEqual(
319
+ [[], ["note", "after"]],
320
+ pages(spacer + note.format("<p style='margin: 0'>note</p>")),
321
+ )
322
+ # With lines to spare, the last goes on with the padding.
323
+ lines = "<p style='margin: 0'>one<br/>two<br/>three</p>"
324
+ self.assertEqual(
325
+ [["one", "two"], ["three", "after"]],
326
+ pages('<div style="height: 740pt"></div>' + note.format(lines)),
327
+ )
328
+
329
+ def test_a_declared_height_that_does_not_fit_keeps_the_box(self) -> None:
330
+ """
331
+ Content and padding fit, only the rest of a declared height does not:
332
+ the box stays on its page, as a full-page cover's band does.
333
+ """
334
+ spacer = '<div style="height: 700pt"></div>'
335
+ band = (
336
+ '<div style="padding: 7pt; height: 90pt; background: #eee">'
337
+ "<p style='margin: 0'>band</p></div>"
338
+ )
339
+ boxes, _, pdf = render(spacer + band)
340
+ self.assertEqual([1], sorted({b.page for b in boxes if b.style.backColor}))
341
+ self.assertEqual(1, len(PdfReader(io.BytesIO(pdf)).pages))
342
+
272
343
  def assertInside(self, box: Box, table: Drawn) -> None:
273
344
  self.assertEqual(box.page, table.page)
274
345
  self.assertGreaterEqual(table.x, box.x)
@@ -13,7 +13,7 @@ import os
13
13
  import subprocess
14
14
  import sys
15
15
  import tempfile
16
- from contextlib import redirect_stdout
16
+ from contextlib import redirect_stderr, redirect_stdout
17
17
  from pathlib import Path
18
18
  from unittest import TestCase, mock
19
19
 
@@ -80,6 +80,34 @@ class CommandTest(TestCase):
80
80
  self.assertTrue(dest.is_file())
81
81
  self.assertEqual("cli\nhello", PdfReader(dest).pages[0].extract_text().strip())
82
82
 
83
+ def test_pdfx_options_write_pdf_x(self) -> None:
84
+ from tests.test_print import embedded, fake_profile
85
+
86
+ self.src.write_text(embedded("<p>print</p>"), encoding="utf-8")
87
+ profile = Path(self.tmp.name) / "press.icc"
88
+ profile.write_bytes(fake_profile())
89
+ dest = Path(self.tmp.name) / "out.pdf"
90
+ code, _ = run_cli(
91
+ "-q",
92
+ "--pdfx-profile",
93
+ str(profile),
94
+ "--pdfx-condition",
95
+ "FOGRA39",
96
+ str(self.src),
97
+ str(dest),
98
+ )
99
+ self.assertEqual(0, code)
100
+ reader = PdfReader(dest)
101
+ self.assertEqual("PDF/X-4", reader.metadata["/GTS_PDFXVersion"])
102
+ intent = reader.trailer["/Root"]["/OutputIntents"][0].get_object()
103
+ self.assertEqual("FOGRA39", intent["/OutputConditionIdentifier"])
104
+
105
+ def test_pdfx_condition_without_a_profile_is_a_usage_error(self) -> None:
106
+ dest = Path(self.tmp.name) / "out.pdf"
107
+ with redirect_stderr(io.StringIO()):
108
+ code, _ = run_cli("--pdfx-condition", "FOGRA39", str(self.src), str(dest))
109
+ self.assertEqual(2, code)
110
+
83
111
  def test_destination_defaults_to_the_source_name(self) -> None:
84
112
  cwd = os.getcwd()
85
113
  os.chdir(self.tmp.name)
@@ -537,7 +537,7 @@ class SourceTest(ParserTestCase):
537
537
  def test_unimplemented_properties_are_named_by_source(self) -> None:
538
538
  output, _ = self._render(
539
539
  '<style>p { box-shadow: 1px 1px red }</style><link rel="stylesheet"'
540
- ' href="ext.css"><p style="opacity: 0.5; width: min(1pt, 2pt)">y</p>',
540
+ ' href="ext.css"><p style="cursor: help; width: min(1pt, 2pt)">y</p>',
541
541
  {"ext.css": "p { float: left }"},
542
542
  )
543
543
  unimplemented = [
@@ -554,7 +554,7 @@ class SourceTest(ParserTestCase):
554
554
  self.assertTrue(ext.endswith(": float"), ext)
555
555
  # A style attribute used to be read without a word about it.
556
556
  self.assertTrue(attribute.startswith("the style attribute of <p>: "))
557
- self.assertTrue(attribute.endswith(": opacity"), attribute)
557
+ self.assertTrue(attribute.endswith(": cursor"), attribute)
558
558
  self.assertIn(
559
559
  "the style attribute of <p>: Ignoring CSS declarations whose value"
560
560
  " xhtml2pdf cannot evaluate: width: min()",
@@ -322,6 +322,20 @@ class FlexRenderTest(TestCase):
322
322
  self.assertIn("SIDE", text)
323
323
  self.assertIn("CELL", text)
324
324
 
325
+ def test_a_long_table_item_keeps_its_rows_on_one_line(self) -> None:
326
+ """Its columns follow their content, so no cell wraps on later pages."""
327
+ rows = "".join(
328
+ f"<tr><td>row {i}</td><td>value {i}</td></tr>" for i in range(200)
329
+ )
330
+ table = f"<table>{rows}</table>"
331
+ plain = [p.extract_text() for p in self._render(f"<div>{table}</div>").pages]
332
+ for direction in ("row", "column"):
333
+ with self.subTest(direction=direction):
334
+ reader = self._render(
335
+ f"<div style='display:flex; flex-direction:{direction}'>{table}</div>"
336
+ )
337
+ self.assertEqual(plain, [p.extract_text() for p in reader.pages])
338
+
325
339
 
326
340
  class FlexWrapTest(TestCase):
327
341
  def test_wrap_makes_lines(self) -> None:
@@ -12,6 +12,8 @@ announced.
12
12
  from __future__ import annotations
13
13
 
14
14
  import logging
15
+ import shutil
16
+ import tempfile
15
17
  from io import BytesIO
16
18
  from pathlib import Path
17
19
  from unittest import TestCase
@@ -255,3 +257,27 @@ class HashPrefixedFamilyTest(TestCase):
255
257
  fonts = base_fonts(render(html))
256
258
 
257
259
  self.assertTrue(any("NotoSans" in name for name in fonts), sorted(fonts))
260
+
261
+
262
+ class Type1FontTest(TestCase):
263
+ """A Type 1 face is its .pfb outlines and its .afm metrics, side by side."""
264
+
265
+ def setUp(self) -> None:
266
+ import reportlab
267
+
268
+ directory = tempfile.TemporaryDirectory()
269
+ self.addCleanup(directory.cleanup)
270
+ self.directory = Path(directory.name)
271
+ fonts = Path(reportlab.__file__).parent / "fonts"
272
+ for suffix in ("afm", "pfb"):
273
+ shutil.copy(fonts / f"DarkGardenMK.{suffix}", self.directory)
274
+
275
+ def test_either_file_of_the_pair_embeds_the_face(self) -> None:
276
+ for suffix in ("afm", "pfb"):
277
+ with self.subTest(suffix):
278
+ html = document("Garden", src=f"DarkGardenMK.{suffix}", text="abc")
279
+ output = BytesIO()
280
+ pisa.CreatePDF(
281
+ html, dest=output, path=str(self.directory / "document.html")
282
+ )
283
+ self.assertIn("/DarkGardenMK", base_fonts(output.getvalue()))
@@ -0,0 +1,65 @@
1
+ from __future__ import annotations
2
+
3
+ import ssl
4
+ from unittest import TestCase
5
+
6
+ from xhtml2pdf.config.httpconfig import HttpConfig
7
+
8
+
9
+ class HttpConfigTest(TestCase):
10
+ def test_default_timeout(self) -> None:
11
+ self.assertEqual({"timeout": 5}, HttpConfig())
12
+
13
+ def test_constructor_arguments_are_kept(self) -> None:
14
+ config = HttpConfig(source_address=("127.0.0.1", 0))
15
+ self.assertEqual(("127.0.0.1", 0), config["source_address"])
16
+
17
+ def test_a_timeout_given_to_the_constructor_is_kept(self) -> None:
18
+ self.assertEqual(30, HttpConfig(timeout=30)["timeout"])
19
+ self.assertEqual(30, HttpConfig({"timeout": 30})["timeout"])
20
+
21
+ def test_http_options_are_stored_without_prefix(self) -> None:
22
+ for option in ("--http_timeout", "-http_timeout", "http_timeout"):
23
+ with self.subTest(option=option):
24
+ config = HttpConfig()
25
+ self.assertTrue(config.is_http_config(option, 10))
26
+ self.assertEqual(10, config["timeout"])
27
+
28
+ def test_http_options_with_other_keys(self) -> None:
29
+ config = HttpConfig()
30
+ self.assertTrue(config.is_http_config("--http_key_file", "client.key"))
31
+ self.assertTrue(config.is_http_config("--http_cert_file", "client.crt"))
32
+ self.assertEqual("client.key", config["key_file"])
33
+ self.assertEqual("client.crt", config["cert_file"])
34
+
35
+ def test_non_http_options_are_ignored(self) -> None:
36
+ for option in ("--encoding", "-q", "debug"):
37
+ with self.subTest(option=option):
38
+ config = HttpConfig()
39
+ self.assertFalse(config.is_http_config(option, "value"))
40
+ self.assertEqual({"timeout": 5}, config)
41
+
42
+ def test_nosslcheck_creates_unverified_context(self) -> None:
43
+ config = HttpConfig()
44
+ self.assertTrue(config.is_http_config("--http_nosslcheck", ""))
45
+
46
+ self.assertNotIn("nosslcheck", config)
47
+ context = config["context"]
48
+ self.assertIsInstance(context, ssl.SSLContext)
49
+ self.assertEqual(ssl.CERT_NONE, context.verify_mode)
50
+ self.assertFalse(context.check_hostname)
51
+
52
+ def test_save_keys_stores_value(self) -> None:
53
+ config = HttpConfig()
54
+ config.save_keys("timeout", 1)
55
+ self.assertEqual(1, config["timeout"])
56
+
57
+ def test_repr_lists_every_item(self) -> None:
58
+ config = HttpConfig()
59
+ config["cert_file"] = "client.crt"
60
+ self.assertEqual("'timeout' = 5, 'cert_file' = 'client.crt', ", repr(config))
61
+
62
+ def test_repr_of_empty_config(self) -> None:
63
+ config = HttpConfig()
64
+ config.clear()
65
+ self.assertEqual("", repr(config))
@@ -0,0 +1,100 @@
1
+ """Where a paragraph's text sits in its lines: CSS half-leading."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import io
6
+ from itertools import pairwise
7
+ from unittest import TestCase
8
+
9
+ from pypdf import PdfReader
10
+ from reportlab.pdfbase.pdfmetrics import getAscentDescent
11
+
12
+ from xhtml2pdf import pisa
13
+ from xhtml2pdf.document import pisaStory
14
+ from xhtml2pdf.xhtml2pdf_reportlab import PmlParagraph
15
+
16
+ FONT_SIZE = 10.0
17
+ #: Top of the default frame: a 1cm page margin.
18
+ FRAME_TOP = 28.3465
19
+
20
+
21
+ def first_paragraph(html: str) -> PmlParagraph:
22
+ story = pisaStory(html).story
23
+ para = next(f for f in story if isinstance(f, PmlParagraph))
24
+ para.wrap(400, 800)
25
+ return para
26
+
27
+
28
+ def baselines(html: str) -> list[float]:
29
+ """Distance from the top of the page to each baseline, in stream order."""
30
+ out = io.BytesIO()
31
+ result = pisa.CreatePDF(html, dest=out)
32
+ assert not result.err
33
+ page = PdfReader(io.BytesIO(out.getvalue())).pages[0]
34
+ height = float(page.mediabox.height)
35
+ found: list[float] = []
36
+
37
+ def visit(text, cm, tm, _font, _size) -> None:
38
+ if text.strip():
39
+ found.append(round(height - (tm[5] * cm[3] + cm[5]), 3))
40
+
41
+ page.extract_text(visitor_text=visit)
42
+ return found
43
+
44
+
45
+ class HalfLeadingTest(TestCase):
46
+ @staticmethod
47
+ def expected_first_baseline(leading: float) -> float:
48
+ ascent, descent = getAscentDescent("Helvetica", FONT_SIZE)
49
+ return ascent + (leading - (ascent - descent)) / 2
50
+
51
+ def test_the_leading_is_shared_above_and_below(self) -> None:
52
+ for line_height, leading in (("150%", 15.0), ("100%", 10.0), ("200%", 20.0)):
53
+ with self.subTest(line_height=line_height):
54
+ para = first_paragraph(
55
+ f'<p style="font-size: {FONT_SIZE}pt; line-height: {line_height}">'
56
+ "one line</p>"
57
+ )
58
+ self.assertAlmostEqual(
59
+ self.expected_first_baseline(leading),
60
+ para._firstBaseline(),
61
+ places=3,
62
+ )
63
+
64
+ def test_plain_lines_keep_their_height(self) -> None:
65
+ para = first_paragraph(
66
+ f'<p style="font-size: {FONT_SIZE}pt; line-height: 150%">'
67
+ + "word " * 300
68
+ + "</p>"
69
+ )
70
+ lines = len(para.blPara.lines)
71
+ self.assertGreater(lines, 1)
72
+ self.assertAlmostEqual(lines * 15.0, para.height, places=3)
73
+
74
+ def test_lines_step_by_the_leading(self) -> None:
75
+ found = baselines(
76
+ f'<p style="font-size: {FONT_SIZE}pt; line-height: 150%">'
77
+ + "word " * 300
78
+ + "</p>"
79
+ )
80
+ steps = {round(b - a, 3) for a, b in pairwise(found)}
81
+ self.assertEqual({15.0}, steps)
82
+
83
+ def test_a_bigger_font_still_grows_its_line(self) -> None:
84
+ plain = first_paragraph(f'<p style="font-size: {FONT_SIZE}pt">x</p>')
85
+ mixed = first_paragraph(
86
+ f'<p style="font-size: {FONT_SIZE}pt">x <span style="font-size: 30pt">'
87
+ "BIG</span> x</p>"
88
+ )
89
+ self.assertGreater(mixed.height, plain.height)
90
+ big_ascent = getAscentDescent("Helvetica", 30)[0]
91
+ self.assertGreaterEqual(mixed._firstBaseline(), big_ascent)
92
+
93
+ def test_the_drawn_text_is_where_the_paragraph_says(self) -> None:
94
+ html = (
95
+ '<div style="font-size: 10pt; line-height: 150%">'
96
+ "<p style='margin: 0'>first</p></div>"
97
+ )
98
+ para = first_paragraph(html)
99
+ top = baselines(html)[0]
100
+ self.assertAlmostEqual(FRAME_TOP + para._firstBaseline(), top, places=2)
@@ -0,0 +1,56 @@
1
+ """line-height, computed and inherited as CSS 2.1 10.8 says."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from unittest import TestCase
6
+
7
+ from xhtml2pdf.document import pisaStory
8
+ from xhtml2pdf.util import leadingFrom, lineHeight
9
+ from xhtml2pdf.xhtml2pdf_reportlab import PmlParagraph
10
+
11
+
12
+ def leadings(html: str) -> list[float]:
13
+ story = pisaStory(html).story
14
+ return [round(f.style.leading, 3) for f in story if isinstance(f, PmlParagraph)]
15
+
16
+
17
+ def nested(line_height: str) -> list[float]:
18
+ """The leading of a 10pt paragraph and of a 20pt one under the same div."""
19
+ return leadings(
20
+ f'<div style="font-size: 10pt; line-height: {line_height}">'
21
+ '<p>small</p><p style="font-size: 20pt">big</p></div>'
22
+ )
23
+
24
+
25
+ class LineHeightTest(TestCase):
26
+ def test_a_number_multiplies_each_elements_own_size(self) -> None:
27
+ self.assertEqual([15.0, 30.0], nested("1.5"))
28
+
29
+ def test_a_whole_number_is_a_number(self) -> None:
30
+ # Not one of HTML's <font size> steps, which read 2 as 0.75em.
31
+ self.assertEqual([20.0, 40.0], nested("2"))
32
+
33
+ def test_a_percentage_is_resolved_where_it_is_declared(self) -> None:
34
+ self.assertEqual([15.0, 15.0], nested("150%"))
35
+
36
+ def test_em_is_resolved_where_it_is_declared(self) -> None:
37
+ self.assertEqual([15.0, 15.0], nested("1.5em"))
38
+
39
+ def test_a_length_is_kept(self) -> None:
40
+ self.assertEqual([12.0, 12.0], nested("12pt"))
41
+
42
+ def test_normal_is_the_font_size(self) -> None:
43
+ self.assertEqual([10.0, 20.0], nested("normal"))
44
+
45
+ def test_the_default_follows_each_size(self) -> None:
46
+ # DEFAULT_CSS gives html line-height: 1.5, a number.
47
+ self.assertEqual(
48
+ [15.0, 30.0],
49
+ leadings(
50
+ '<p style="font-size: 10pt">a</p><p style="font-size: 20pt">b</p>'
51
+ ),
52
+ )
53
+
54
+ def test_inherit_takes_the_parents_source(self) -> None:
55
+ self.assertEqual((30.0, "1.5"), lineHeight("inherit", 20.0, "1.5"))
56
+ self.assertEqual(15.0, leadingFrom("15.0pt", 40.0))
@@ -0,0 +1,64 @@
1
+ """The vertical margins of a list, as a browser gives them."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from unittest import TestCase
6
+
7
+ from reportlab.platypus import Spacer
8
+
9
+ from xhtml2pdf.document import pisaStory
10
+ from xhtml2pdf.xhtml2pdf_reportlab import PmlParagraph
11
+
12
+ FONT = "font-size: 10pt; "
13
+
14
+
15
+ def paragraphs(html: str) -> dict[str, tuple[float, float]]:
16
+ """Text -> (space before, space after) of each paragraph of the story."""
17
+ story = pisaStory(html).story
18
+ found = {}
19
+ for flowable in story:
20
+ if isinstance(flowable, PmlParagraph):
21
+ flowable.wrap(400, 800)
22
+ found[flowable.getPlainText().strip()] = (
23
+ flowable.getSpaceBefore(),
24
+ flowable.getSpaceAfter(),
25
+ )
26
+ return found
27
+
28
+
29
+ class ListMarginTest(TestCase):
30
+ def test_a_list_has_an_em_above_and_below(self) -> None:
31
+ found = paragraphs(
32
+ f'<p style="margin: 0">before</p><ul style="{FONT}"><li>one</li>'
33
+ '<li>two</li></ul><p style="margin: 0">after</p>'
34
+ )
35
+ self.assertEqual(10.0, found["one"][0])
36
+ self.assertEqual(10.0, found["two"][1])
37
+
38
+ def test_a_list_in_a_list_has_none(self) -> None:
39
+ found = paragraphs(
40
+ f'<ul style="{FONT}"><li>outer<ul><li>inner</li></ul></li>'
41
+ "<li>after</li></ul>"
42
+ )
43
+ self.assertEqual((0.0, 0.0), found["inner"])
44
+
45
+ def test_the_last_items_margin_collapses_with_the_lists(self) -> None:
46
+ story = pisaStory(
47
+ '<ul style="margin: 0 0 20pt 0"><li><p style="margin: 0 0 5pt 0">'
48
+ 'a</p></li></ul><p style="margin: 0">after</p>'
49
+ ).story
50
+ # The larger of the two, on the item, and no spacer for the list.
51
+ self.assertFalse(any(isinstance(f, Spacer) for f in story))
52
+ found = paragraphs(
53
+ '<ul style="margin: 0 0 20pt 0"><li><p style="margin: 0 0 5pt 0">'
54
+ 'a</p></li></ul><p style="margin: 0">after</p>'
55
+ )
56
+ self.assertEqual(20.0, found["a"][1])
57
+
58
+ def test_margin_zero_keeps_a_list_tight(self) -> None:
59
+ found = paragraphs(
60
+ '<p style="margin: 0">before</p>'
61
+ '<ul style="margin-top: 0; margin-bottom: 0"><li>one</li></ul>'
62
+ '<p style="margin: 0">after</p>'
63
+ )
64
+ self.assertEqual((0.0, 0.0), found["one"])