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');
}
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);
}
$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