Repository navigation
Template.split_multicell(): measure the element width in the template unit, not always in mm - #2140
Merged
andersonhc merged 2 commits intoNov 10, 2026
Conversation
… unit, not always in mm
andersonhc
approved these changes
Nov 10, 2026
Collaborator
|
@allcontributors please add @Enesuygurs for code |
|
I've put up a pull request to add @Enesuygurs! 🎉 |
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.
Template.split_multicell()(andFlexTemplate.split_multicell()) ignored the unit of the template document. It measures the text in a separate, privateFPDFinstance, so that the target document is not touched (issue #238), but that instance was created withFPDF(), i.e. always in millimeters. The element widthx2 - x1is given in the template's own unit, though, so as soon as a template was defined in anything other than mm, the text was split for the wrong width.With an element that is 40 mm wide, defined in points:
Before: the width was read as 113.39 mm, so the lines came back almost three times wider than the element (the first one is about 293 pt wide for a 113 pt box). With
unit="in"the same element is 1.57 "mm" wide and the call fails withFPDFException: Not enough horizontal space to render a single character.After: the lines are the same as with the equivalent mm template, and each of them fits in the element.
The fix creates the splitting
FPDFwith the unit of the target document (FPDF(unit=self.pdf.k)), so the width is measured exactly as when the element is rendered.The existing
test_template_split_multicelltest usedunit="pt"withx1=20, x2=60, but its expected lines were only right because of this bug: they are the lines for a 40 mm wide element (a 40 pt wide box could not even hold "Lorem ipsum dolor" at 12 pt). I parametrized it over mm, cm, in and pt with the same 40 mm wide element, and it now checks that all four units give the same lines. Without the fix the cm, in and pt cases fail; with it all of them pass. The full test suite passes locally, and black, pylint and mypy are clean.I did not find an existing issue for this, so there is no "Fixes #N".
Checklist:
A unit test is covering the code added / modified by this PR
In case of a new feature, docstrings have been added, with also some documentation in the
docs/folder N/AA mention of the change is present in
CHANGELOG.mdThis PR is ready to be merged
By submitting this pull request, I confirm that my contribution is made under the terms of the GNU LGPL 3.0 license.