Search code examples
phphtmlpdfhtml2pdf

Converting Dynamic HTML TO PDF using html2pdf


The pages I want to convert are generated dynamically via PHP from a MySQL database. In other words the information on a page depends on the Id of the chosen object. So I would normally have a URL like this

renal_prescRequest_review.php?id=4

I am using html2pdf and so far I am unable to perform the conversion. When I pass id=2 directly like this

// get the HTML
ob_start();
include("renal_clinicalTrial_review.php?id=2");
$content = ob_get_clean();`

I get these errors:

Warning: include(renal_prescRequest_review.php?id=2) [function.include ]: failed to open stream: No such file or directory in/home/renalmed/public_html/testing/test/renal_prescRequest_pdf.php on line5

Warning: include(renal_prescRequest_review.php?id=2) [function.include]: failed to open stream: No such file or directory in /home/renalmed/public_html/testing/test/renal_prescRequest_pdf.php on line5

Warning: include() [function.include]: Failed opening 'renal_prescRequest_review.php?id=2' for inclusion(include_path='.:/usr/lib/php:/usr/local/lib/php') in/home/renalme/public_html/testing/test/renal_prescRequest_pdf.php on line5.

The fascinating thing is that these errors are thrown on the PDF document where the actual HTML is supposed to be displayed. I have been on this for quite a while now. Please help.


Solution

  • You can't include a query string on the include file.

    One solution on this is to transfer the codes of renal_prescRequest_review.php in the file where you want to convert html to pdf.

    Below is the sample code.

    <?php
    /**
     * HTML2PDF Librairy - example
     *
     * HTML => PDF convertor
     * distributed under the LGPL License
     *
     * @author      Laurent MINGUET <webmaster@html2pdf.fr>
     *
     * isset($_GET['vuehtml']) is not mandatory
     * it allow to display the result in the HTML format
     */
    // get the HTML
    ob_start();
    // database connection here
    require_once ( 'database-connection.php' );
    // get the id
    $id = $_GET['id'];
    // Retrieve record from database
    // query code here
    ?>
    <page backleft="0mm" backtop="0mm" backright="0mm" backbottom="0mm">    
        ID: <?php echo $id;?>
        <!--other html and php codes here.-->
    </page>
    <?php
    $content = ob_get_clean();
    // convert to PDF
    require_once('../html2pdf/html2pdf.class.php');
    try
    {
        $html2pdf = new HTML2PDF('P', array(200,300), 'en', true, 'UTF-8', array(0, 0, 0, 0));
        //$html2pdf = new HTML2PDF('P', 'A4', 'fr', true, 'UTF-8', 0);
        $html2pdf->pdf->SetDisplayMode('fullpage');
        $html2pdf->writeHTML($content);
        //$html2pdf->Output('test.pdf','D'); // force download pdf
        $html2pdf->Output('test.pdf'); // display only
    }
    catch(HTML2PDF_exception $e) {
        echo $e;
        exit;
    }