Getting started with FPDF & FPDI

https://manuals.setasign.com/fpdi-manual/

I am using V1 since more tutorials are available in it

FPDI & FPDF Installation

composer require setasign/fpdi-fpdf 1.*
OR
composer require setasign/fpdi-fpdf 2.*

FPDF Hello World Example

<?php
error_reporting(0);
require_once "vendor/setasign/fpdf/fpdf.php";

$templatesDocsFolder = "docs/templates/";
$templatesDocName = "template3.pdf";
$templatesDocPath = $templatesDocsFolder.$templatesDocName;


$generatedDocsFolder = "docs/generated/";
$generatedDocName = $templatesDocName.date('d-m-Y_his').".docx";
$generatedDocPath = $generatedDocsFolder.$generatedDocName;

$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
?>

FPDI Merge PDFs & Put Page numbers

<?php
/*
ERRORS ENCOUNTERED
FPDF error: This document (testcopy.pdf) probably uses a compression technique which is not supported by the free parser shipped with FPDI

SOLUTION:
https://stackoverflow.com/questions/12154190/fpdf-error-this-document-testcopy-pdf-probably-uses-a-compression-technique-w
 FPDF library supports only PDF version 1.4 & lesser. So to over come from this issue use GhostScript. This script helps to change the PDF versions dynamically.

(1) Download the Ghostscript here. https://www.ghostscript.com/download/gsdnld.html

(2) Install the Ghostscript and define the environment variable PATH.

(3) Then, use the below php code to change the PDF version.

shell_exec( "gswin32 -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dNOPAUSE -dQUIET -dBATCH -sOutputFile=".$new_pdf." ".$old_pdf.""); 
"F:\Program Files\gs\gs9.54.0\bin\gswin64" -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dNOPAUSE -dQUIET -dBATCH -sOutputFile="template2New.pdf" "template2.pdf"

(4) Now we get PDF with version 1.4 as output, then continue with FPDF.
 
 With PHP the same could be done with the following Code if 'exec' command is permitted.
	//The PDF version that you want to convert
	//the file into.
	$pdfVersion = "1.4";

	//The path that you want to save the new
	//file to
	$newFile = "/path/to/new.pdf";

	//The path of the file that you want
	//to convert
	$currentFile = "/path/to/old.pdf";

	//Create the GhostScript command
	$gsCmd = "gs -sDEVICE=pdfwrite -dCompatibilityLevel=$pdfVersion -dNOPAUSE -dBATCH -sOutputFile=$newFile $currentFile";

	//Run it using PHP's exec function.
	exec($gsCmd);
*/
error_reporting(0);
require_once "vendor/setasign/fpdf/fpdf.php";
require_once('vendor/autoload.php');
require_once "vendor/setasign/fpdi/fpdi.php";

//use setasign\Fpdi;
//ob_start();
$templatesDocsFolder = "docs/templates/";
$templatesDocName = "template__NO__.pdf";
$templatesDocPath = $templatesDocsFolder.$templatesDocName;


$generatedDocsFolder = "docs/generated/";
$generatedDocName = $templatesDocName.date('d-m-Y_his').".pdf";
$generatedDocPath = $generatedDocsFolder.$generatedDocName;


class PDF extends FPDI
{

function Header()
{
    // Positionnement à 1,5 cm du bas
    //$this->SetY(-15);
    // Police Arial italique 8
    /* $this->SetFont('Arial','I',24);
    // Numéro de page
    $this->Cell(0,10,'Devis from MyCompany - Page '.($this->PageNo()+3).'/{nb}'.'        Paraphes :',0,0,'C');
    $this->Cell(0,10,($this->PageNo()+3),0,0,'C'); */
	
		/* $this->SetFont('Helvetica');
		$this->SetTextColor(255, 0, 0);
		$this->SetXY(0,0);
		$this->Write(0, 'This is just a simple header'); */
}

function Footer()
{
}

}

// Instanciation de la classe dérivée



$pdf = new PDF();
$pdf->AliasNbPages();

//
    // Here is page 1, you don't need the details
//$pdf->AddPage();
    // Here is page 2, some other pages can come too


// Then begins the importation
for($templateNo = 1; $templateNo < 4 ; $templateNo++ )
{	
	// get the page count
	$pageCount = $pdf->setSourceFile(str_replace("__NO__",$templateNo,$templatesDocPath));
	// iterate through all pages
	for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
		// import a page
		$templateId = $pdf->importPage($pageNo);
		// get the size of the imported page
		$size = $pdf->getTemplateSize($templateId);

		// create a page (landscape or portrait depending on the imported page size)
		if ($size['w'] > $size['h']) {
			$pdf->AddPage('L', array($size['w'], $size['h']));
		} else {
			$pdf->AddPage('P', array($size['w'], $size['h']));
		}

		// use the imported page
		$pdf->useTemplate($templateId);
		
		    // now write some text above the imported page
			$pdf->SetFont('Helvetica');
			/*$pdf->SetTextColor(255, 0, 0);
			$pdf->SetXY(10,10);
			$pdf->SetXY(0,10);
			$pdf->Write(0, 'This is just a simple text in Page ' . ($pdf->PageNo()+3)); */
			/* $pdf->SetXY(0,0); */
			$pdf->SetFont('Arial','I',24);
			$pdf->Cell(0,0,($pdf->PageNo()),0,0,'C');
			$addPageNo = ($pageCount > 1)?".".$pageNo:"";
			$pdf->Cell(0,0,("P".$templateNo.$addPageNo),0,1,'R');
			//$pdf->Write(0,($this->PageNo()+3));
	}//for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
		
	//$pdf->AddPage();
}//for($templateNo = 1; $templateNo < 4 ; $templateNo++ )
ob_end_flush();
$pdf->Output(str_replace("__NO__",$templateNo,$templatesDocPath),'F');
$pdf->Output('devis.pdf','I');
?>