Menu

TCPDF - Creating PDFs for Printing

Help
dtirer
2014-10-20
2014-12-05
  • dtirer

    dtirer - 2014-10-20

    I'm using TCPDF to create PDFs for printing. I am wondering, how do I deal with scaling both fonts and images from 72 dpi to 300 dpi for PDF printing? Are there settings in the code for this?

     
  • cpw

    cpw - 2014-12-05

    Fonts are described by vectors, they don't have a resolution at all. Or technically speaking: any resolution you like, they're scalable without any loss of quality.

    Concerning images you should be good to go by just using large images (generally meaning: the more pixels, the better) and setting a width and/or a height.

    BTW: dpi's don't mean anything without a reference size - an image file itself has no dpi value (to be precise: it might have one, but that's only metadata that you can set as high or low as you like in your image editor). People are quite commonly confused with that, so here's a short example (image pixel dimensions in the filenames):

    $pdf->Image($file = "image_1_1600x849.jpg", $x = 0, $y = 0, $w = 100, $h = 0, $type = '', $link = '', $align = '', $resize = false);
    
    $pdf->Image($file = "image_2_400x212.jpg", $x = 0, $y = 60, $w = 100, $h = 0, $type = '', $link = '', $align = '', $resize = false);
    
    $pdf->Image($file = "image_3_1600x849.jpg", $x = 0, $y = 120, $w = 100, $h = 0, $type = '', $link = '', $align = '', $resize = true, $dpi = 72);
    

    All three images in the PDF have a width of 100 mm (10 cm or 3.94 inches) and an automatically calculated height, so the aspect ratio is kept. All three are displayed in exactly the same size in the PDF document.

    If you zoom into this PDF (let's say 650%), you'll notice that image 1 still looks pretty good, image 2 is pixelated and blurred, image 3 is more or less a blocky mess. When printing this document with a good high resolution printer you'll notice exactly the same thing: image 1 will look the best, image 3 the worst.

    Here's where dpi comes into play:

    Image 1 has a pixel dimension of 1600 x 849 and needs to be printed in 100 x 53 mm, respectively 10.0 x 5.3 cm, respectively (divided by 2.54) 3.94 inch x 2.09 inch.

    So we have 1600 pixels for 3.94 inch => 1600 pixels / 3.94 inch = ~406 dpi.

    Image 2 has a pixel dimension of 400 x 212 and also needs to be printed in said 3.94 inch x 2.09 inch.

    So we have 400 pixels for 3.94 inch => 400 pixels / 3.94 inch = ~102 dpi.

    Image 3 is kind of a special case:

    TCPDF can resize images, in this case meaning a change of the pixel dimensions. This is useful if you have e.g. a 6000 x 8000 pixel photo that you'd like to print in the size of a stamp. Embedding that in its native pixel dimensions would blow up the size of the resulting PDF quite significantly, so this option ($resize) will change the pixel dimensions according to the desired height and width ($w and $h) while maintaining a given resolution ($dpi, default value 300).

    So with resizing enabled our 6000 x 8000 pixel image that needs to be printed in 1.5 x 2 cm (0.59" x 0.79") at 300 dpi would be resized to (0.59" x 300 px = 177 px) x (0.79" x 300 px = 237 px), which is just the right amount of pixels for the given size with the given resolution. If this image isn't resized by TCPDF, you'll have a resolution of 6000 / 0.59 or 8000 / 0.79 = 10169 dpi.

    Image 3 has resizing enabled and is set to 72 dpi, so TCPDF resizes it accordingly to 238 x 150 (3.94" x 72 dpi and 2.09" x 72 dpi).

     

Log in to post a comment.