Skip to content
Advertisement

Getting spacing issues in TCPDF with writeHTML

I am creating a PDF with HTML contents using the writeHTML function of TCPDF. Below is the relevant code:

<?php
  include('PDF/tcpdf.php');
  $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

  $pdf->SetCreator(PDF_CREATOR);
  $pdf->SetAuthor('test');
  $pdf->SetTitle("test");
  $pdf->setPrintHeader(false);
  $pdf->setPrintFooter(false);
  $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  $pdf->SetMargins(20, PDF_MARGIN_TOP, 20);
  $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
  $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
  $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
  $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  $pdf->SetFont('helvetica', '', 12, '', true);
  $pdf->AddPage(); 
  $html = '<div>Die Wohnung des Hauptmieters befindet sich in der 
      <span style="font-weight: bold;">This text is supposed to space properly but is overlapping.</span>
      in <span style="font-weight: bold;">I don't know why, can anyone help me here?</span>
      in der <span style="font-weight: bold;">It would be greatly appreciated.</span>
      Etage. Es wird ein Raum zu Wohnzwecken und zur ausschließlichen Nutzung an 
      den Untermieter vermietet. Der Vermieter hat der Untervermietung schriftlich zugestimmt.      
    </div>';
  $pdf->writeHTML($html, true, false, true, false, 'L');
  $pdf->Output(__DIR__ . '/test.pdf', 'I');    
?>

With this code, I am getting some weird overlapping of the text marked to be bold and the rest of the text, as shown below:

enter image description here

Any help to resolve this would be appreciated.

Note that: The HTML here is being received from a front end as via an XMLHTTPRequest, and must be presented as it is in the PDF document.

Advertisement

Answer

I was unable to find the root cause for this issue. But I ended up using the mPDF library and was able to get the text to render correctly.

I hope this info helps someone in the future. Of course if anyone can point out the reason for the issue it would be great.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement