pdfmake icon indicating copy to clipboard operation
pdfmake copied to clipboard

Images are up-scaled by pdfmake and look blurry

Open Bangybug opened this issue 11 years ago • 12 comments

I'm trying to use pdfmake to render math formulas into pdf. Formulas are generated by a php script "phpmathpublisher.php"

The formulas are always up-scaled by pdfmake (or pdfkit - I don't know).

The playground of pdfmake http://pdfmake.org/playground.html illustrates this problem well. The original size of the image in the playground is 695 x 310, but it is rendered at a larger scale in the pdf.

I think that the DPI of the Pdf is affecting the images that way. I'm not sure what units are used when specifying fit/width/height of an image in Pdfmake. Looks like Pdfmake is using pixels, but PDF kit is using points. PDF make does not properly transform pixels to points. That's my wild guess.

I found similar issues: https://github.com/bpampuch/pdfmake/issues/67

Bangybug avatar Jun 18 '15 12:06 Bangybug

I'm seeing the same issue. If I pass a 900x600 pixel image, the image is scaled by 4/3 in the resulting pdf.

snarkitect avatar Oct 13 '15 00:10 snarkitect

It appears this up-scaling is happening b/c the default unit for pdfmake is points. When no size is passed for the image, it gets the dimensions from the parsed image's header information. However, the image's dimensions in the header are in pixels "PNG - Width and height give the image dimensions in pixels.". So, when pdfmake determines the size of the image, that value is in pixels, but it assumes the value is in points - resulting in the 4/3 scaling which we see in the pdf.

I've been trying to modify pdfmake.js to convert the pixel values to points before inserting the image onto the page, but so far, am not having any luck.

Has anyone else encountered this and have a working fix or workaround? I will continue working on this, but if someone else has already accomplished what I'm trying to do, I'd love to get that code!!

stephenanders avatar May 02 '16 15:05 stephenanders

As a temporary workaround I premultiply the width of my image with 0.7487922705314 (the height is adjusted by pdfmake automatically)

Bangybug avatar May 02 '16 15:05 Bangybug

@Bangybug at what point did you do this premultiplication? I tried it at the JSON level with something like:

{
    image: options.productBarChart,
    width: 515 * 0.7487922705314
}

However it's still blurry.. just with less width as expected.

christomitov avatar May 24 '16 15:05 christomitov

I'd also like to get a fix for this figured out.

s7726 avatar Jun 01 '16 14:06 s7726

@s7726 I forgot to mention, this was actually a problem with the image and not PDFmake. What I was doing was rendering an image from Google charts using PhantomJS. By default this image was low DPI but I was able to hack around that by doing page.zoomFactor = 300.0/72.0; . Not sure this will help your specific case but maybe if someone else stumbles on this and is doing something similar it'll save them some time.

christomitov avatar Jun 01 '16 14:06 christomitov

My case is an 816x108px png that is supposed to be like letter head on a US Letter size document. It fits and renders nicely in MS Word and in a normal image viewer. It turns out fuzzy and low resolution in the PDF output.

s7726 avatar Jun 01 '16 20:06 s7726

+1

csvan avatar Dec 14 '16 10:12 csvan

@Bangybug where does this 0.7487922705314 come from?

nettiopsu avatar May 03 '18 11:05 nettiopsu

@nettiopsu it should be 0.75 (3/4) as stated by stephenanders Pdfmake expects the size in points, not in pixels. And we know that 1 pixel is 0.75 pt.

Bangybug avatar May 04 '18 09:05 Bangybug

How to fit different size images over the pdf?

Ex - width 562 height 1000 width 1266 height 650 width 1920 height 1200

12smc120 avatar Jan 25 '19 06:01 12smc120

guys its 2023 and am stuck on this problem if anyone solved it please share

Phero49 avatar May 24 '23 18:05 Phero49