I used several search and methods to find this convertion from html to pdf
but I didn't got. What is the way to convert an HTML page to PDF? I already downloaded tcpdf library
but I don't how I to use it.
Thanks in advance.
Use TCPDF
Please download tcpdf and extrat it and put it in your application folder
And in the page that you need to convert pdf please add html part need to show in pdf into a variable
eg: $data = "Hello World"; /include tcpdf here/
$tcpdf = new TCPDF();
$textfont = 'helvetica';
$tcpdf->SetAuthor("Julia Holland");
$tcpdf->SetAutoPageBreak(true);
$tcpdf->setPrintHeader(false);
$tcpdf->setPrintFooter(false);
$tcpdf->SetTextColor(0, 0, 0);
$tcpdf->SetFont($textfont,'',9);
$tcpdf->AddPage();
$tcpdf->writeHTML($data, true, 0, true, 0);
$tcpdf->Output('filename11.pdf', 'I');
Now you can use custom fonts,style etch that are listed in tcpdf site and this one prints Hello world in pdf
download tcpdf from here http://www.tcpdf.org/
Please Use tcpdf , try example 6 in tcpdf examples link http://www.tcpdf.org/examples.php
try this example :
require_once('write path to include /config/lang/eng.php');
require_once('write path to include tcpdf.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 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 auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
//set some language-dependent strings
$pdf->setLanguageArray($l);
// ---------------------------------------------------------
// set font
$pdf->SetFont('dejavusans', '', 10);
// add a 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 some HTML content
$html = 'Write your html code here';
// output the HTML content
$pdf->writeHTML($html, true, false, true, false, '');
$this->pdf->lastPage();
ob_clean();
//Close and output PDF document
$this->pdf->Output($filename, 'D'); //D if you want download it use I if you want open it on new tab
Change constant value e.g PDF_HEADER_LOGO from tcpdf/config/tcpdf_config.php
follow this link http://www.tcpdf.org/ and try this
<?php
// Include the main TCPDF library (search for installation path).
require_once('../lib/tcpdf/tcpdf.php');
// extend TCPF with custom functions
class MYPDF extends TCPDF {
// Colored table
public function ColoredTable($header,$data) {
// Colors, line width and bold font
$this->SetFillColor(255, 0, 0);
$this->SetTextColor(255);
$this->SetDrawColor(128, 0, 0);
$this->SetLineWidth(0.3);
$this->SetFont('', 'B');
// Header
$w = array(40, 35, 40, 45);
$num_headers = count($header);
for($i = 0; $i < $num_headers; ++$i) {
$this->Cell($w[$i], 7, $header[$i], 1, 0, 'C', 1);
}
$this->Ln();
// Color and font restoration
$this->SetFillColor(224, 235, 255);
$this->SetTextColor(0);
$this->SetFont('');
// Data
$fill = 0;
foreach($data as $row) {
$this->Cell($w[0], 6, $row[0], 'c');
$this->Cell($w[1], 6, $row[1], 'c');
$this->Cell($w[2], 6, number_format($row[2]), 'LR', 0, 'R', $fill);
$this->Cell($w[3], 6, number_format($row[3]), 'LR', 0, 'R', $fill);
$this->Ln();
$fill=!$fill;
}
$this->Cell(array_sum($w), 0, '', 'T');
}
}
// create new PDF document
$pdf = new MYPDF(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 011');
$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.' 011', 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)
$lg = Array();
$lg['a_meta_charset'] = 'UTF-8';
$lg['a_meta_dir'] = 'rtl';
$lg['a_meta_language'] = 'fa';
$lg['w_page'] = 'page';
// set some language-dependent strings (optional)
$pdf->setLanguageArray($lg);
// ---------------------------------------------------------
// set font
$pdf->SetFont('aealarabiya', '', 18);
// add a page
$pdf->AddPage();
// column titles
$header = array('Country', 'Capital', 'Area (sq km)', 'Pop. (thousands)');
// data loading
$data = array('data1' , 'data2' , '323' , '243');
// print colored table
$pdf->ColoredTable($header, $data);
// ---------------------------------------------------------
// close and output PDF document
ob_end_clean();
$pdf->Output('example_011.pdf', 'I');
?>
$tcpdf->writeHTML($html, true, false, true, false, '');
function. as I have mentioned you take referenceTCPDF
examples.