question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Get height and width of images and texts before render them

See original GitHub issue

Hi @devongovett ! I’m react-pdf creator. We are following your advice of using pdfkit as a render engine, unless until we need to use some more complex stuff that pdfkit currently doesn’t have. Just let me say that is working great so far!

However, I’ll need to know the bounding box of texts and images before render them on the document. Is that even possible? This is something crucial for us, since we need to update yoga layout to know the text’s x and y.

I saw that text has widthOfString and heightOfString, but they not return what I expect. What’s more, heightOfString makes the program crash.

Thank in advance!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
devongovettcommented, May 23, 2017

widthOfString returns the width of the given text as if it were all on one line. It uses font metrics scaled to the current font (set with doc.font and font size (set with doc.fontSize(x)).

heightOfString requires you already know your desired width to wrap within. You can call it with doc.heightOfString(text, {width: x}). Couldn’t reproduce the crash here, so can you provide a stack trace if it’s still broken?

0reactions
TBGerstenbergcommented, Feb 2, 2021

Hi,

I know this issue is quite old, yet I wonder if someone managed to figure the bounding box of images out before rendering (as part of the original question).

I am rendering my images using the “fit” option, which scales the image to fit into a bounding box of predefined dimensions. To calculate the space an image would take when rendered like this, I have tried loading the image with document.open(imageUrl) and performing the same scaling calculations as I assume PDFKit is doing, which is pretty standard:

if (options.fit) {
    [bw, bh] = options.fit;
    bp = bw / bh;
    ip = image.width / image.height;
    if (ip > bp) {
        w = bw;
        h = bw / ip;
    } else {
        h = bh;
        w = bh * ip;
    }
}

from : https://github.com/foliojs/pdfkit/blob/326350b7f958ed24730ce809b98dc803d3e14562/lib/mixins/images.js

yet after rendering the image, the actual height it takes is a lot larger (moves document.y more) than I calculated before rendering. Heres how i’m calculating the vertical space my image is (supposedly) taking:

...

let boundingRectWidth = MAX_COLUMN_WIDTH;
let boundingRectHeight = 
imageMeta.orientation === 'LANDSCAPE'
          ? MAX_IMAGE_HEIGHT_LANDSCAPE
          : MAX_IMAGE_HEIGHT_PORTRAIT;

let bp = boundingRectWidth / boundingRectHeight; // These are the values that are set in the "fit" option when rendering
let ip = image.width / image.height; // These are the actual image dimensions 

if (ip > bp) {
   heightOfImage = boundingRectHeight / ip;
} else {
   heightOfImage = boundingRectHeight;
}

...

Any idea why that would be happening?

Thanks in advance.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to get text size or view size before rendering in React ...
I think this would help you. From here you can get the Dimensionn of the view.
Read more >
Setting Height And Width On Images Is Important Again
To prevent layout shifts and improve performance scores, we need to always set width and height attributes on our images.
Read more >
Multimedia: Images - Learn web development | MDN
This article looks at optimizing image and video to improve web ... For this reason, it's critical that you set width and height...
Read more >
Change the size of a picture, shape, text box, or WordArt
What do you want to do? Resize a picture, shape, WordArt, or other object; Resize an item to an exact height and width;...
Read more >
Dynamically get image dimensions from image URL in React
The usual way you would go about it is to have an API that gives back the media metadata from the server. But...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found