TCPDF: How to set FONT SIZE in right way

27,389

Solution 1

OK I found answer and solution. When we create new PDF document in tcPDF, dimensions units whole document can be in formats like mm, cm, pt, px. But fonts are in points - pt.

So solution...

  1. Set the documents units with 'setPageUnit'.
  2. If we have dimensions in pixels, we must convert it by 'pixelsToUnits'.

PHP - tcPDF Exampe :

$pdf->setPageUnit('pt');
$document_width = $pdf->pixelsToUnits('100');
$document_height = $pdf->pixelsToUnits('100');
$x = $pdf->pixelsToUnits('20');
$y = $pdf->pixelsToUnits('20');
$font_size = $pdf->pixelsToUnits('20');
$txt = 'AAAg';

$pdf->SetFont ('helvetica', '', $font_size , '', 'default', true );
$pdf->Text  ( $x, $y, $txt, false, false, true, 0, 0, '', false, '', 0, false, 'T', 'M', false );

Solution 2

Changing font size in TCPDF ... Which can set using below code :

$pdf = new TCPDF();
$pdf->SetFont('Font family', '', font size here);

Which are default setting in TCPDF

Share:
27,389
Patrik
Author by

Patrik

Updated on April 23, 2020

Comments

  • Patrik
    Patrik about 4 years

    I want to set some text-blocks by TCPDF. But I have some problems with font size. First text block is on x-y/5-5, and his font size is 5 to. But it is samaller then 5. Font-size in TCPDF is not in the same units like other dimensions ?

    TCPDF - dimensions and font-size

    PHP

    $text1 = 'AAAg';
    $text1_x = 5;
    $text1_y = 5;
    $text1_font_size = 5;
    
    $text2 = 'BBBg';
    $text2_x = 10;
    $text2_y = 10;
    $text2_font_size = 10;
    
    $text3 = 'CCCg';
    $text3_x = 15;
    $text3_y = 15;
    $text3_font_size = 15;
    // I tried  $pdf->Cell and $pdf->Text... both are doing the same...
    

    Web example.

  • Patrik
    Patrik about 9 years
    Maybe I was not clear, but... Please look again at my question... This is what I'm exactly doing, but there is problem. Becouse $x and $y is 5, and font-size is 5 to. But it font-size does not look like 5 .... ;-)
  • Admin
    Admin almost 8 years
    It WORKS! i had to use very tiny unit: mm, your sample worked perfectly.
  • Coisox
    Coisox over 2 years
    how to "get" PageUnit? I;ve tried getPageUnit(), GetPageUnit(), getBaseUnit(), GetBaseUnit()