Functions in FPDF

In this article we will review some of the most important functions that we can find in the PHP FPDF library.

In it we placed a very simple example of how to create a pdf file from PHP. Well, now we are going to see one by one the functions that appeared in said example.

AddPage(); $pdf->SetFont(‘Arial’,’B’,16); $pdf->Cell(40,10,’Hello, World!’); $pdf->Output(); ?>

pdff()

Let’s start with the FPDF() constructor class that allows us to create the pdf document and format it. We have to take into account that the parameters that we pass will be used in all the methods. Its syntax is the following:

FPDF(]);

  • Orientation is the form of placement of the page, that is, we must indicate if it is normal or landscape. The default value “P” is normal. The value for landscape is “L”
  • unit is the user measurement and its possible values ​​are: “pt” point, “mm” millimeter, “cm” centimeter and “in” inch. The default value is “mm”
  • page format. It can have the following values: A3, A4, A5, Letter, and Legal. Default is A4

An example would be the following:

$pdf=new FPDF(‘L’,’pt’,’Legal’);

AddPage()

This function adds a new page to the pdf document. As parameters it only has the orientation and the format, the rest of the characteristics are taken by default from the constructor.

Its syntax is the following:

$pdf->AddPage(]);

The orientation and format parameters are the same as in FPDF(). If you don’t pass parameters to it, it will automatically take those from the constructor.

setFont

It is the function that allows us to establish the font format used in the pdf file. It is mandatory to call this function at the beginning of the file creation, otherwise the document would not be valid.

See also  Network security: Firewall

If we want to add a font type that is not in the standard we must use the AddFont() function; which we will see later.

The SetFont syntax is as follows:

SetFont(string family]);

  • family: font family that can be the standards (Courier, Helvetica or Arial, Times, Symbol, ZapfDingbats) or add one using AddFont();
  • style: font style that can be regular, bold “B”, italic “I” and subscript “U”.
  • size: font size in points. Its default value is 12.

An example would be the following:

SetFont(‘Helvetica’,’I’,13);

Cell

This function prints a cell where we are going to print our text. It has optional borders and background color. In this cell we can place the text aligned or centered.

Its syntax is the following:

Cell(float w ]]]]]])

  • w: width of the cell. If we put 0 the cell extends to the right margin.
  • H: height of the cell.
  • Text: the text that we are going to add.
  • Border: tells us if they are going to be visible or not. if it is 0 they will not be visible, if it is 1 the borders will be seen.
  • Ln: tells us where to start writing after calling this function. Being 0 to the right, 1 at the beginning of the next line, 2 below.
  • Align: to align the text. “L” aligned to the left, “C” centered and “R” aligned to the right.
  • Fill: tells us if the background of the cell is going to be colored or not. values ​​are True or False

An example would be the following:

$pdf->Cell(10,10,’We are viewing’,1,1,’C’);

See also  Control structures in Javascript

Output()

It sends the document to the browser, to a local file or to a string. We can open it in a dialog box or prepare it for a download.

Its syntax is the following:

string Output()

  • name: we give a name to the file, if it is not indicated it calls it by default doc.pdf
  • destination: delivery destination in the document. “I” sends the file to the browser with the option to save as…, “D” sends the document to the browser ready for download, “F” saves the file to a local file, “S” returns the document as a string.

An example would be the following:

$fpdf->Output(‘test’,’I’);

These are just some of the most important functions of the FPDF library. Don’t worry that we haven’t seen more complete examples, because we’ll be using them in a lot of the code that we’ll see next in the .

In the next installment we are going to address the .

Loading Facebook Comments ...
Loading Disqus Comments ...