A Guillaume
A Guillaume

Reputation: 13

TCPDF html render not indented

I'm trying to display data with TCPDF from my DB which were specifically indented.

I use a writeHTMLCell to do so.

e.g. :

"This should be indented as so :
- First bullet
- Second one
Then the end of the example."

But of course, it render the text on a single line.

Does anyone know how i can render the text properly ?

Thank you very much !

Upvotes: 0

Views: 625

Answers (1)

Adam Moller
Adam Moller

Reputation: 973

With HTML preformatted text requires that you use <pre>. Here is an example:

This should be indented as so:
  - First bullet
  - Second one
Then the end of the example.

<pre>
This should be indented as so:
  - First bullet
  - Second one
Then the end of the example.
</pre>

Using <pre> tags with the preformatted text block should achieve the result you are looking for. Preformatted text defaults to a monospaced font and can be changed with $pdf->SetDefaultMonospacedFont(). Here is an example with and without <pre>:

<?php
require_once('tcpdf_include.php');

$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->AddPage();

$text_sample = 'This should be indented as so:
    - First bullet
    - Second one
Then the end of the example.';

$y = $pdf->getY();
$pdf->SetDefaultMonospacedFont('helvetica');
$pdf->SetFont('helvetica', '', 14, '', true);
$pdf->SetFillColor(255, 255, 255);
$pdf->SetTextColor(255, 92, 92);
$pdf->writeHTMLCell(190, '', '', $y, $text_sample, 1, 0, 1, true, 'J', true);
$pdf->SetTextColor(53,183,41);
$pdf->writeHTMLCell(190, '', '', $y+20, '<pre>'.$text_sample.'</pre>', 1, 1, 1, true, 'J', true);

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

The PDF will contain the following cells:

enter image description here

Upvotes: 1

Related Questions