How to Convert HTML WebPage to PDF Using PHP


Convert a web page into PDF format, download and save into folder as below.

Include dompdf_config.inc.php file from the library.

Create a folder for saving the PDF file which is generated from the webpage.

<?php &lt;br ?-->  require_once "dompdf_config.inc.php"; 
  if(isset($_POST['download']))
  { 
   $dompdf = new DOMPDF();
   $dompdf-&gt;load_html(file_get_contents("yourpagetoconvert"));
   $dompdf-&gt;render();
   $dompdf-&gt;stream("invoice.pdf", array("Attachment" =&gt; false));
   $output = $dompdf-&gt;output();
   $file_to_save = 'uploads/filename.pdf';
   file_put_contents($file_to_save, $output);

   //print the pdf file to the screen for saving
   header('Content-type: application/pdf');
   header('Content-Disposition: inline; filename="filename.pdf"');
   header('Content-Transfer-Encoding: binary');
   header('Content-Length: ' . filesize($file_to_save));
   header('Accept-Ranges: bytes');
   readfile($file_to_save);  
  }
 ?>

HTML code to convert on submit:


Download dompdf library from the github which is free. Click here to download.

Add a Comment

Your email address will not be published. Required fields are marked *