ForgePDF is a PHP library based on the FPDF concept, but modernized to take advantage of features from newer PHP versions. It has been optimized for better performance and lower memory consumption, allowing efficient and customized PDF document generation.
ForgePDF is available via Composer. To install it, run:
composer require luanoldcode/forgepdf
Include the autoloader in your code:
require 'vendor/autoload.php';
use ForgePDF\ForgePDF;
$pdf = new ForgePDF();
$pdf->addPage();
$pdf->setFont('Arial', 'B', 16);
$pdf->cell(40, 10, 'Hello, ForgePDF!');
$pdf->output('my_document.pdf');
addPage()
: Adds a new page to the document.setFont($family, $style, $size)
: Sets the text font.cell($width, $height, $text)
: Creates a cell with the specified text.output($filename)
: Saves the PDF to the specified path or displays it in the browser.You can create custom headers and footers by overriding the header
and footer
methods.
class MyPDF extends ForgePDF {
public function header() {
$this->setFont('Arial', 'B', 12);
$this->cell(0, 10, 'My Header', 0, 1, 'C');
}
public function footer() {
$this->setY(-15);
$this->setFont('Arial', 'I', 8);
$this->cell(0, 10, 'Page ' . $this->pageNo(), 0, 0, 'C');
}
}
$pdf = new MyPDF();
$pdf->addPage();
$pdf->setFont('Arial', '', 12);
$pdf->cell(0, 10, 'Document content');
$pdf->output('document_with_header.pdf');
ForgePDF allows you to easily add images to PDF.
$pdf->addPage();
$pdf->image('image_path.jpg', 10, 10, 50, 50);
$pdf->output('pdf_with_image.pdf');
image($file, $x, $y, $w, $h)
: Adds an image to the PDF at the specified coordinates.ForgePDF uses optimized streams to handle large documents.
$pdf->addPage();
$pdf->setFont('Arial', '', 10);
for ($i = 1; $i <= 1000; $i++) {
$pdf->cell(0, 10, "Line $i", 0, 1);
}
$pdf->output('large_document.pdf');
Create styled tables with ForgePDF using aligned cells.
$pdf->addPage();
$pdf->setFont('Arial', 'B', 12);
// Header
$pdf->cell(40, 10, 'Column 1', 1);
$pdf->cell(40, 10, 'Column 2', 1);
$pdf->cell(40, 10, 'Column 3', 1);
$pdf->ln();
// Data
$pdf->setFont('Arial', '', 12);
for ($i = 1; $i <= 10; $i++) {
$pdf->cell(40, 10, "Row $i", 1);
$pdf->cell(40, 10, "Value $i", 1);
$pdf->cell(40, 10, "Other $i", 1);
$pdf->ln();
}
$pdf->output('table.pdf');
constructor - constructor
AcceptPageBreak - accept or not automatic page break
AddFont - add a new font
AddLink - create an internal link
AddPage - add a new page
AliasNbPages - define an alias for number of pages
Cell - print a cell
Close - terminate document
Error - fatal error
Footer - page footer
GetPageHeight - get current page height
GetPageWidth - get current page width
GetStringWidth - compute string length
GetX - get current x position
GetY - get current y position
Header - page header
Image - output an image
Line - draw a line
Link - put a link
Ln - line break
MultiCell - print text with line breaks
Output - save or send the document
PageNo - page number
Rect - draw a rectangle
SetAuthor - set the document author
SetAutoPageBreak - set the automatic page breaking mode
SetCompression - turn compression on or off
SetCreator - set document creator
SetDisplayMode - set display mode
SetDrawColor - set drawing color
SetFillColor - set filling color
SetFont - set font
SetFontSize - set font size
SetKeywords - associate keywords with document
SetLeftMargin - set left margin
SetLineWidth - set line width
SetLink - set internal link destination
SetMargins - set margins
SetRightMargin - set right margin
SetSubject - set document subject
SetTextColor - set text color
SetTitle - set document title
SetTopMargin - set top margin
SetX - set current x position
SetXY - set current x and y positions
SetY - set current y position and optionally reset x
Text - print a string
Write - print flowing text
ForgePDF is licensed under the GNU General Public License v3.0. For more information, visit the official license.
For questions, contributions or suggestions, visit the official repository at LuanOldCode's GitHub. 🚀