Print a webpage to pdf document using php
Think wkhtmltopdf is the answer you are looking for as it uses WebKit to render the webpage just like it would look in a browser and then converts it to a PDF. This saves you writing a load of PHP code to organise the layout of your PDF.
Details from their site:
For code and more information on wkhtmltopdf integration with PHP see https://github.com/aur1mas/WkhtmltopdfSimple shell utility to convert html
to pdf using the webkit rendering
engine, and qt.Searching the web, I have found
several command line tools that allow
you to convert a HTML-document to a
PDF-document, however they all seem to
use their own, and rather incomplete
rendering engine, resulting in poor
quality. Recently QT 4.4 was released
with a WebKit widget (WebKit is the
engine of Apples Safari, which is a
fork of the KDE KHtml), and making a
good tool became very easy.
- Convert web pages into PDF documents using webkit
- Adding headers and footers (static version only)
- TOC generation (static version only)
- Batch mode conversions
- (Linux) No longer requires an XServer to be running (however the X11
client libs must be installed)
PHP+html webpage to pdf
you can use tcpdf to render pdf from html. it provides more flexibility
https://tcpdf.org/examples/
see this code, this will generate dummy invoice.
<?php
function generateInvoice(){
$invoice_no = 0001;
require_once('tcpdf/tcpdf.php');
class MYPDF extends TCPDF
{
public function Header() {
}
// Page footer
public function Footer() {
// Position at 15 mm from bottom
$this->SetY(-15);
// Set font
$this->SetFont('helvetica', 'I', 12);
}
}
// create new PDF document
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// 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(0, 0, 0);
$pdf->SetHeaderMargin(0);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
// set auto page breaks
$pdf->SetAutoPageBreak(True, 0);
// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// set some language-dependent strings (optional)
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
require_once(dirname(__FILE__).'/lang/eng.php');
$pdf->setLanguageArray($l);
}
// ---------------------------------------------------------
//set custom font
//$fontname = TCPDF_FONTS::addTTFfont('OpenSans-Semibold.ttf', 'TrueTypeUnicode', '',12);
// use the font
$pdf->SetFont($fontname, '', 12, '', false);
// add a page
$pdf->AddPage();
$pdf->setFontSize(40);
$pdf->Write(0, 'Company', '', 0, 'C', true, 0, false, false, 0);
$pdf->setFontSize(12);
$y = $pdf->getY();
$pdf->writeHTMLCell(100, 90, '18', $y+10, '<table cellspacing="0" style="background:#000;" cellpadding="1">
<tr>
<td align="left" columnspan="1" style="font-size:15px" width="80" > Name</td>
<td align="left" style="font-size:15px" width="">name surname</td>
</tr>
<tr>
<td align="left" columnspan="1" style="font-size:15px" width="80" > Address</td>
<td align="left" style="font-size:15px" width="">India</td>
</tr>
<tr>
<td align="left" columnspan="1" style="font-size:15px" width="80" > Mob.</td>
<td align="left" style="font-size:15px" width="">0000000</td>
</tr>
<tr>
<td align="left" columnspan="1" style="font-size:15px" width="80" > Email</td>
<td align="left" style="font-size:15px" width="">johnday@johnday.johnday</td>
</tr>
</table>', 0, 0, 0, false, 'J', true);
$y = $pdf->getY();
$pdf->writeHTMLCell(100, 30, 114, $y, '<table cellspacing="0" style="background:#000;" cellpadding="1">
<tr>
<td align="left" columnspan="1" style="font-size:15px" width="100" > Invoice # :</td>
<td align="left" style="font-size:15px" width="">1000</td>
</tr>
<tr>
<td align="left" columnspan="1" style="font-size:15px" width="100" > Invoice Date</td>
<td align="left" style="font-size:15px" width="">22/02/2016</td>
</tr>
</table>', 0, 0, 0, false, 'J', true);
$y = $pdf->getY();
//$pdf->writeHTMLCell('',10,'',$y+5,$html, 0, 1, 0, true, 'C', true);
$pdf->writeHTML($html, true, false, false, false, '');
$y = $pdf->getY();
$data ='<table cellspacing="0" style="background:#000;" border="1" cellpadding="2">
<thead>
<tr style="background-color:#f2f2f2;">
<th align="center" columnspan="1" style="font-size:15px" width="140" > item</th>
<th align="center" columnspan="1" style="font-size:15px" width="140" > price</th>
<th align="center" columnspan="1" style="font-size:15px" width="140" > quantity</th>
<th align="center" columnspan="1" style="font-size:15px" width="140" > subtotal</th>
<th align="center" columnspan="1" style="font-size:15px" width="140" > total</th>
</tr>
</thead>
<tbody>
<tr style="background-color:#fff;">
<td align="center" columnspan="1" style="font-size:15px" width="140" >book</td>
<td align="center" columnspan="1" style="font-size:15px" width="140" >2</td>
<td align="center" columnspan="1" style="font-size:15px" width="140" >2</td>
<td align="center" columnspan="1" style="font-size:15px" width="140" >4</td>
<td align="center" columnspan="1" style="font-size:15px" width="140" >4</td>
</tr>
<tr><td></td>
<td></td>
<td></td>
<td align="left" columnspan="1" style="font-size:15px" width="140" > Subtotal</td>
<td align="left" style="font-size:13px" width="140">$'. number_format((float) (121), 2, '.', '').'</td>
</tr>
<tr style="background-color:#f2f2f2;">
<td></td>
<td></td>
<td></td>
<td align="left" columnspan="1" style="font-size:15px" width="140" > GST</td>
<td align="left" style="font-size:13px" width="140">included</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td align="left" style="font-size:15px" columnspan="1" width="140"> Discount</td>
<td align="left" style="font-size:13px" width="140">$'.number_format((float) (12121.212), 2, '.', '') .'</td>
</tr>
<tr style="background-color:#f2f2f2;">
<td></td>
<td></td>
<td></td>
<td align="left" style="font-size:15px" columnspan="1" width="140" > Amount Paid</td>
<td align="left" style="font-size:13px" width="140">$'.number_format((float) (1212), 2, '.', '').'</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td align="left" style="font-size:15px" columnspan="1" width="140" > Amount Due</td>
<td align="left" style="font-size:13px" width="140">$'.number_format((float) (1221), 2, '.', '').'</td>
</tr>
</table>
</td>
</tr>
</tbody>
</table>';
//$pdf->writeHTMLCell('', '','',$y+8,$data, 0, 1, 0, true, 'C', true);
$pdf->writeHTML($data, true, false, false, false, '');
$y = $pdf->getY();
$pdf->writeHTMLCell('', '', '4',$y+4, '<table cellspacing="0" style="background:#000;" cellpadding="2">
<tr style="background-color:#21B67B;">
<td align="center" style="font-size:20px;color:#fff;" columnspan="0" width="390">PAYMENT METHODS WE ACCEPT</td>
</tr>
<tr>
<td align="left" style="font-size:15px;" width="440">We accept payment by cash, credit card or bank deposit<br></td>
</tr>
<tr>
<td align="left" style="font-size:15px;" width="400">Our Bank Details: </td>
</tr>
<tr>
<td align="left" style="font-size:15px;" width="400">Bank name: demo Bank </td>
</tr><tr>
<td align="left" style="font-size:15px;" width="400">BSB Number: 000000 </td>
</tr><tr>
<td align="left" style="font-size:15px;" width="400">Account Number: 00000 </td>
</tr>
<tr>
<td align="left" style="font-size:15px;" width="400">Account Name: demo demo Ltd </td>
</tr>
<tr>
<td align="left" style="font-size:15px;" width="400"></td>
</tr>
<tr>
<td align="left" style="font-size:15px;" width="400">To make a payment via credit card please call our office on (000) 0000 0000. Cash can be paid on delivery.</td>
</tr>
<tr>
<td align="left" style="font-size:15px;" width="400"></td>
</tr>
<tr>
<td align="center" style="font-size:24px;color:#21B67B;" width="400">Thank you for your business!</td>
</tr>
</table><br<br>', 0, 0, 0, false, 'J', true);
$pdf->lastPage();
$pdf->Output($invoice_no.'.pdf', 'I');
return $invoice_no.'.pdf';
}
generateInvoice();
?>
Print webpage as PDF in PHP
I suggest mpdf. I'ts very simple, you can create your html as string and render it as pdf.
require_once("mpdf/mpdf.php");
$html = "<p>Hello World</p>";
$mpdf = new mPDF('c', 'A4');
$mpdf->WriteHTML($html);
print $mpdf->Output();
How to convert a PHP web page to PDF?
You can use MPDF, a PHP library which generates PDF files from UTF-8 encoded HTML. It's under GNU GPL v2 licence.
https://mpdf.github.io/
https://github.com/mpdf/mpdf
How can we print the current webpage to a pdf file using php
Perhaps you might be looking for this.
<a href="http://pdfmyurl.com?url=www.mypage.com">download My page as pdf</a>
Also this question should help you. https://stackoverflow.com/questions/910243/how-to-convert-an-html-to-pdf-in-php-using-fpdf-lib-1-6
Related Topics
PHP - Simplexml - Addchild with Another Simplexmlelement
Php: Fastest Way to Check for Invalid Characters (All But A-Z, A-Z, 0-9, #, -, ., $)
Can a PHP File Name (Or a Dir in Its Full Path) Have Utf-8 Characters
Png Transparency Resize with Simpleimage.PHP Class
Save CSV Files into MySQL Database
Yii Framework: Using Data from Related Active Record Models for Searching
When Do/Should I Use _Construct(), _Get(), _Set(), and _Call() in PHP
Customizing My-Account Addresses Fields in Woocommerce 3
Callback Function Return Return($Var & 1)
How to Insert JSON Array into MySQL Database
How to Put the Results of a MySQLi Prepared Statement into an Associative Array
Laravel 5 Auth Post Submit - Tokenmismatchexception in Verifycsrftoken.PHP Line 46
How to Remove Text Between Tags in PHP
PHP Prepared Statements and Transactions in a Loop