Html to pdf Conversion is a very useful functionality in web development. But Most of the cases Developers are Confused about how to convert HTML to PDF by using which library. In the market, you will find verities of the library are available by which we can convert your HTML to pdf file. Some Of them are like Dompdf, tcpdf, mpdf, etc. So You can use anyone to get HTML to pdf. Here we will work by using Dompdf. If you want to use tcpdf then click here.
Follow the below 2 steps, you can do HTML to pdf using PHP within a minute.
Step-1: Download the Dompdf library click the here
Step-2: Extract it in your root folder
Step-3: Just include the file name in your PHP file and initialize the Dompdf by seeing the below example.
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | <?php require_once './dompdf/autoload.inc.php'; // reference the Dompdf namespace use Dompdf\Dompdf; //initialize dompdf class $document = new Dompdf(); $output ='<div class="main_div"> <div class="box box-widget"> <div class="box-header with-border" style="font-size:14px;"> <h1><strong>Convert HTML To PDF Using PHP</h1></div> <div class="box-footer" style="display:block;"> <h3>What is Lorem Ipsum?</h3> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrystandard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum </div> </div></div>'; ; //echo $output; $document->loadHtml($output); //set page size and orientation $document->setPaper('A4', 'portrait'); //Render the HTML as PDF echo $document->render(); //Get output of generated pdf in Browser $document->stream("Example.pdf", array("Attachment"=>0)); //1 = Download //0 = Preview ?> |