Sharif
Sharif

Reputation: 728

PHP, generate PDF file from HTML

I'm a novice when it comes to generating pdf files

I've tried tcpdf and mpdf but not getting the format the way i want it be.

Here's my mpdf code

include('./mpdf.php');
    $mpdf=new mPDF();
    $html = '<!DOCTYPE html>
<html>
<head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="cssForTable.css">
</head>
<body>
<table>
    <thead>
    <tr>
        <th width="25%">Client Details</th>
        <th colspan="2" width="50%"><h2 style="padding-top: 0; margin-bottom: 0; margin-top: 0;text-align: center"><strong>ROAD TRAFFIC ACCIDENT</strong></h2></th>
        <th width="25%">Legal HQ</th>
    </tr>
    </thead>
    <tbody>
    <tr>
        <td width="50%" colspan="2">
            <strong>Instructed Another Solicito?</strong> &nbsp; &nbsp; &nbsp; &nbsp;
            Yes &nbsp; <input type="checkbox" /> &nbsp; &nbsp; No &nbsp; <input type="checkbox" />
        </td>
        <th width="50%" colspan="2" rowspan="3">
            Claiming For:<br />
            Personal Injury &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Yes &nbsp; <input type="checkbox" style="margin-bottom: 7px;" />
                            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;No &nbsp; <input type="checkbox" style="margin-bottom: 7px;" /><br />
            Vehicle Damage &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Yes &nbsp; <input type="checkbox" style="margin-bottom: 7px;" />
                             &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;No &nbsp; <input type="checkbox" style="margin-bottom: 7px;" /><br />
            Loss of Earnings &nbsp; &nbsp; &nbsp; &nbsp; Yes &nbsp; <input type="checkbox" />
                             &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;No &nbsp; <input type="checkbox" />
        </th>
    </tr>
    <tr><th colspan="2"><h3 style="padding-top: 0; margin-bottom: 0; margin-top: 0;">ACCIDENT DETAILS</h3></th></tr>
    <tr>
        <th width="25%">Date of Accident</th>
        <td width="25%">&nbsp;</td>
    </tr>
    <tr>
        <th width="25%">Time of Accident</th>
        <td width="25%">&nbsp;</td>
        <th width="25%">No of Occupants (inc driver)</th>
        <td width="25%">&nbsp;</td>
    </tr>
    <tr>
        <th width="25%">Location of Accident</th>
        <td width="25%">&nbsp;</td>
        <td width="50%" colspan="2" rowspan="2" valign="top" >
            <strong>Were Police Involved? &nbsp; &nbsp; &nbsp; &nbsp;
                Yes &nbsp; <input type="checkbox" /> &nbsp; &nbsp; 
                No &nbsp; <input type="checkbox" />
            <br />
            <br />If so, Police No/Station: </strong>
        </td>
    </tr>
    <tr>
        <td width="25%" colspan="2" rowspan="2" valign="top"><strong>Accident Circumstances</strong><br /><br /></td>
    </tr>
    <tr>
        <th width="25%" colspan="2">
            Is Client:<br />
            Owner <input type="checkbox" style="margin-left: 15em; margin-bottom: 7px;" /><br />
            Driver &nbsp;<input type="checkbox" style="margin-left: 15em; margin-bottom: 7px;" /><br />
            Passenger &nbsp;<input type="checkbox" style="margin-left: 13em;" />
        </th>
    </tr>
    <tr>
        <th width="25%">Weather Conditions</th>
        <th width="75%" colspan="3">        
            <label style="margin-left: 2em; margin-bottom: 7px;" > Sunny &nbsp; &nbsp; <input type="checkbox" /></label>
            <label style="margin-left: 8em; margin-bottom: 7px;" > Rain &nbsp; &nbsp; <input type="checkbox" /></label>
            <label style="margin-left: 8em; margin-bottom: 7px;" > Snow &nbsp; &nbsp; <input type="checkbox" /></label><br />
            <label style="margin-left: 2em; margin-bottom: 7px;" > Ice &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="checkbox" /></label>
            <label style="margin-left: 8em; margin-bottom: 7px;" > Fog &nbsp; &nbsp;  &nbsp;<input type="checkbox" /></label>
            <label style="margin-left: 8em; margin-bottom: 7px;" > Dark &nbsp; &nbsp;  &nbsp; <input type="checkbox" /></label>
        </th>
    </tr>
    </tbody>
</table>
</body>
</html>';


    $mpdf->WriteHTML($html);
    $mpdf->Output();
    exit;

The above code generates the following PDF file (screen shot)

enter image description here

Where as it should look like the following

enter image description here

My question is, is there a way to generate PDF as we generate HTML I mean is there a way to print whatever html+PHP has generated on client side?

Any Idea?

Upvotes: 2

Views: 2069

Answers (2)

Ashwani Garg
Ashwani Garg

Reputation: 1527

Yes, you can do this. Here is the complete code. It works perfectly. You can get value from another page using post method also. Your choice.

<?php $student_id = $_GET['student_id']; ?>
<?php
include("mpdf/mpdf.php");
$html .= "
<html>
<head>
<style>
body {font-family: sans-serif;
    font-size: 10pt;
    background-image: url(\"images/ok.jpg\");

    background-repeat: no-repeat;
    padding-top:10pt;
    margin-top: 100px;
    padding-top: 50px;
}
td { vertical-align: top; 
    border-left: 0.6mm solid #000000;
    border-right: 0.6mm solid #000000;
    align: center;
}

p.student_id{
    padding-left : 140px;
    padding-top  : -27px;
} 

</style>
</head>
<body>
<!--mpdf                                                                          

<p class=\"student_id\">$student_id</p>


<sethtmlpageheader name='myheader' value='on' show-this-page='1' />
<sethtmlpagefooter name='myfooter' value='on' />
mpdf-->


</body>
</html>
";

$mpdf=new mPDF();
$mpdf->WriteHTML($html);
$mpdf->SetDisplayMode('fullpage');

$mpdf->Output();
?>

Upvotes: 0

Peter Es
Peter Es

Reputation: 51

HTML2PDF converters may be overstrained with that layout. You may should consider PDF-Generators with more powerful layout-options. The downside is, that you have to define your layout in another layout-language, but this will guarantee a satisfying output. Have you considered to use XSL/FO with a suitable generator like PDFnow?

Upvotes: 2

Related Questions