1

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.

15
  • You will need to render the HTML page using a layout engine, which I'm not sure if that is possible in just PHP
    – Brad
    Commented Jan 18, 2013 at 6:51
  • use MPDF instead it will convert HTML to PDF and it support lots of CSS properties. Commented Jan 18, 2013 at 7:06
  • After creating html pass you html in $tcpdf->writeHTML($html, true, false, true, false, ''); function. as I have mentioned you take reference TCPDF examples.
    – Roopendra
    Commented Jan 18, 2013 at 7:09
  • i download and copy paste the tcpdf folder in my application folder , then what is the next step , which php file i import into my page Commented Jan 18, 2013 at 7:14
  • hey roopendra i create a php page which uses flurry datas and high charts , now i want to convert it into pdf format, when the user clicks the page datas should be downloadble as pdf i put a link download pdf . pls help me Commented Jan 18, 2013 at 7:18

4 Answers 4

0

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/

11
  • Assign your html page need to render as pdf into $data in above example
    – Sanjay
    Commented Jan 18, 2013 at 6:59
  • i already created data and html elements without using $data then how ? Commented Jan 18, 2013 at 7:04
  • You just need to assign that created data into $data, because $data we are passing into tcpdf to render as pdf.So if you are already cretaed the html like this <div class="pdf">data to show</div> just assign it as $data = '<div class="pdf">data to show</div>';
    – Sanjay
    Commented Jan 18, 2013 at 7:08
  • hi sanjay , i have already charts in page when the user clciks these created charts and data (using flurry , datas) want to converted into pdf Commented Jan 18, 2013 at 7:54
  • Yes you need to pass that data into page that render as pdf and make that into html. Html is converted into pdf .In tcpdf document you get many examples to customize the pdf and its layout
    – Sanjay
    Commented Jan 18, 2013 at 7:59
0

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

0
0

Use the simple or advanced API from htm2pdf.co.uk

2
  • how ? pls tell me the steps Commented Jan 18, 2013 at 7:55
  • pls see their website for full info Commented Jan 20, 2013 at 9:01
0

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');
?>
1
  • Welcome to SO! Please realize this question dates from early 2013 (almost 3 years ago). You can still answer of course, especially when new tools / libraries / versions are available and open room to new and better solutions. In that case, please detail the reasons why you think your answer would outperform the ones already provided.
    – ghybs
    Commented Nov 15, 2015 at 9:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.