William Entriken
William Entriken

Reputation: 39283

TCPDF is indenting the first line of output

Here is my code:

<?php
require '../../vendor/tecnickcom/tcpdf/tcpdf.php';

// create new PDF document
$pdf = new TCPDF('L', 'in', [6,4]);
#$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
#$pdf->setFontSubsetting(true);
$pdf->SetPrintHeader(false);
$pdf->SetPrintFooter(false);
$pdf->SetFont('helvetica', '', 14);

$pdf->AddPage();
$pdf->SetXY(0.5, 0.2);
$pdf->Write(0, "Hello\nworld");
$pdf->Output('Postcards-'.date('Y-m-d').'.pdf', 'I');

And here is the output (ignore border)

enter image description here

If I enter several lines, only the first is indented. Why? and how do I stop this?

Upvotes: 2

Views: 1033

Answers (1)

Jakuje
Jakuje

Reputation: 25966

First of all, I would consider using MultiCell() function to write data to fixed place as you probably need. It handles quite much all these problems that the low-level function Write() does not. The below line works like a charm:

$pdf->MultiCell(0, 0, "Hello\nworld");

If you really need to use Write(), the actual problem the line above

$pdf->SetXY(0.5, 0.2);

Commenting that out makes it working again. To understand what is going on there, the new line returns the cursor back to the start of the line **defined by the page regions and margins, not below the previous location of the cursor.

You have much better control of this when you work with the Cells(). You can notice it can accept $ln argument of values 0 to 2, where the value 2 positions the cursor below the box regardless the regions/margins.

Upvotes: 2

Related Questions