A few days ago I was working on a project that required an option to generate a pdf file of the product table. I used dompdf for this; it was working fine generating the HTML table as pdf. It was a good tool but the problem arose when cross browser issue came to concern. Also, there was a bug in that tool. Actually, that was not a bug. When the pdf file size increases it returns a 500 server error. And not returning the file. Then I decided to write the pdf file cell by cell. So I moved to fpdf tool. But there I also faced a problem. Mostly the layout issues. So I was searching another tool and found tcpdf. It was great and saved the day. Today I am going to discuss one of the tcpdf features to create pdf from HTML. You can find all methods and parameters in their site. But I am writing this for a quick help.
How to use
Download the files from this link http://sourceforge.net/projects/tcpdf/files/
And add the required files like this.
require get_template_directory() . '/inc/quotation/tcpdf/tcpdf.php';
Add general settings like setup page, page size, and other things by using the code below in a new page that you want to open as pdf page in a browser.
$pdf = new TCPDF('P', 'pt', 'A4', true, 'UTF-8', false);
$pdf->SetCreator('');
$pdf->SetAuthor('');
$pdf->SetTitle('');
$pdf->SetSubject('');
$pdf->SetKeywords('');
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->SetDefaultMonospacedFont('');
$pdf->SetMargins(10, 10, 10, true);
$pdf->SetAutoPageBreak(TRUE, 10);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$pdf->setLanguageArray($l);
$pdf->SetDisplayMode('fullpage', 'SinglePage', 'UseNone');
$pdf->SetTextColor(119, 119, 119);
$pdf->SetDrawColor(119, 119, 119, false, '');
$pdf->AddPage('P', 'A4');
For adding HTML to pdf and generate the output you have to add HTML as a string and have to use write HTML method.
$html = ‘your html content goes gere’; $pdf->writeHTML($html, true, false, true, false, ''); $pdf->Output($quotation_id . '.pdf', 'D');
For writing HTML into a cell you can set the page position and add it. You can use all tags in that methods. So basically adding an image to your pdf is not an issue anymore. Because you can easily add the image tag to it.
$pdf->SetXY(0,110); $pdf->writeHTMLCell(125, 125, 10, 10, 'Hello World !!!', 0, 0, false, true, 'L', false);
Always remember a thing for designing layout that tcpdf is good for layout designing of a pdf but it doesn’t always support all CSS. Basic CSS and in line CSS are the best ways to keep the design in tcpdf.
If you want to design a table then always try to use table default properties like valign width etc methods. Otherwise, your tries will be in vain.
Thanks for reading this small article and i look forward to receiving your thoughts.