PHP Fpdf Class Library application implementation Code _php skill

Source: Internet
Author: User
Tags sprintf
Copy Code code as follows:

<?php
Require (' chinese.php ');
Class PDF extends Pdf_chinese
{
function header ()//Set Header
{
$this->setfont (' GB ', ', ', 10);
$this->write ("XX company product directory");
$this->ln (20); Line Wrap
}
function Footer ()//setting footer
{
$this->sety (-15);
$this->setfont (' GB ', ', ', 10);
$this->cell (0,10, ' $this->pageno (). ' Page ');
}
}

$conn = mysql_connect ("localhost", "root", ""); Connecting to a database

mysql_select_db ("Product", $conn); Execute SQL
$query _rs_prod = "SELECT * from Product order by prod_id";
$rs _prod = mysql_query ($query _rs_prod, $conn) or Die (Mysql_error ());
$row _rs_prod = Mysql_fetch_assoc ($rs _prod);
$totalRows _rs_prod = mysql_num_rows ($rs _prod);

$pdf =new PDF (); Create a new Fpdf object
$pdf->addgbfont (); Set Chinese fonts
$pdf->open (); Start creating PDF
$pdf->addpage (); Add one page

$pdf->setfont (' GB ', ', ', 10); Set font style

$header =array (' Product number ', ' Product name ', ' Product type ', ' Product Unit price '); To set the table header
$width =array (20,80,40,20); Set width per column

for ($i =0; $i <count ($header); $i + +)//Circular output header
$pdf->cell ($width [$i],6, $header [$i],1];
$pdf->ln ();

Do//loop output table body
{
$pdf->cell ($width [0],6, $row _rs_prod[' prod_id '],1);
$pdf->cell ($width [1],6, $row _rs_prod[' prod_name '],1);
$pdf->cell ($width [2],6, $row _rs_prod[' Prod_type '],1);
$pdf->cell ($width [3],6, $row _rs_prod[' Prod_price '],1);
$pdf->ln ();
while ($row _rs_prod = Mysql_fetch_assoc ($rs _prod));

$pdf->output ("Product.pdf", true); Download PDF file
?>

Copy Code code as follows:

<?php
Define (' Fpdf_fontpath ', ' font/'); Define the path where the font folder is located
Require_once (' fpdf/fpdf.php '); Contains Fpdf class library files
$pdf =new FPDF (' P ', ' mm ', ' A4 '); Create a new Fpdf object, vertical paper, in millimeters, paper size A4
$pdf->open (); Start creating PDF
$pdf->addpage (); Add one page
$pdf->setfont (' Courier ', ' I ', 20); Set font style
$pdf->cell (0,0, ' Hello world! '); Add a cell
$pdf->output (); Output PDF to Browser
?>

Copy Code code as follows:

<?php
Define (' Fpdf_fontpath ', ' font/'); Define the path where the font folder is located
Require_once (' fpdf/fpdf.php '); Contains Fpdf class library files
$pdf =new FPDF (' P ', ' mm ', ' A4 '); Create a new Fpdf object, vertical paper, in millimeters, paper size A4
$pdf->open (); Start creating PDF
$pdf->addpage (); Add one page
$pdf->setfont (' Courier ', ' I ', 20); Set font style
$pdf->image (' sight.jpg ', 20,20,0,0); Add a picture with file name Sight.jpg
$pdf->output (); Output PDF to Browser
?>

Copy Code code as follows:

<?php
Define (' Fpdf_fontpath ', ' font/'); Define the path where the font folder is located
Require_once (' fpdf/fpdf.php '); Contains Fpdf class library files
$pdf =new FPDF (' P ', ' mm ', ' A4 '); Create a new Fpdf object, vertical paper, in millimeters, paper size A4
$pdf->open (); Start creating PDF
$pdf->addpage (); Add one page
$pdf->setfont (' Courier ', ' I ', 20); Set font style
$pdf->cell (60,10, ' Hello world! ', 1); Add a cell border to 1
$pdf->output (); Output PDF to Browser
?>

Copy Code code as follows:

<?php
Define (' Fpdf_fontpath ', ' font/'); Define the path where the font folder is located
Require_once (' fpdf/fpdf.php '); Contains Fpdf class library files
$pdf =new FPDF (' P ', ' mm ', ' A4 '); Create a new Fpdf object, vertical paper, in millimeters, paper size A4
$pdf->open (); Start creating PDF
$pdf->addpage (); Add one page

$pdf->setfont (' Arial ', ', ', 14); Set font style

$header =array (' Name ', ' age ', ' Sex ', ' Salary '); To set the table header
$data =array (); Set up the table body
$data [0] = array (' Simon ', ', ', ' Male ', ' 5,000.00 ');
$data [1] = array (' Elaine ', ' n ', ' Female ', ' 6,000.00 ');
$data [2] = array (' Susan ', ', ', ' Female ', ' 7,000.00 ');
$data [3] = array (' David ', ' n ', ' Male ', ' 8,000.00 ');

$width =array (40,40,40,40); Set width per column

for ($i =0; $i <count ($header); $i + +)//Circular output header
$pdf->cell ($width [$i],6, $header [$i],1];
$pdf->ln ();

foreach ($data as $row)//circular output table body
{
$pdf->cell ($width [0],6, $row [0],1);
$pdf->cell ($width [1],6, $row [1],1);
$pdf->cell ($width [2],6, $row [2],1);
$pdf->cell ($width [3],6, $row [3],1);
$pdf->ln ();
}

$pdf->output (); Output PDF to Browser
?>

Copy Code code as follows:

<?php
Define (' Fpdf_fontpath ', ' font/'); Define the path where the font folder is located
Require_once (' fpdf/fpdf.php '); Contains Fpdf class library files
$pdf =new FPDF (' P ', ' mm ', ' A4 '); Create a new Fpdf object, vertical paper, in millimeters, paper size A4
$pdf->open (); Start creating PDF
$pdf->addpage (); Add one page
$pdf->setfont (' Courier ', ' I ', 20); Set font style
$pdf->cell (0,0, ' Hello, FPDF '); Add a cell and export Chinese
$pdf->output (); Output PDF to Browser
?>

Copy Code code as follows:

<?php
Require (' chinese.php ');
Class PDF extends Pdf_chinese
{
function header ()//Set Header
{
$this->setfont (' GB ', ', ', 10);
$this->write ("fpdf Chinese Test");
$this->ln (20);
}

function Footer ()//setting footer
{
$this->sety (-15);
$this->setfont (' GB ', ', ', 10);
$this->cell (0,10, ' $this->pageno (). ' Page ');
}
}

$pdf =new PDF (); Create PDF document
$pdf->addgbfont ();
$pdf->open ();
$pdf->aliasnbpages ();
$pdf->addpage ();
$pdf->setfont (' GB ', ' I ', 20);
$pdf->cell (0,10, ' Hello, FPDF '); Output a section of Chinese
$pdf->output ();
?>

Copy Code code as follows:

<?php
$conn = mysql_connect ("localhost", "root", ""); Connecting to a database
$colname _rs_article = $_get[' id ']; Get parameter ID

mysql_select_db ("CMS", $conn); Execute SQL
$query _rs_article = sprintf ("SELECT * from articles WHERE article_id =%s", $colname _rs_article);
$rs _article = mysql_query ($query _rs_article, $conn) or Die (Mysql_error ());
$row _rs_article = Mysql_fetch_assoc ($rs _article);
$totalRows _rs_article = mysql_num_rows ($rs _article);

function conv ($Text)//processing of returned text
{
$Text =htmlspecialchars ($Text); Convert HTML key characters
$Text =nl2br ($Text); Convert line break
return $Text;
}
?>
<p align= "center" ><b><?php Echo $row _rs_article[' title '];?></b></p>
<p align= "center" ><font size=2><?php Echo $row _rs_article[' author '];?> | <a href= "showpdf.php?id=<?php echo $row _rs_article[' article_id '];?>" > Download PDF document </a></font> </p>
<HR>
<p><?php Echo Conv ($row _rs_article[' content ');?></p>

Copy Code code as follows:

<?php
Require (' chinese.php ');
Class PDF extends Pdf_chinese
{
function header ()//Set Header
{
$this->setfont (' GB ', ', ', 10);
$this->write (10, ' article System-XX website ');
$this->ln (20); Line Wrap
}
function Footer ()//setting footer
{
$this->sety (-15);
$this->setfont (' GB ', ', ', 10);
$this->cell (0,10, ' $this->pageno (). ' Page ');
}
}
Start of the main program
$conn = mysql_connect ("localhost", "root", ""); Connecting to a database
$colname _rs_article = $_get[' id ']; Get parameter ID

mysql_select_db ("CMS", $conn); Execute SQL
$query _rs_article = sprintf ("SELECT * from articles WHERE article_id =%s", $colname _rs_article);
$rs _article = mysql_query ($query _rs_article, $conn) or Die (Mysql_error ());
$row _rs_article = Mysql_fetch_assoc ($rs _article);
$totalRows _rs_article = mysql_num_rows ($rs _article);
Start creating PDF document
$pdf =new PDF ();
$pdf->addgbfont ();
$pdf->open ();
$pdf->aliasnbpages ();
$pdf->addpage ();
$pdf->setfont (' GB ', ' B ', 20);
$pdf->cell (0,10, $row _rs_article[' title ')); Output article title
$pdf->ln (); Line Wrap
$pdf->setfont (' GB ', ', ', 10);
$pdf->cell (0,10, $row _rs_article[' author ')); Output article author
$pdf->ln ();
$pdf->setfont (' GB ', ', ', 12);
$content = $row _rs_article[' content '];
while ($content!= "")//looping the content of the article to PDF on a per-page basis
{
$length = strlen ($content); Get article length
$output = substr ($content, 0, 1024); Get the output of this page, 1 pages per 1024 characters
$pdf->cell (0,10, $output); Output article content
$content = substr ($content, 1024, $length); Get the remaining output content
$pdf->addpage (); Change page
}
$pdf->output ($row _rs_article[' title '). " PDF ", true); Output PDF file, file name is article title
?>

Copy Code code as follows:

<?php
define (' Fpdf_fontpath ', ' font/');//define the path where the font folder is located
Require_once (' fpdf/fpdf.php '); Contains the FPDF class library file

Class PDF extends FPDF
{
Function header ()//Set header
{
$this->setfont (' Ar Ial ', ' B ', 15); Set the header font
$this->cell (80);//Move cell
$this->cell (30,10, ' Title ');//write header text
$this->ln (20);//Line Change }

Function Footer ()//Set footer
{
$this->sety (-15);//Set footer location
$this->setfont (' Arial ', ' I ' , 8); Set footer Font
$this->cell (0,10, ' Page-'. $this->pageno ());//Output The current page number as the footer content

}

$pdf =new PDF ( ' P ', ' mm ', ' A4 '; Create a new Fpdf object, vertical paper, in millimeters, paper size A4
$pdf->open ();//Start creating PDF
$pdf->addpage () Courier ', ' I ', 20); Set font style
$pdf->cell (0,0, ' Hello world! ');//Add one cell
$pdf->output ()//output PDF to browser
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.