PHP code example of 2lenet / pdf-generator-bundle

1. Go to this page and download the library: Download 2lenet/pdf-generator-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

2lenet / pdf-generator-bundle example snippets



public function getListActions(): array
    {
        $actions = parent::getListActions();

        array_unshift($actions, ListAction::new(
            "action.balise",
            Path::new('lle_pdf_generator_admin_balise'),
            Icon::new("bookmark")
        ));

        return $actions;
    }




namespace App\Entity

use Symfony\Component\Serializer\Annotation\Groups;

class Commande
{
    /**
     * @Groups({"commande"})
     */
    private $type;
}



namespace App\Entity

use Symfony\Component\Serializer\Annotation\Groups;

class Commande
{
    /**
     * @Groups({"pdfgenerator"})
     */
    private $type;
}


/**
 * @Route("/pdf")
 */
public function pdf(PdfGenerator $generator, UserRepository $userRepository)
{
    $data = [];
    foreach($userRepository->findAll() as $user){
        $data[] = ['name' => $user->getName()];
    }
    //create an response by Bdd
    return $generator->generateResponse('MYMODELCODE', $data);
    //or
    //create an PdfMerger by Bdd
    $generator->generate('MYMODELCODE', $data)->merge('pdf.pdf','F');
    //or
    //create an PdfMerger by ressource
    return $generator->generateByRessourceResponse(TcpdfGenerator::getName(), MyTcpdfClass::class, $data);
    //or
    //create an response by ressource
    $generator->generateByRessource(TcpdfGenerator::getName(), MyTcpdfClass::class, $data)->merge('pdf.pdf','F');
}


$pdfMerger = $generator->generate('MYMODELCODE', $data)->merge('pdf.pdf','F');
$pdf = $pdfMerger->toTcpdfFpdi();
$pdf->addPage('P');
$pdf->writeHTML('Hello', true, 0, true, 0);
$pdf->Output('file.pdf', 'F');
return new ResponseBinaryFile('file.pdf');




namespace App\Entity;

use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Doctrine\ORM\Mapping as ORM;
use Lle\PdfGeneratorBundle\Entity as PDF;

/**
 *
 * @ORM\Table(name="lle_pdf_model", indexes={@ORM\Index(name="code_idx", columns={"code"})})
 * @ORM\Entity
 * @Vich\Uploadable
 */
class MyModelPdf implements PDF\PdfModelInterface
{
    use PDF\PdfModelTrait;
}

    public static function getName():string; //name of type
    public function generate(string $source, iterable $params, string $savePath):void; //generate the pdf with the ressource $source and parameters $params in a tmp file $savePath
    public function getRessource(string $pdfPath, string $modelRessource): string; //calcule the ressource with pdfPath or not (the ressource can be an class name for exemple)


class TcpdfGenerator extends AbstractPdfGenerator
{

    private $pdfPath;
    
    public function generate(string $source, iterable $params, string $savePath):void{
        $reflex = new \ReflectionClass($source);
        $pdf = $reflex->newInstance();
        if ($pdf instanceof Pdf) {
            $pdf->setRootPath($this->pdfPath);
            $pdf->setData($params['vars']);
            $pdf->initiate();
            $pdf->generate();
            $pdf->setTitle($pdf->title());
        } else {
            throw new \Exception('PDF GENERATOR ERROR: ressource '.$source.' n\'est pas une class PDF');
        }
        $pdf->output($savePath, 'F');
    }

    //$pdfPath is in config lle_pdf_generator.path (default:data/pdfmodel)
    public function getRessource(string $pdfPath, string $modelRessource): string{
        $this->pdfPath = $pdfPath;
        return $modelRessource;
    }

    public static function getName(): string{
        return 'tcpdf';
    }
}



namespace App\Service\Pdf;

use Lle\PdfGeneratorBundle\Lib\Pdf;

class MyTcpdfClass extends Pdf
{

    //$this->rootPath is in config lle_pdf_generator.path (default:data/pdfmodel)
    public function init()
    {
        $this->setSourceFile($this->rootPath . 'background.pdf');
    }
    
    public function myColors()
    {
        return ['blanc' => 'FFFFFF','default'=> '000000', 'red' => 'FF0000'];
    }

    //the fonts is in $this->rootPath.'/fonts'
    public function myFonts()
    {
        return ['titre' => ['size'=>12,'color'=>'noir','family'=>'courier', 'style'=>'BU']];
    }

    public function generate()
    {
        $this->AddPage('P');
        $this->showGrid(5); //is an debug function which show an gride by 5px
        $this->changeFont('titre');
        $this->w(10,10,'Hello <b>'. $this->data['name'] .'</b>');
    }

    public function footer()
    {
    }
}


/**
 * @Route("/pdf")
 */
public function pdf(PdfGenerator $generator, UserRepository $userRepository)
{
    $data = [];
    foreach($userRepository->findAll() as $user){
        $data[] = ['name' => $user->getName()];
    }
    return $generator->generateByRessourceResponse(TcpdfGenerator::getName(), MyTcpdfClass::class, $data);
}


/**
 * @Route("/pdf")
 */
public function pdf(PdfGenerator $generator, UserRepository $userRepository)
{
    $data = [];
    foreach($userRepository->findAll() as $user){
        $data[] = ['name' => $user->getName()];
    }
    return $generator->generateResponse('MYTCPDF', $data);
}


/**
 * @Route("/pdf")
 */
public function pdf(PdfGenerator $generator, UserRepository $userRepository)
{
    $data = [];
    foreach($userRepository->findAll() as $user){
        $data[] = ['name' => $user->getName()];
    }
    return $generator->generateByRessourceResponse(WordToPdfGenerator::getName(), 'test.docx', $data);
}




/**
 * @Route("/pdf")
 */
public function pdf(PdfGenerator $generator, UserRepository $userRepository)
{
    $data = [];
    foreach($userRepository->findAll() as $user){
        $data[] = ['name' => $user->getName()];
    }
    return $generator->generateResponse('MYDOC', $data);
}

$generator->generateResponse('MYDOC', [['logo'=> 'logo.png']]);

$generator->generateResponse('MYDOC', [['logo'=> '/logo.png']]);


$password = '***';
$info = [
    'Name' => 'name',
    'Location' => 'location',
    'Reason' => 'reason',
    'ContactInfo' => 'url',
];
$signature = new Signature($generator->getPath().'cert/tcpdf.crt', $password, $info);


/*...*/
$picture = 'signature.png';
$signature = new Signature($generator->getPath().'cert/tcpdf.crt', $password, $info, $pictur);
//or
$pos = [
    'w' => 40, //width default 40
    'h' => 20, //heght default 20
    'x' => 10, //x default pageWidth - w
    'y' => 10, //y default pageHeight - (h*2+5)
    'p' => 1 //page default last page
];
$signature = new Signature($generator->getPath().'cert/tcpdf.crt', $password, $info, $pictur, $pos);


$signature = new Signature($certif, $password, $info);

$signature->setSegments([[$x1,$y1],[$x2,$y2]], $pos);
//or
$signature->setPoints([$x1,$y1,$x2,$y2], $pos);
//or
$signature->setImage('signe.png', $pos);

$signature->setPosition($pos); // you can use it also


return $generator->generateByRessourceResponse(WordToPdfGenerator::getName(), 'test.docx', $data, [$signature]);
//or
return $generator->generateResponse('MYMODELCODE', $data, [$signature]);


$pdfMerger = $generator->generateByRessource(WordToPdfGenerator::getName(), 'test.docx', $data);
//or
$pdfMerger = $generator->generate('MYMODELCODE', $data);
$pdf = $generator->signes($pdfMerger, [$signature]); //return an TcpdfFpdi (signe($pdfMerger, $signature) exist also)
$pdf->Output('My pdf', 'D'); // return a signed pdf
$pdfMerger->merge('My pdf', 'D'); // return a unsigned pdf


$pdfMerger = $generator->generate('MYMODELCODE', $data);
$signature->signe($pdfMerger)->Output('My pdf', 'D');


$pdfMerger = $generator->generate('MYMODELCODE', $data);
$pdf = $pdfMerger->toTcpdfFpdi();
$pdf = $signature->signeTcpdfFpdi($pdf);
$pdf = $signature2->signeTcpdfFpdi($pdf);
$pdf->Output('My pdf', 'D');


$data = [
    'eleves' => new PdfIterable($this->em->getRepository(Eleve::class)->findAll()),
    'users' => new PdfIterable([['nom'=>'saenger','adresse'=>['rue'=>'rue du chat'], 'logo'=>'logo.png'], ['nom'=>'boehler', 'adresse'=>['rue'=>'rue du chien'], 'logo.png']]),            
];
return $generator->generateByRessourceResponse(WordToPdfGenerator::getName(), 'myiterable.docx', $data);


$data = [
    'etablissement' => $etablissement,
    'eleves' => PdfIterable($etablissement->getEleves())
];


return $generator->generateByRessourceResponse(
    TcpdfGenerator::getName(), 
    [MyTcpdfClass::class,AnotherTcpdfClass::class], 
    $data);


return $generator->generateByRessourceResponse(
    [TcpdfGenerator::getName(),WortdToPdfGenerator::getName()], 
    [MyTcpdfClass::class,'mydoc.docx'], 
    $data);