Skip to content

How to prevent boundsOfString to create new page #1625

Open

Description

@raman-nbg

Question

Description

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)
  • Operating System: macos

Activity

  1. hollandjake commented on Jun 18, 2025

    @hollandjake
    Contributor

    Definitely a bug, investigating now.

    @blikblum this issue cause is in the line_wrapper, so also affects heightOfString

  2. hollandjake commented on Jun 18, 2025

    @hollandjake
    Contributor

    @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

  3. raman-nbg commented on Aug 16, 2025

    @raman-nbg
    Author

    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.

  4. potatopri commented on Oct 10, 2025

    @potatopri

    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?

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions