[go: up one dir, main page]

0% found this document useful (0 votes)
12 views21 pages

Reporting in PHP (Day-2)

The document discusses the creation of PDF files using the FPDF library in PHP, outlining its advantages, such as being free, flexible, and widely used. It provides a step-by-step guide on how to create PDF documents, including adding pages, setting fonts, and printing text. Additionally, it details various methods and parameters available in the FPDF library for customizing PDF output.

Uploaded by

miraslam aslam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views21 pages

Reporting in PHP (Day-2)

The document discusses the creation of PDF files using the FPDF library in PHP, outlining its advantages, such as being free, flexible, and widely used. It provides a step-by-step guide on how to create PDF documents, including adding pages, setting fonts, and printing text. Additionally, it details various methods and parameters available in the FPDF library for customizing PDF output.

Uploaded by

miraslam aslam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

PDF REPORTING

Why Create PDF files?


• Because your client wants one.
• Because you want to have a 'saveable' page.
• Because you want an 'immutable' page.
• Because some things are done easier in PDFs
then HTML.
• Because you may want a password protected
document.
• And Because of the wonderful things it does...
PDF Reporting
Using FPDF
Introduction
PHP has several libraries for generating PDF
documents. We will use the popular fpdf library.
The FPDF library is a set of PHP code you include in
your scripts with the require function.
The basic concepts of the structure and features of a
PDF file should be common to all the pdf libraries.

This library (FPDF) is available at http://www.fpdf.org.


Documentation at: http://www.fpdf.org/en/doc/
Why FPDF?
FPDF is an existing library, used by many people.
Pros:
• Written by somebody else - no re-inventing the
wheel
• Tested, debugged, and hopefully working code
• Flexible, extensible, many different modules
• Many examples of different things you can do with it
• The price is right! Free!
Cons:
• A bit of a learning curve to get started
• Infrequently updated (Could just be 'done' at this
point)
Documents and Pages
•A PDF document is made up of a number of pages. Each page
contains text and/or images.
Here we will see:
–how to make a document
–create pages in that document
–set font for the document
–put text onto the pages
–send the pages back to the browser

But first we will:

<?php
require("../fpdf/fpdf.php"); // path to fpdf.php
?>
Documents and Pages (contd…)
Example# 1:

<?php
require("../fpdf/fpdf.php"); // path to fpdf.php
$pdf = new FPDF( ); // create object of FPDF
$pdf->AddPage( ); // add a page to document
$pdf->SetFont('Arial','B',16); // set fonts of the text
$pdf->Cell(40,10,'Hello Out There!'); // add text to the page
$pdf->Output( ); // send document to browser,
means output the document
?>
FPDF ( ) constructor parameters Parameter options

P
Portrait; default
Orientation
L
Landscape

pt
Point (1/72 of an inch)
in
Inch
Units of Measurement
mm (default)
Millimeter

cm
Centimeter

Letter Legal
A5
Page Size A3
A4- (default)
An array containing width and height
AddPage()
Syntax:
AddPage([string orientation [, mixed size [, int rotation]]])
Description:
Adds a new page to the document.
Parameters:
orientation: Page orientation. Possible values are (case insensitive):
P or Portrait, L or Landscape
The default value is the one passed to the constructor.
size: Page size. It can be either one of the following values (case insensitive):
A3, A4, A5, Letter, Legal
or an array containing the width and the height (expressed in user unit).
Default values are that of a constructor..
Rotation: Angle by which to rotate the page. It must be a multiple of 90; positive
values mean clockwise rotation. The default value is 0.
SetFont()

Syntax:
SetFont(string family [, string style [, float size]])

Description:
Sets the font used to print character strings. It is mandatory to call
this method at least once before printing text or the resulting
document would not be valid.
Cell()

Syntax:
Cell(float w [, float h [, string txt [, mixed border [, int ln [, string align [, int
fill [, mixed link]]]]]]])

Description:
Prints a cell (rectangular area) with optional borders, background
color and character string. The upper-left corner of the cell
corresponds to the current position. The text can be aligned or
centered. After the call, the current position moves to the right or to
the next line. It is possible to put a link on the text.
If automatic page breaking is enabled and the cell goes beyond the
limit, a page break is done before outputting.
SetFillColor()

Syntax:
SetFillColor(int r [, int g, int b])

Description:
Defines the color used for all filling operations (filled rectangles and
cell backgrounds). It can be expressed in RGB components or gray
scale. The method can be called before the first page is created
and the value is retained from page to page.

Parameters:
r
If g and b are given, red component; if not, indicates the gray level. Value between 0 and 255.
SetLineWidth()

SetLineWidth(float width)
Description
Defines the line width. By default, the value equals 0.2 mm. The
method can be called before the first page is created and the
value is retained from page to page.
MultiCell()

Syntax:
MultiCell(float w, float h, string txt [, mixed border [, string align
[, boolean fill]]])

Description:
This method allows printing text with line breaks. They can be
automatic (as soon as the text reaches the right border of the cell)
or explicit (via the \n character). As many cells as necessary are
output, one below the other. Text can be aligned, centered or
justified. The cell block can be framed and the background painted.
Output()
string Output([string dest [, string name [, boolean isUTF8]]]

Parameters
name:

The name of the file. If not specified, the document will be sent to the browser
(destination I) with the name doc.pdf.
dest:

Destination where to send the document. It can take one of the following values:
I: send the file inline to the browser. The plug-in is used if available. The name given by
name is used when one selects the "Save as" option on the link generating the PDF.
D: send to the browser and force a file download with the name given by name.
F: save to a local file with the name given by name (may include a path).
S: return the document as a string. name is ignored.
Text()

Syntax:
Text(float x, float y, string txt)

Description:
Prints a character string. The origin is on the left of the first
character, on the baseline. This method allows to place a string
precisely on the page, but it is usually easier to use Cell(),
MultiCell() or Write() which are the standard methods to print text.
Write()

Syntax:
Write(float h, string txt [, mixed link])

Description:
This method prints text from the current position. When the right
margin is reached (or the \n character is met) a line break occurs
and text continues from the left margin. Upon method exit, the
current position is left just at the end of the text.
It is possible to put a link on the text.
SetX() & SetY()

Syntax:
SetX(float x)
SetY(float y)

Description:
Sets Position of all preceding elements

Note: SetX and SetY should not be used together. For this purpose
use SetXY()
SetXY()

Syntax:
SetXY(float x, float y)

Description:
Defines the ordinate of the current position. If the passed values
are negative, they are relative respectively to the right and bottom
of the page.
Line()

Syntax:
Line(float x1, float y1, float x2, float y2)

Description:
Draws a line between two points.
Ln()

Syntax:
Ln([float h])

Description:
Performs a line break.

Parameters:
h
The height of the break.
By default, the value equals the height of the last printed cell.

You might also like