save a html webpage as pdf using php -


we know can save webpage pdf pressing ctrl+p , save pdf. how can done directly using php? please note webpage pre-designed , need save pdf

you can use tcpdf, @ example gives this result;

<?php //============================================================+ // file name   : example_006.php // begin       : 2008-03-04 // last update : 2013-05-14 // // description : example 006 tcpdf class //               writehtml , rtl support // // author: nicola asuni // // (c) copyright: //               nicola asuni //               tecnick.com ltd //               www.tecnick.com //               info@tecnick.com //============================================================+  /**  * creates example pdf test document using tcpdf  * @package com.tecnick.tcpdf  * @abstract tcpdf - example: writehtml , rtl support  * @author nicola asuni  * @since 2008-03-04  */  // include main tcpdf library (search installation path). require_once('tcpdf_include.php');  // create new pdf document $pdf = new tcpdf(pdf_page_orientation, pdf_unit, pdf_page_format, true, 'utf-8', false);  // set document information $pdf->setcreator(pdf_creator); $pdf->setauthor('nicola asuni'); $pdf->settitle('tcpdf example 006'); $pdf->setsubject('tcpdf tutorial'); $pdf->setkeywords('tcpdf, pdf, example, test, guide');  // set default header data $pdf->setheaderdata(pdf_header_logo, pdf_header_logo_width, pdf_header_title.' 006', pdf_header_string);  // set header , footer fonts $pdf->setheaderfont(array(pdf_font_name_main, '', pdf_font_size_main)); $pdf->setfooterfont(array(pdf_font_name_data, '', pdf_font_size_data));  // set default monospaced font $pdf->setdefaultmonospacedfont(pdf_font_monospaced);  // set margins $pdf->setmargins(pdf_margin_left, pdf_margin_top, pdf_margin_right); $pdf->setheadermargin(pdf_margin_header); $pdf->setfootermargin(pdf_margin_footer);  // set auto page breaks $pdf->setautopagebreak(true, pdf_margin_bottom);  // set image scale factor $pdf->setimagescale(pdf_image_scale_ratio);  // set language-dependent strings (optional) if (@file_exists(dirname(__file__).'/lang/eng.php')) {     require_once(dirname(__file__).'/lang/eng.php');     $pdf->setlanguagearray($l); }  // ---------------------------------------------------------  // set font $pdf->setfont('dejavusans', '', 10);  // add page $pdf->addpage();  // writehtml($html, $ln=true, $fill=false, $reseth=false, $cell=false, $align='') // writehtmlcell($w, $h, $x, $y, $html='', $border=0, $ln=0, $fill=0, $reseth=true, $align='', $autopadding=true)  // create html content $html = '<h1>html example</h1> special characters: &lt; € &euro; &#8364; &amp; è &egrave; &copy; &gt; \\slash \\\\double-slash \\\\\\triple-slash <h2>list</h2> list example: <ol>     <li><img src="images/logo_example.png" alt="test alt attribute" width="30" height="30" border="0" /> test image</li>     <li><b>bold text</b></li>     <li><i>italic text</i></li>     <li><u>underlined text</u></li>     <li><b>b<i>bi<u>biu</u>bi</i>b</b></li>     <li><a href="http://www.tecnick.com" dir="ltr">link http://www.tecnick.com</a></li>     <li>sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.<br />nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt.</li>     <li>sublist         <ol>             <li>row 1                 <ul>                     <li>sublist</li>                 </ul>             </li>             <li>row two</li>         </ol>     </li>     <li><b>t</b>e<i>s</i><u>t</u> <del>line through</del></li>     <li><font size="+3">font + 3</font></li>     <li><small>small text</small> normal <small>small text</small> normal <sub>subscript</sub> normal <sup>superscript</sup> normal</li> </ol> <dl>     <dt>coffee</dt>     <dd>black hot drink</dd>     <dt>milk</dt>     <dd>white cold drink</dd> </dl> <div style="text-align:center">images<br /> <img src="images/logo_example.png" alt="test alt attribute" width="100" height="100" border="0" /><img src="images/tcpdf_box.svg" alt="test alt attribute" width="100" height="100" border="0" /><img src="images/logo_example.jpg" alt="test alt attribute" width="100" height="100" border="0" /> </div>';  // output html content $pdf->writehtml($html, true, false, true, false, '');   // output rtl html content $html = '<div style="text-align:center">the words &#8220;<span dir="rtl">&#1502;&#1494;&#1500; [mazel] &#1496;&#1493;&#1489; [tov]</span>&#8221; mean &#8220;congratulations!&#8221;</div>'; $pdf->writehtml($html, true, false, true, false, '');  // test inline css $html = '<p>this example of html code demonstrate supported css inline styles. <span style="font-weight: bold;">bold text</span> <span style="text-decoration: line-through;">line-trough</span> <span style="text-decoration: underline line-through;">underline , line-trough</span> <span style="color: rgb(0, 128, 64);">color</span> <span style="background-color: rgb(255, 0, 0); color: rgb(255, 255, 255);">background color</span> <span style="font-weight: bold;">bold</span> <span style="font-size: xx-small;">xx-small</span> <span style="font-size: x-small;">x-small</span> <span style="font-size: small;">small</span> <span style="font-size: medium;">medium</span> <span style="font-size: large;">large</span> <span style="font-size: x-large;">x-large</span> <span style="font-size: xx-large;">xx-large</span> </p>';  $pdf->writehtml($html, true, false, true, false, '');  // reset pointer last page $pdf->lastpage();  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // print table  // add page $pdf->addpage();  // create html content $subtable = '<table border="1" cellspacing="6" cellpadding="4"><tr><td>a</td><td>b</td></tr><tr><td>c</td><td>d</td></tr></table>';  $html = '<h2>html table:</h2> <table border="1" cellspacing="3" cellpadding="4">     <tr>         <th>#</th>         <th align="right">right align</th>         <th align="left">left align</th>         <th>4a</th>     </tr>     <tr>         <td>1</td>         <td bgcolor="#cccccc" align="center" colspan="2">a1 ex<i>amp</i>le <a href="http://www.tcpdf.org">link</a> column span. 1 2 tree 4 5 6 7 8 9 ten.<br />line after br<br /><small>small text</small> normal <sub>subscript</sub> normal <sup>superscript</sup> normal  bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla<ol><li>first<ol><li>sublist</li><li>sublist</li></ol></li><li>second</li></ol><small color="#ff0000" bgcolor="#ffff00">small small small small small small small small small small small small small small small small small small small small</small></td>         <td>4b</td>     </tr>     <tr>         <td>'.$subtable.'</td>         <td bgcolor="#0000ff" color="yellow" align="center">a2 € &euro; &#8364; &amp; è &egrave;<br/>a2 € &euro; &#8364; &amp; è &egrave;</td>         <td bgcolor="#ffff00" align="left"><font color="#ff0000">red</font> yellow bg</td>         <td>4c</td>     </tr>     <tr>         <td>1a</td>         <td rowspan="2" colspan="2" bgcolor="#ffffcc">2aa<br />2ab<br />2ac</td>         <td bgcolor="#ff0000">4d</td>     </tr>     <tr>         <td>1b</td>         <td>4e</td>     </tr>     <tr>         <td>1c</td>         <td>2c</td>         <td>3c</td>         <td>4f</td>     </tr> </table>';  // output html content $pdf->writehtml($html, true, false, true, false, '');  // print html cells  $html = '<span color="red">red</span> <span color="green">green</span> <span color="blue">blue</span><br /><span color="red">red</span> <span color="green">green</span> <span color="blue">blue</span>';  $pdf->setfillcolor(255,255,0);  $pdf->writehtmlcell(0, 0, '', '', $html, 'lrtb', 1, 0, true, 'l', true); $pdf->writehtmlcell(0, 0, '', '', $html, 'lrtb', 1, 1, true, 'c', true); $pdf->writehtmlcell(0, 0, '', '', $html, 'lrtb', 1, 0, true, 'r', true);  // reset pointer last page $pdf->lastpage();  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // print table  // add page $pdf->addpage();  // create html content $html = '<h1>image alignments on html table</h1> <table cellpadding="1" cellspacing="1" border="1" style="text-align:center;"> <tr><td><img src="images/logo_example.png" border="0" height="41" width="41" /></td></tr> <tr style="text-align:left;"><td><img src="images/logo_example.png" border="0" height="41" width="41" align="top" /></td></tr> <tr style="text-align:center;"><td><img src="images/logo_example.png" border="0" height="41" width="41" align="middle" /></td></tr> <tr style="text-align:right;"><td><img src="images/logo_example.png" border="0" height="41" width="41" align="bottom" /></td></tr> <tr><td style="text-align:left;"><img src="images/logo_example.png" border="0" height="41" width="41" align="top" /></td></tr> <tr><td style="text-align:center;"><img src="images/logo_example.png" border="0" height="41" width="41" align="middle" /></td></tr> <tr><td style="text-align:right;"><img src="images/logo_example.png" border="0" height="41" width="41" align="bottom" /></td></tr> </table>';  // output html content $pdf->writehtml($html, true, false, true, false, '');  // reset pointer last page $pdf->lastpage();  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // print html colors  // add page $pdf->addpage();  $textcolors = '<h1>html text colors</h1>'; $bgcolors = '<hr /><h1>html background colors</h1>';  foreach(tcpdf_colors::$webcolor $k => $v) {     $textcolors .= '<span color="#'.$v.'">'.$v.'</span> ';     $bgcolors .= '<span bgcolor="#'.$v.'" color="#333333">'.$v.'</span> '; }  // output html content $pdf->writehtml($textcolors, true, false, true, false, ''); $pdf->writehtml($bgcolors, true, false, true, false, '');  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  // test word-wrap  // create html content $html = '<hr /> <h1>various tests</h1> <a href="#2">link page 2</a><br /> <font face="courier"><b>thisisaverylongword</b></font> <font face="helvetica"><i>thisisanotherverylongword</i></font> <font face="times"><b>thisisaverylongword</b></font> thisisanotherverylongword <font face="times">thisisaverylongword</font> <font face="courier"><b>thisisaverylongword</b></font> <font face="helvetica"><i>thisisanotherverylongword</i></font> <font face="times"><b>thisisaverylongword</b></font> thisisanotherverylongword <font face="times">thisisaverylongword</font> <font face="courier"><b>thisisaverylongword</b></font> <font face="helvetica"><i>thisisanotherverylongword</i></font> <font face="times"><b>thisisaverylongword</b></font> thisisanotherverylongword <font face="times">thisisaverylongword</font> <font face="courier"><b>thisisaverylongword</b></font> <font face="helvetica"><i>thisisanotherverylongword</i></font> <font face="times"><b>thisisaverylongword</b></font> thisisanotherverylongword <font face="times">thisisaverylongword</font> <font face="courier"><b>thisisaverylongword</b></font> <font face="helvetica"><i>thisisanotherverylongword</i></font> <font face="times"><b>thisisaverylongword</b></font> thisisanotherverylongword <font face="times">thisisaverylongword</font>';  // output html content $pdf->writehtml($html, true, false, true, false, '');  // test fonts nesting $html1 = 'default <font face="courier">courier <font face="helvetica">helvetica <font face="times">times <font face="dejavusans">dejavusans </font>times </font>helvetica </font>courier </font>default'; $html2 = '<small>small text</small> normal <small>small text</small> normal <sub>subscript</sub> normal <sup>superscript</sup> normal'; $html3 = '<font size="10" color="#ff7f50">the</font> <font size="10" color="#6495ed">quick</font> <font size="14" color="#dc143c">brown</font> <font size="18" color="#008000">fox</font> <font size="22"><a href="http://www.tcpdf.org">jumps</a></font> <font size="22" color="#a0522d">over</font> <font size="18" color="#da70d6">the</font> <font size="14" color="#9400d3">lazy</font> <font size="10" color="#4169el">dog</font>.';  $html = $html1.'<br />'.$html2.'<br />'.$html3.'<br />'.$html3.'<br />'.$html2;  // output html content $pdf->writehtml($html, true, false, true, false, '');  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // test pre tag  // add page $pdf->addpage();  $html = <<<eof <div style="background-color:#880000;color:white;"> hello world!<br /> hello </div> <pre style="background-color:#336699;color:white;"> int main() {     printf("helloworld");     return 0; } </pre> <tt>monospace font</tt>, normal font, <tt>monospace font</tt>, normal font. <br /> <div style="background-color:#880000;color:white;">div level 1<div style="background-color:#008800;color:white;">div level 2</div>div level 1</div> <br /> <span style="background-color:#880000;color:white;">span level 1 <span style="background-color:#008800;color:white;">span level 2</span> span level 1</span> eof;  // output html content $pdf->writehtml($html, true, false, true, false, '');  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  // test custom bullet points list  // add page $pdf->addpage();  $html = <<<eof <h1>test custom bullet image list items</h1> <ul style="font-size:14pt;list-style-type:img|png|4|4|images/logo_example.png">     <li>test custom bullet image</li>     <li>test custom bullet image</li>     <li>test custom bullet image</li>     <li>test custom bullet image</li> <ul> eof;  // output html content $pdf->writehtml($html, true, false, true, false, '');  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  // reset pointer last page $pdf->lastpage();  // ---------------------------------------------------------  //close , output pdf document $pdf->output('example_006.pdf', 'i');  //============================================================+ // end of file //============================================================+ 

a more specific , simplified example including retrieving html file.

not tested, use concept;

<?php  // include main tcpdf library (search installation path). require_once('tcpdf_include.php');  // create new pdf document $pdf = new tcpdf(pdf_page_orientation, pdf_unit, pdf_page_format, true, 'utf-8', false);  // set font $pdf->setfont('dejavusans', '', 10);  // add page $pdf->addpage();  // create html content $html = file_get_contents('path/to/file/with.html');  // output html content $pdf->writehtml($html, true, false, true, false, '');  // reset pointer last page $pdf->lastpage();  //close , output pdf document $pdf->output('example_006.pdf', 'i'); 

Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -