I want to create pdf file from my web page written by php . My document must produce from mysql and produce pdf file.I want to be to save this pdf and to read.Please give me code sample.
-
1You can use for example [TCPDF](http://www.tecnick.com/public/code/cp_dpage.php?aiocp_dp=tcpdf) library to generate PDFs. This site also has some code examples. As for fetching and manipulating the data from your db you will have to code this yourself. – RaYell Jul 30 '09 at 05:40
-
I think [fpdf](http://www.fpdf.org) Provides most simple way to create PDFs with sample codes given on site as well. Here you can find with example [http://www.fpdf.org/en/script/script10.php](http://www.fpdf.org/en/script/script10.php) – Milap Jan 28 '12 at 06:33
-
@bb. could you take a look at my question http://stackoverflow.com/questions/16925076/php-creating-a-pdf-how-can-i-get-this-working-errors-populating/16925161?noredirect=1#16925161 – ValleyDigital Jun 04 '13 at 18:54
4 Answers
Using the TCPDF library:
<?
require_once("tcpdf/tcpdf.php");
$pdf = new TCPDF();
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor("my name");
$pdf->SetTitle("my doc");
$pdf->SetSubject("x");
$pdf->SetKeywords("a, b, c");
// set default header data
$pic = "/gfx/header.png";
$pdf->SetHeaderData(realpath($pic), "25", "Title");
// 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 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
$pdf->setLanguageArray($l);
//initialize document
$pdf->AliasNbPages();
// add a page
$pdf->AddPage();
// ---------------------------------------------------------
// set font
$pdf->SetFont("helvetica", "", 12);
// set a background color
$pdf->SetFillColor(230, 240, 255, true);
$pdf->SetFont("", "b", 16);
$pdf->Write(16, "some text\n", "", 0, 'C');
$pdf->Output("filename.pdf", "I");
?>

- 1,371
- 1
- 14
- 21
-
I test this code.But I found error at $pdf->Write(16, "some text"\n", "", 0, 'C'); $pdf->Output("filename.pdf", "I"); these lines.How do I solve? – thinzar Jul 31 '09 at 05:27
-
I you already have an HTML page, the fastest way for you might be to use a tool like HTML2PDF to "convert" that HTML data to PDF, instead of generating a PDF file "from scratch".
Quoting that page :
It allows the conversion of valid HTML 4.01 in PDF format, and is distributed under LGPL.
This library has been designed to handle mainly TABLE intertwined to generate invoices delivery, and other official documents. It does not yet have all the tags.
There are some example of HTML, and the corresponding PDF files, so you can see what's capable of.

- 395,085
- 80
- 655
- 663
Download newest version of TCPDF source code from https://github.com/tecnickcom/tc-lib-pdf , The Source code itself have a directory which the folder named as examples. you can check out the code from the examples or else you can try below code.
<?php
require_once('tcpdf/config/tcpdf_config.php');
require_once('tcpdf/tcpdf.php');
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set default font subsetting mode
$pdf->setFontSubsetting(true);
// set margins
//$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
//$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
// set default header data
$pdf->SetHeaderData('', '', 'Hello ','Gudmrng', array(0,64,255), array(0,64,128));
$pdf->setFooterData(array(0,64,0), array(0,64,128));
// 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 font
// dejavusans is a UTF-8 Unicode font, if you only need to
// print standard ASCII chars, you can use core fonts like
// helvetica or times to reduce file size.
$pdf->SetFont('dejavusans', '', 10, '', true);
// $pdf->SetFont('helvetica', '', 8);
// Add a page
// This method has several options, check the source code documentation for more information.
$pdf->AddPage();
$tbl_header = '<h2 style="color:blue;text-align: center;font-size: 14px;">Headding</h2></br>';
$html = '<table width="530" cellspacing="0" cellpadding="3" border="1">
<tr>
<th style="text-align: center;font-size: 10px;font-weight: bold;width:100px;">heading 1</th>
<th style="text-align: center;font-size: 10px;font-weight: bold;width:122px;">heading 2</th>
</tr>';
$html .= '<tr>';
$html .= '<td><b>Hello World</b></td>';
$html .= '<td><b>Helo </b></td>';
$html .= '</tr>';
$html .= '</table>';
$pdf->writeHTML($tbl_header . $html, true, false, false, false, '');
//$pdf->writeHTML($html, true, 0);
//$pdf->writeHTML($html, true, 0);
$pdf->Output('test_1.pdf','I');
?>

- 531
- 5
- 27
i used CutyCapt for do that.
- download and install a CutyCapt ! and xvfb-run .
- make pdf style in html page and then create pdf from this page by this command:
xvfb-run cutycapt --min-width=1485 --url='html url' --out='path to pdf'

- 90
- 1
- 4