0

At first i thought TCPDF's GetStringWidth() was not working right, giving me the wrong width:

[----------------------------------------------------------------------------------------------------------------------]

I have some text like $txt = "hallo".

I've tried to get the strings width via the built in function GetStringWidth() and then create a cell with the strings width.

// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

$pdf->setFontSubsetting(true);

$pdf->AddPage();

$pdf->SetFont('times', '', 20);
$pdf->SetCellPadding(0);

$txt = "hey i'm santa clause";
$width = $pdf->GetStringWidth($txt);

$pdf->Cell($width, $h=0, $txt, $border=1, $ln=0, $align='', $fill=false, $link='', $stretch=0, $ignore_min_height=false, $calign='T', $valign='M');

$pdf->Output('example_001.pdf', 'I');
?>

The created cell however is to short for it's content. If I try the same with courier it works fine.

I think it's a measure of GetStringWidth not processing the font's width right, cause of Courier having the same width for each character, which works.

How can I get GetStringWidth() working

[----------------------------------------------------------------------------------------------------------------------]

Today I tried opening it with Firefox/Evince instead of chrome

Here's the result:enter image description here Firefox is on the left, chrome on the right.

The generated PDF file is v. 1.7

Used version of chrome:

google-chrome-beta (41.0.2272.53-1)

What could have gone wrong here? Is it really chromes fault? Or am I missing somethign here?

Thanks

lance
  • 65
  • 10
  • edited code snippet. it's now the whole document. after adding $pdf->SetCellPadding(0) and setting $pdf->setFont() before $pdf->GetStringWidth() still no success. – lance Feb 04 '15 at 12:03
  • created a ticket in the TCPDF bug forums. it was closed twice without any real suggestions. the author said the method is working fine and I was told to look for help in the regular TCPDF forum. did posted my issue there, too. no help so far – lance Feb 10 '15 at 10:15

1 Answers1

0

You must try with replacing:

$pdf->Output("{$filename}.pdf", 'I');

with

$pdf->Output("{$filename}.pdf", 'D');