Skip to content Skip to sidebar Skip to footer
Reading Time: 2 minutes

How do I convert html file into pdf file using php.In the past, I found everywhere to convert html file into pdf file using php.Finally I found easy method to convert html page into pdf.In this article is describing how to convert html content or page in to pdf without worrying.

DOMPDF can be use to convert html file into pdf easily.
Here is a dompdf home page link.You can download dompdf library file from there.
First of all you must download dompdf library.Then follow these steps.

Step-1

This is my index.php file.

Step-2

Then I adding links to pdf library files into this page.

require_once("dompdf_config.inc.php");

Step-3

Creating pdf generating process.

if ( isset( $_POST["html"] ) ) {
  if ( get_magic_quotes_gpc() )
    $_POST["html"] = stripslashes($_POST["html"]);  // remove unwanted characters
  $dompdf = new DOMPDF(); // creating dompdf object
  $dompdf->load_html($_POST["html"]); // load html data from above form
  $dompdf->set_paper($_POST["paper"], $_POST["orientation"]); // set print page type
  $dompdf->render(); // generate pdf file
  $dompdf->stream("mypage.pdf", array("Attachment" => false)); // save pdf file.I named it "mypage.pdf".
  exit(0);
}

Put this all together.

load_html($_POST["html"]);// load html data from above form
  $dompdf->set_paper($_POST["paper"], $_POST["orientation"]); // set print page type
  $dompdf->render(); // generate pdf file
  $dompdf->stream("mypage.pdf", array("Attachment" => false)); // save pdf file.I named it "mypage.pdf".
  exit(0);
}
?>

How to convert specific content into PDF

Now I’m going to explain how to do this and I’m not going to describes everything because essential part is explained above.
Here is my php page.

'.
			  '

Lorem ipsum dolor sit amet, consectetuer adipiscing elit.

'. '

Aenean commodo ligula eget dolor. Aenean massa.

'. '

Cum sociis natoque penatibus et magnis dis parturient.

'. ''; $dompdf = new DOMPDF(); // creating dompdf object $dompdf->load_html($html); // load html data $dompdf->render(); // generate pdf file $dompdf->stream("mypage.pdf");// save pdf file. ?>

When your are working in the live server there may ba a error generating pdf file.So then follow these steps.

  • Check you are correctly give the link path to the library config file.
  • Check you are correctly wrote scripts like above.
  • Add the following scripts in your page top.
ob_start();
ob_get_contents();
ini_set("memory_limit", "16M");
// ADD THIS SCRIPT BETWEEN THE $dompdf->render(); CODE AND THE $dompdf->stream("mypage.pdf"); CODE.
$dompdf->output();

Convert textarea contents with formatting

textarea to pdf
If you try to do the textarea contents convert with it’s formatting may be that will not work with above solutions.
That means textarea has line breaks, that will not showing in the PDF file. Here is the solution for that.

if ( isset( $_POST["html"] ) ) {
  if ( get_magic_quotes_gpc() ) {
    $_POST["html"] = stripslashes($_POST["html"]); // remove unwanted characters
  }
  $dompdf = new DOMPDF(); // creating dompdf object
  $dompdf->load_html(nl2br($_POST["html"])); // added nl2br to work this. :)
  $dompdf->set_paper($_POST["paper"], $_POST["orientation"]); // set print page type
  $dompdf->render(); // generate pdf file
  $dompdf->output(); // this is required to show the output
  $dompdf->stream("mypage.pdf", array("Attachment" => false)); // save pdf file.I named it "mypage.pdf".
  exit(0);
}

Hope this would be help.

You can convert table,page,contents, and inserting images, changing fonts,changing paper size….etc to visiting dompdf usages page.
Download Convert HTML into PDF using PHP Source(476 KB)