title: PHP, generate PDF file from HTML
body: <p>I'm a novice when it comes to generating pdf files</p>
  
  <p>I've tried tcpdf and mpdf but not getting the format the way i want it be.</p>
  
  <p>Here's my mpdf code</p>
  
  <pre><code>include('./mpdf.php');
      $mpdf=new mPDF();
      $html = '&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;&lt;/title&gt;
      &lt;link rel="stylesheet" type="text/css" href="cssForTable.css"&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;table&gt;
      &lt;thead&gt;
      &lt;tr&gt;
          &lt;th width="25%"&gt;Client Details&lt;/th&gt;
          &lt;th colspan="2" width="50%"&gt;&lt;h2 style="padding-top: 0; margin-bottom: 0; margin-top: 0;text-align: center"&gt;&lt;strong&gt;ROAD TRAFFIC ACCIDENT&lt;/strong&gt;&lt;/h2&gt;&lt;/th&gt;
          &lt;th width="25%"&gt;Legal HQ&lt;/th&gt;
      &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
      &lt;tr&gt;
          &lt;td width="50%" colspan="2"&gt;
              &lt;strong&gt;Instructed Another Solicito?&lt;/strong&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;
              Yes &amp;nbsp; &lt;input type="checkbox" /&gt; &amp;nbsp; &amp;nbsp; No &amp;nbsp; &lt;input type="checkbox" /&gt;
          &lt;/td&gt;
          &lt;th width="50%" colspan="2" rowspan="3"&gt;
              Claiming For:&lt;br /&gt;
              Personal Injury &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Yes &amp;nbsp; &lt;input type="checkbox" style="margin-bottom: 7px;" /&gt;
                              &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;No &amp;nbsp; &lt;input type="checkbox" style="margin-bottom: 7px;" /&gt;&lt;br /&gt;
              Vehicle Damage &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Yes &amp;nbsp; &lt;input type="checkbox" style="margin-bottom: 7px;" /&gt;
                               &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;No &amp;nbsp; &lt;input type="checkbox" style="margin-bottom: 7px;" /&gt;&lt;br /&gt;
              Loss of Earnings &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Yes &amp;nbsp; &lt;input type="checkbox" /&gt;
                               &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;No &amp;nbsp; &lt;input type="checkbox" /&gt;
          &lt;/th&gt;
      &lt;/tr&gt;
      &lt;tr&gt;&lt;th colspan="2"&gt;&lt;h3 style="padding-top: 0; margin-bottom: 0; margin-top: 0;"&gt;ACCIDENT DETAILS&lt;/h3&gt;&lt;/th&gt;&lt;/tr&gt;
      &lt;tr&gt;
          &lt;th width="25%"&gt;Date of Accident&lt;/th&gt;
          &lt;td width="25%"&gt;&amp;nbsp;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;th width="25%"&gt;Time of Accident&lt;/th&gt;
          &lt;td width="25%"&gt;&amp;nbsp;&lt;/td&gt;
          &lt;th width="25%"&gt;No of Occupants (inc driver)&lt;/th&gt;
          &lt;td width="25%"&gt;&amp;nbsp;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;th width="25%"&gt;Location of Accident&lt;/th&gt;
          &lt;td width="25%"&gt;&amp;nbsp;&lt;/td&gt;
          &lt;td width="50%" colspan="2" rowspan="2" valign="top" &gt;
              &lt;strong&gt;Were Police Involved? &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;
                  Yes &amp;nbsp; &lt;input type="checkbox" /&gt; &amp;nbsp; &amp;nbsp; 
                  No &amp;nbsp; &lt;input type="checkbox" /&gt;
              &lt;br /&gt;
              &lt;br /&gt;If so, Police No/Station: &lt;/strong&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td width="25%" colspan="2" rowspan="2" valign="top"&gt;&lt;strong&gt;Accident Circumstances&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;th width="25%" colspan="2"&gt;
              Is Client:&lt;br /&gt;
              Owner &lt;input type="checkbox" style="margin-left: 15em; margin-bottom: 7px;" /&gt;&lt;br /&gt;
              Driver &amp;nbsp;&lt;input type="checkbox" style="margin-left: 15em; margin-bottom: 7px;" /&gt;&lt;br /&gt;
              Passenger &amp;nbsp;&lt;input type="checkbox" style="margin-left: 13em;" /&gt;
          &lt;/th&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;th width="25%"&gt;Weather Conditions&lt;/th&gt;
          &lt;th width="75%" colspan="3"&gt;        
              &lt;label style="margin-left: 2em; margin-bottom: 7px;" &gt; Sunny &amp;nbsp; &amp;nbsp; &lt;input type="checkbox" /&gt;&lt;/label&gt;
              &lt;label style="margin-left: 8em; margin-bottom: 7px;" &gt; Rain &amp;nbsp; &amp;nbsp; &lt;input type="checkbox" /&gt;&lt;/label&gt;
              &lt;label style="margin-left: 8em; margin-bottom: 7px;" &gt; Snow &amp;nbsp; &amp;nbsp; &lt;input type="checkbox" /&gt;&lt;/label&gt;&lt;br /&gt;
              &lt;label style="margin-left: 2em; margin-bottom: 7px;" &gt; Ice &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;input type="checkbox" /&gt;&lt;/label&gt;
              &lt;label style="margin-left: 8em; margin-bottom: 7px;" &gt; Fog &amp;nbsp; &amp;nbsp;  &amp;nbsp;&lt;input type="checkbox" /&gt;&lt;/label&gt;
              &lt;label style="margin-left: 8em; margin-bottom: 7px;" &gt; Dark &amp;nbsp; &amp;nbsp;  &amp;nbsp; &lt;input type="checkbox" /&gt;&lt;/label&gt;
          &lt;/th&gt;
      &lt;/tr&gt;
      &lt;/tbody&gt;
  &lt;/table&gt;
  &lt;/body&gt;
  &lt;/html&gt;';
  
  
      $mpdf-&gt;WriteHTML($html);
      $mpdf-&gt;Output();
      exit;
  </code></pre>
  
  <p>The above code generates the following PDF file (screen shot)</p>
  
  <p><img src="https://i.stack.imgur.com/2TT13.jpg" alt="enter image description here"></p>
  
  <p>Where as it should look like the following</p>
  
  <p><img src="https://i.stack.imgur.com/QehWx.jpg" alt="enter image description here"></p>
  
  <p>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?</p>
  
  <p>Any Idea?</p>