CrimpJiggler Posted January 9, 2014 Share Posted January 9, 2014 I have this firefox plugin where you click a button and it saved the webpage as a PDF. As a result, I had the idea that creating PDFs is easy and you can just feed HTML and CSS right an application to convert it to PDF. I wanna add a save as PDF button to my site but its not looking so easy. I downloaded an application called tcpdf2 is a PHP PDF creator and theres a folder full of examples in there, but most of them are examples of PDFs programmed from scratch with PHP. I'm looking for a way to just feed HTML in to to it. Something along the lines of what these firefox plugins do. Heres one of the examples which contains raw HTML and CSS: <?php //============================================================+ // File name : example_061.php // Begin : 2010-05-24 // Last Update : 2013-05-14 // // Description : Example 061 for TCPDF class // XHTML + CSS // // Author: Nicola Asuni // // (c) Copyright: // Nicola Asuni // Tecnick.com LTD // www.tecnick.com // [email protected] //============================================================+ /** * Creates an example PDF TEST document using TCPDF * @package com.tecnick.tcpdf * @abstract TCPDF - Example: XHTML + CSS * @author Nicola Asuni * @since 2010-05-25 */ // Include the main TCPDF library (search for 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 061'); $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.' 061', PDF_HEADER_STRING); // set header and 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 some language-dependent strings (optional) // --------------------------------------------------------- // set font $pdf->SetFont('helvetica', '', 10); // add a page $pdf->AddPage(); /* NOTE: * ********************************************************* * You can load external XHTML using : * * $html = file_get_contents('/path/to/your/file.html'); * * External CSS files will be automatically loaded. * Sometimes you need to fix the path of the external CSS. * ********************************************************* */ // define some HTML content with style $html = <<<EOF <!-- EXAMPLE OF CSS STYLE --> <style> div.cat_wrapper { padding:1px; border:2px solid gray; margin:4px; border-radius: 5px; background-color:#FFFFFF; } div.qa_box_wrapper { padding:1px; border:2px solid gray; margin:4px; border-radius: 5px; background-color:#FFFFFF; } div.qa_box_wrapper img { max-width:95%; } div.qa_box_question_div,qa_box_answer_div { margin-left:4px; } div.answer_button { display:block; width:100%; } div.main_box { padding:1px; border:2px solid gray; margin:5px; border-radius: 5px; background-color:#FFFFFF; } </style> <div class="qa_box_answer_div"> This reaction is a Diels-Alder cycloaddition in which singlet oxygen acts as the dienophile. <br /> <br /></div> EOF; // output the HTML content $pdf->writeHTML($html, true, false, true, false, ''); // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // add a page $pdf->AddPage(); $html = ' <h1>HTML TIPS & TRICKS</h1> <h3>REMOVE CELL PADDING</h3> <pre>$pdf->SetCellPadding(0);</pre> This is used to remove any additional vertical space inside a single cell of text. <h3>REMOVE TAG TOP AND BOTTOM MARGINS</h3> <pre>$tagvs = array(\'p\' => array(0 => array(\'h\' => 0, \'n\' => 0), 1 => array(\'h\' => 0, \'n\' => 0))); $pdf->setHtmlVSpace($tagvs);</pre> Since the CSS margin command is not yet implemented on TCPDF, you need to set the spacing of block tags using the following method. <h3>SET LINE HEIGHT</h3> <pre>$pdf->setCellHeightRatio(1.25);</pre> You can use the following method to fine tune the line height (the number is a percentage relative to font height). <h3>CHANGE THE PIXEL CONVERSION RATIO</h3> <pre>$pdf->setImageScale(0.47);</pre> This is used to adjust the conversion ratio between pixels and document units. Increase the value to get smaller objects.<br /> Since you are using pixel unit, this method is important to set theright zoom factor.<br /><br /> Suppose that you want to print a web page larger 1024 pixels to fill all the available page width.<br /> An A4 page is larger 210mm equivalent to 8.268 inches, if you subtract 13mm (0.512") of margins for each side, the remaining space is 184mm (7.244 inches).<br /> The default resolution for a PDF document is 300 DPI (dots per inch), so you have 7.244 * 300 = 2173.2 dots (this is the maximum number of points you can print at 300 DPI for the given width).<br /> The conversion ratio is approximatively 1024 / 2173.2 = 0.47 px/dots<br /> If the web page is larger 1280 pixels, on the same A4 page the conversion ratio to use is 1280 / 2173.2 = 0.59 pixels/dots'; // output the HTML content $pdf->writeHTML($html, true, false, true, false, ''); // reset pointer to the last page $pdf->lastPage(); // --------------------------------------------------------- //Close and output PDF document $pdf->Output('example_061.pdf', 'I'); //============================================================+ // END OF FILE //============================================================+ but when I attempt to modify it, the script stops working and no PDF gets outputted. I've been playing around with these examples and its really delicate, one small modification like adding a HTML tag can stop it from working. Link to comment https://forums.phpfreaks.com/topic/285231-creating-pdfs-with-php/ Share on other sites More sharing options...
Ch0cu3r Posted January 9, 2014 Share Posted January 9, 2014 Use file_get_contents to get the HTML of your webpage. Then pass it to $pdf->writeHTML() $html = file_get_contents('http://site.com/page-to-convert.php'); $pdf->writeHTML($html, true, false, true, false, ''); Link to comment https://forums.phpfreaks.com/topic/285231-creating-pdfs-with-php/#findComment-1464568 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.