Search code examples
phppdfwkhtmltopdfhtml-to-pdf

PHP HTMLtoPDF is not working and returning only empty PDFs


I have downloaded PHP HTMLtoPDF converter from here. But when tried to just print single line output, it does not print in the output pdf. Its empty.

Below is the code which i tried.

$content1 = "<page><a>Sample PDF file</a></page>";

    // convert in PDF
    require_once('/home/www/APIs/html2pdf_v4.03/html2pdf.class.php');
    try
    {
        $html2pdf = new HTML2PDF('P', 'A4', 'fr');
        $html2pdf->setModeDebug();
        $html2pdf->setDefaultFont('Arial');
        $html2pdf->writeHTML($content1);
        $html2pdf->Output('exemple00.pdf');
    }
    catch(HTML2PDF_exception $e) {
        echo $e;
        exit;
    }

Solution

  • Whenever I get into trouble with something I simplify. Exterminate all the possibilities that could hinder the workings of a program.

    With this in mind I have looked at the website and, probably just like you, found a sample piece.

    $html2pdf = new HTML2PDF('P','A4','fr');
    $html2pdf->WriteHTML("text");
    $html2pdf->Output('exemple.pdf');
    

    Try this and see if it works. Why? This is the simplest of setups. If this does not work you know it's not you (or your refrence to the class is wrong).

    What I know from the pdf to html creators is that they all have a problem converting incorrect html to a pdf. In this case your "a" could be interpreted as wrong seeing it has no href (which could be required).

    So to make this simple use a single line of string text without html like "test" and check back with us. But if that doesn't work I's probably suggest using another of the converters (which I would recommend anyway).

    BTW, I use https://github.com/dompdf/dompdf which is very advanced in html error detection and common options.