py-pdf/fpdf2

Nested HTML lists start with a newline

lcgeneralprojects opened this issue · 2 comments

Describe the bug
An HTML list nested in another HTML list erroneously starts with a newline.

Minimal code
Please include some minimal Python code reproducing your issue:

from fpdf import FPDF

if __name__ == '__main__':
    pdf = FPDF()
    pdf.add_page()
    pdf.write_html(f"""<ul>
        <li>First-order list item</li>
        <li><ul>
                <li>Second-order list item</li>
                <li>Second-order list item</li>
            </ul>
        </li>
    </ul>""")
    pdf.output("test_nested_lists.pdf")

Environment
Please provide the following information:

  • Operating System: Windows
  • Python version: Python 3.12.2
  • fpdf2 version used: current version of the master branch of the remote repository (latest tag is 2.7.8)

I did some analysis here, comparing the HTML specs and the actual behaviour of some popular web browsers, and fount that:

  • By the specs, every list (including nested lists) should have a margin of 1em both above and below it.
  • With nested lists, browsers vary in how they implement the top margin, but usually omit the bottom margin.
  • Our current output is quite similar to what the browsers do (implementing half the spec)
  • #1170 will eliminate the top margin of nested lists, technically violating the HTML specs, but looking more balanced and reasonable then what the browsers do.

Fixed in #1170.