Skip to content

A clone of FPDF with an almost compatible interface while using SetaPDF-Core internally for the PDF generation.

License

Notifications You must be signed in to change notification settings

Setasign/SetaFPDF

Repository files navigation

SetaFPDF

SetaFPDF is a clone ofFPDFwith an almost compatible interface while usingSetaPDF-Coreinternally for the PDF generation.

Motivation

The main motivation for this project was to be able to create PDF/A documents in PHP without changing all existing PDF generation scripts which rely onFPDF.

FPDF is a wide spread and very common PHP library for PDF generation. It is small, well tested and runs on millions of websites. It also comes with several extensions and an activeforum.

TheSetaPDF-Corecomponent is a PHP component which allows PHP developers to interact with existing PDF documents on a low level. While there is no high-level API for PDF generation it comes with all low-level features which are required to create PDF/A documents:

  • TrueType font sub-setting
  • Access to XMP metadata
  • Color profiles
  • Support for embedded files / attachments

Additionally it comes with other nice features which would be available automatically:

  • Access to document outlines
  • Access to page labels
  • Support for annotations
  • Support for creating and handling destinations (also named destinations)
  • Support for individual actions
  • Support for other colors and color spaces
  • Standard and public key encryption (up to AES256)
  • ...and much more...

Bringing both SetaPDF and FPDF together result in SetaFPDF: An almost identical interface of FPDF backed up by SetaPDF on the lower level.

Future of this project

This project is not a start of a new PDF generation project in PHP but it is a temporary solution to improve existing projects which rely on FPDF.

Requirements

Installation

Add following to your composer.json:

{
"require":{
"setasign/setafpdf":"^1.0"
},
"repositories":[
{
"type":"composer",
"url":"https:// setasign /downloads/"
}
]
}

and executecomposer update.You need to define therepositoryto evaluate the dependency to theSetaPDF-Corecomponent (seeherefor more details).

Evaluation version

By default this packages depends on a licensed version of theSetaPDF-Corecomponent. If you want to use it with an evaluation version please use following in your composer.json:

{
"require":{
"setasign/setafpdf":"dev-evaluation"
},
"repositories":[
{
"type":"composer",
"url":"https:// setasign /downloads/"
}
]
}

Notice that the evaluation branch depends on the version forPHP 7.1 and up.

Without Composer

Make sure, that theSetaPDF-Corecomponent isinstalledand itsautoloader is registeredcorrectly.

Then simply require thesrc/autoload.phpfile or register following namespaces in your own PSR-4 compatible autoload implementation:

$loader=new\Example\Psr4AutoloaderClass;
$loader->register();
$loader->addNamespace('setasign\SetaFpdf','path/to/src');

Usage

You can use SetaFPDF the same way as FPDF. Their interfaces are almost identically. So just use another constructor and be aware of following differences:

  • TheAddFont()method was changed to allow only TrueType fonts instead of FPDF font definition files.
  • The methodAliasNbPages()will throw an\SetaPDF_Exception_NotImplementedexception throughout. It is removed in favor of a more clean solution by re-accessing the pages afterwards through theSetPage()method. By doing this e.g. the alignment is more consistent than with themarker.
  • The$isUTF8parameter is removed from theOutput()method. The HTTP header is always send with an UTF-8 variant by SetaPDF.
  • The$isUTF8parameter is removed from following methods:SetAuthor(),SetCreator(),SetKeywords(),SetSubject()andSetTitle().You need to pass the parameters in UTF-8 throughout.
  • The methodSetCompression()will throw anSetaPDF_Exception_NotImplementedexception if called withfalseas its argument (SetaPDF has no option to turn compression off).
  • There's noError()method anymore. Exceptions are thrown where the errors occur.

Improved and new methods:

  • TheSetDrawColor(),SetTextColor()andSetFillColor()accept 1 argument (0-255) for grayscale colors, 3 arguments (each 0-255) for RGB and 4 arguments (each 0-100) for CMYK now by default.
  • A new methodSetPage()is introduced to allow you to navigate between pages. With it you can e.g. write page numbers and the final page count into the pages footer or header. This was done before withAliasNbPages().
  • The newgetPageCount()method is self-explaining.
  • ThegetManager()method allows you to get access to the underlaying object structure.

Examples and update information

A simple example will look like:

<?php
use\setasign\SetaFpdf\SetaFpdf;

require_once'vendor/autoload.php';

$pdf=newSetaFpdf();
$pdf->AddPage();
$pdf->AddFont('DejaVuSans','','/path/to/DejaVuSans.ttf');
$pdf->SetFont('DejaVuSans','',20);
$pdf->Write(20,'Love & Ζω!');// Write in UTF-8
$pdf->Output();

Nothing new here. It's code you already know from FPDF. And that's it. In the normal cases you only need to replace the constructor with\setasign\SetaFpdf\SetaFpdf,update theAddFont(),ensure UTF-8 input and you're done!

Page numbering (previously done byFPDF::AliasNbPages())

If your script relies on page numbering which is implemented by the use ofAliasNbPages()in e.g. theHeader()orFooter()methods you need to refactor your script. With FPDF it was done e.g. this way:

classPdfextendsFPDF
{
publicfunctionFooter()
{
$this->SetY(-15);
$this->Cell(0,10,'Page'.$this->PageNo().'/{nb}',0,0,'R');
}
}

$pdf=newPdf();
$pdf->AddPage();
//...
$pdf->AddPage();
//...
$pdf->AddPage();
//...
$pdf->AddPage();
//...
$pdf->Output();

With SetaFPDF you need to iterate over the created pages and write the footer manually:

classPdfextendsSetaFpdf
{
publicfunctionwriteFooters()
{
$this->SetAutoPageBreak(false);

$pageCount=$this->getPageCount();
// iterate through the pages and draw the footer
for($pageNo=1;$pageNo<=$pageCount;$pageNo++) {
$this->SetPage($pageNo);
$this->SetY(-15);
$this->Cell(0,10,'Page'.$this->PageNo().'/'.$pageCount,0,0,'R');
}

$this->SetAutoPageBreak(true);
}
}

$pdf=newPdf();
$pdf->AddPage();
//...
$pdf->AddPage();
//...
$pdf->AddPage();
//...
$pdf->AddPage();
//...
// write the footers
$pdf->writeFooters();
//...
$pdf->Output();

Import existing pages from existing PDF documents

There is also a clone forFPDIavailable. If you need the methods of FPDI just use the classSetaFpdiand you can use methods likesetSourceFile(),importPage()anduseTemplate():

<?php
use\setasign\SetaFpdf\SetaFpdi;

require_once'vendor/autoload.php';

$pdf=newSetaFpdi();

$pageCount=$pdf->setSourceFile('/path/to/template.pdf');

for($pageNo=1;$pageNo<=$pageCount;$pageNo++) {
$tpl=$pdf->importPage($pageNo);
$pdf->AddPage();
$pdf->useTemplate($tpl,['adjustPageSize'=>true]);
}

$pdf->Output();

Generating PDF/A documents

With SetaFPDF you are able to create PDF/A documents. Anyhow it's up to you to not use features which are not allowed in PDF/A. Also it's up to you to add the missing puzzle pieces through the underlaying SetaPDF functionallities. There's a simple example available indemos/pdf-a-3b.phpthat adds the required pieces to make the PDF PDF/A conform.

If you want to create PDF/A documents while importing other files, you need to make sure, that these documents are PDF/A already.

License

This package is open-sourced software licensed under theMIT license.

About

A clone of FPDF with an almost compatible interface while using SetaPDF-Core internally for the PDF generation.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages