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.
'. '