You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
How to prevent boundsOfString to create new page #1625
I am using boundsOfString(...) to calculate the bounds of a string to ensure it fits on the current page. Unfortunately calling this function creates a new page if the text doesn't fit on the page.
How can I prevent a new page to be created?
Code sample
doc.boundsOfString("some long text ... ");
Your environment
pdfkit version: 0.17.1
Node version: 22.14
Browser version (if applicable): (running on node)
@blikblum, we are likely going to need to overhaul line_wrapper to do two things:
Compute a virtual layout of text without modifying anything in the document
Use virtual layout to render text to document with precision
This would also allow us to tackle the issue of numerical precision. as well as decouple the rendering and layout system, rather than using the current approach of trying to rollback the document state after running the line_wrapper.wrap()
Splitting this logic enables us to do correct calculations for sizing, i.e. doc.widthOfString(), doc.heightOfString(), doc.boundOfString().
I'll try to get a POC working, this will also massively improve the table rendering pipeline too
For now I am using a workaround. In my case I need to generate exactly one page and I need to have the font size as big a possible but it needs to fit on one page.
Therefore, I am trying with the largest font size using boundsOfString. If there is a second page I try again with a smaller font size until there is no new page anymore. At the very end I use the pdf-lib package to only get the first page and remove all other pages generated by boundsOfString.
Not the nicest way, but it works to a certain level.
I think the best way to check whether the text fits on the current page is to use
(text as NSString).boundingRect(...), because it only calculates the text size without affecting the layout.
boundsOfString(...), as you mentioned, actually lays out the text and if it doesn't fit, it creates a new page.
So for measurement only, boundingRect is the correct approach.
Is this correct?
Activity
hollandjake commentedon Jun 18, 2025
Definitely a bug, investigating now.
@blikblum this issue cause is in the line_wrapper, so also affects
heightOfStringhollandjake commentedon Jun 18, 2025
@blikblum, we are likely going to need to overhaul line_wrapper to do two things:
This would also allow us to tackle the issue of numerical precision. as well as decouple the rendering and layout system, rather than using the current approach of trying to rollback the document state after running the
line_wrapper.wrap()Splitting this logic enables us to do correct calculations for sizing, i.e.
doc.widthOfString(),doc.heightOfString(),doc.boundOfString().I'll try to get a POC working, this will also massively improve the table rendering pipeline too
raman-nbg commentedon Aug 16, 2025
For now I am using a workaround. In my case I need to generate exactly one page and I need to have the font size as big a possible but it needs to fit on one page.
Therefore, I am trying with the largest font size using
boundsOfString. If there is a second page I try again with a smaller font size until there is no new page anymore. At the very end I use thepdf-libpackage to only get the first page and remove all other pages generated byboundsOfString.Not the nicest way, but it works to a certain level.
potatopri commentedon Oct 10, 2025
I think the best way to check whether the text fits on the current page is to use
(text as NSString).boundingRect(...), because it only calculates the text size without affecting the layout.
boundsOfString(...), as you mentioned, actually lays out the text and if it doesn't fit, it creates a new page.
So for measurement only, boundingRect is the correct approach.
Is this correct?