Skip to content

WeasyPrint truncates PDF files when HTML carries style height:100% and encounters unlucky layout #2798

Description

@celskeggs

I encountered a very odd bug where an unlucky layout resulted in my mkdocs-to-pdf output getting arbitrarily cut off after page 161 of 726. The rest of the pages were entirely missing.

I created a simple reproduction case:

import weasyprint
weasyprint.HTML(string='<html style="height:100%"><body><h3 style="padding-top: 914.8px;">Header</h3><hr/>Overflow</body></html>').render().write_pdf("minimal.pdf")

Example output: minimal.pdf. Note that the string "Overflow" is mysteriously absent from the output.

Here is my proposed patch:

diff --git a/weasyprint/layout/block.py b/weasyprint/layout/block.py
index 0d76f798..baca686e 100644
--- a/weasyprint/layout/block.py
+++ b/weasyprint/layout/block.py
@@ -806,7 +806,8 @@ def block_container_layout(context, box, bottom_space, skip_stack, page_is_empty
                 max_lines)
         elif stop:
             # Stop after the rendering of child.
-            if box.height != 'auto':
+            style_height = box.style.get('height', 'auto')
+            if box.height != 'auto' and style_height != 'auto' and getattr(style_height, 'unit') != '%':
                 box_bottom = box.position_y + box.border_height()
                 bottom_margin = collapse_margin(adjoining_margins)
                 if context.overflows(box_bottom, position_y - bottom_margin):

I believe the problem is that box.height != 'auto' does not necessarily mean that the user defined a fixed height with the intention of cutting off any content beyond that height. In particular, this is untrue when the height is expressed as a percentage.

Example output with fix applied: minimal-fixed.pdf

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugExisting features not working as expected

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions