Preserve leading spaces on new lines in <pre> blocks#1866
Merged
andersonhc merged 2 commits intoJun 17, 2026
Conversation
Fixes py-pdf#1063. The skip_leading_spaces default of Paragraph and ParagraphCollectorMixin.paragraph() was False, so an explicitly passed False got swallowed by the `or self._region.skip_leading_spaces` fallback whenever the region had skip_leading_spaces=True, which is how the HTML parser configures it. As a result, leading whitespace inside <pre> and <pre><code> blocks was dropped. The default is now None, and an explicit True or False always wins over the region default, resolved once when the paragraph is constructed. build_lines() and the bullet renderer use the paragraph's own resolved value instead of falling back to the region again. In html.py, _new_paragraph() now passes skip_leading_spaces=not self._pre_formatted, so preformatted blocks keep their leading whitespace while normal blocks behave as before. A regression test based on the issue's minimal example is added in test/html/.
andersonhc
approved these changes
Jun 17, 2026
andersonhc
left a comment
Collaborator
There was a problem hiding this comment.
Thank you for the contribution.
I don't have any comment - the PR should be ready to be merged as soon as I fix the problems with the github actions workflow.
Collaborator
|
@allcontributors please add @eugen-goebel for code |
|
I've put up a pull request to add @eugen-goebel! 🎉 |
Collaborator
|
Merged. Thank you @eugen-goebel |
Author
|
Thanks a lot for the review and the quick merge, and for adding me to the contributors list. This was my first contribution here and it was a really good experience. I will keep an eye out for more issues I can help with. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1063.
Leading spaces on new lines inside
<pre>and<pre><code>blocks were dropped. The root cause is thatParagraphandParagraphCollectorMixin.paragraph()hadskip_leading_spacesdefaulting toFalse, and the effective value was computed asskip_leading_spaces or self._region.skip_leading_spaces. Since the HTML parser creates its text region withskip_leading_spaces=True, an explicitly passedFalsewas always swallowed by theor.Following @gmischler's plan from the issue thread:
fpdf/text_region.pythe default is nowNonein bothParagraph.__init__andparagraph(), and an explicitTrueorFalsealways wins over the region default. The value is resolved once when the paragraph is constructed, andbuild_lines()and the bullet renderer use the paragraph's own value instead of falling back to the region again. This is the part @dmail00 pointed out was still missing.fpdf/html.py_new_paragraph()now passesskip_leading_spaces=not self._pre_formatted, so preformatted blocks keep their leading whitespace while every other block behaves exactly as before.I added a regression test in
test/html/based on the minimal example from the issue. The existingtest_html_whitespace_handlingreference is unchanged, since its<pre>lines have no leading spaces.Checklist:
docs/folder — N/A (bugfix)CHANGELOG.mdBy submitting this pull request, I confirm that my contribution is made under the terms of the GNU LGPL 3.0 license.