Search code examples
phphtml2pdfxhtml2pdf

HTML2PDF < sign is not working in php


I have exported data in PDF it is working fine for me, but for one string the text is "test<5", when i export this text in PDF it is just showing me "test" in PDF after < sign all the text getting blank, can anyone please tell me how can i resolve this issues ? I am using HTML2PDF for export data in PDF

$html2pdf = new HTML2PDF('P', 'A4', 'fr',true, 'UTF-8');
$html2pdf->setDefaultFont('Arial');
$html2pdf->writeHTML('test<5');
$html2pdf->Output('e-Voucher.pdf');

Solution

  • You're writing to HTML, hence the writeHTML function. In this case you want to encode the character so it can be interpreted as HTML.

    One way of doing this is:

    $html2pdf = new HTML2PDF('P', 'A4', 'fr',true, 'UTF-8');
    $html2pdf->setDefaultFont('Arial');
    $html2pdf->writeHTML("test&#x3c;5");
    $html2pdf->Output('e-Voucher.pdf');
    

    Or as Aleksandar said:

    $html2pdf = new HTML2PDF('P', 'A4', 'fr',true, 'UTF-8');
    $html2pdf->setDefaultFont('Arial');
    $html2pdf->writeHTML("test&lt;5");
    $html2pdf->Output('e-Voucher.pdf');
    

    My output to both:

    test<5 example