https://github.com/2lenet/pdfgeneratorbundle
https://github.com/2lenet/pdfgeneratorbundle
Last synced: 12 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/2lenet/pdfgeneratorbundle
- Owner: 2lenet
- License: mit
- Created: 2018-09-17T07:54:48.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2024-06-07T12:18:16.000Z (about 2 years ago)
- Last Synced: 2025-04-07T18:46:26.924Z (about 1 year ago)
- Language: PHP
- Size: 381 KB
- Stars: 3
- Watchers: 5
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PdfGeneratorBundle
## Installation
`composer require 2lenet/pdf-generator-bundle`
Require: unoserver (for word_to_pdf)
```docker-compose
unoserver:
image: registry.2le.net/2le/2le:unoserver
```
## Configuration
config (with default value):
```yaml
lle_pdf_generator:
path: "data/pdfmodel"
default_generator: "word_to_pdf"
class: 'Lle\PdfGeneratorBundle\Entity\PdfModel'
```
add routing (for show the ressource
use ``````)
```yaml
lle_pdf_generator:
resource: "@LlePdfGeneratorBundle/Resources/config/routes.yaml"
prefix: /
```
if you create an model without type and with ressource is mydoc.doc the generator will create an pdf based on
data/pdfmodel/mydoc.doc with word_to_pdf generator.
## Configure your tags
You can easily list the tags used in your models.
To do this, simply declare the route to the page where the tags will be listed. The name of the route is "
lle_pdf_generator_admin_balise".
Example in Crudit:
```php
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;
}
```
If you use several document templates linked to different modules, you can declare your annotations in the
pdf_generator.yaml
of your project:
```yaml
lle_pdf_generator:
path: "data/pdfmodel"
default_generator: "word_to_pdf"
data_models:
- facture
- commande
```
To complete the list of tags, use Symfony annotations declared in your pdf_generator.yaml file. For example:
```php
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');
}
```
You can create an instance of TcpdfFpdi (Tcpdf and Fpdi) with the PdfMerger
```php
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');
```
## Use with your entity:
change config "class" of pdf generator "App/Entity/MyModelPdf"
```php
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';
}
}
```
## Use the type tcpdf
An exemple for tcpdf:
```php
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 '. $this->data['name'] .'');
}
public function footer()
{
}
}
```
```php
findAll() as $user){
$data[] = ['name' => $user->getName()];
}
return $generator->generateByRessourceResponse(TcpdfGenerator::getName(), MyTcpdfClass::class, $data);
}
```
You can create an pdf model in bdd with ressource "App\Service\Pdf\MyTcpdfClass" and code MYTCPDF type "tcpdf"
```php
findAll() as $user){
$data[] = ['name' => $user->getName()];
}
return $generator->generateResponse('MYTCPDF', $data);
}
```
## use the word_to_pdf (format Microsoft Word XML)
Create a .docx file /data/pdfmodel/test.docx with Hello ${name}
```php
findAll() as $user){
$data[] = ['name' => $user->getName()];
}
return $generator->generateByRessourceResponse(WordToPdfGenerator::getName(), 'test.docx', $data);
}
```
You can create an pdf model in bdd with ressource "test.docx" and code MYDOC type "word_to_pdf"
```php
findAll() as $user){
$data[] = ['name' => $user->getName()];
}
return $generator->generateResponse('MYDOC', $data);
}
```
you can use variables ${@img[logo]:100x200} or ${@img[logo]} for create images. (regex is #^@img\[(\w+)\](:(\d+)x(
\d+))?$#)
```php
$generator->generateResponse('MYDOC', [['logo'=> 'logo.png']]);
```
search to {{lle_pdf_generator.path}}/logo.png so default is data/pdfmodel/logo.png
```php
$generator->generateResponse('MYDOC', [['logo'=> '/logo.png']]);
```
search to /logo.png
https://phpword.readthedocs.io/en/latest/templates-processing.html
##Sign pdf
Use the Lle\PdfGeneratorBundle\Lib\Signature class you can sign an pdf response or an PdfMerge
### Cretae the signature
```
openssl req -x509 -nodes -days 365000 -newkey rsa:1024 -keyout tcpdf.crt -out tcpdf.crt
openssl pkcs12 -export -in tcpdf.crt -out tcpdf.p12
```
```php
'name',
'Location' => 'location',
'Reason' => 'reason',
'ContactInfo' => 'url',
];
$signature = new Signature($generator->getPath().'cert/tcpdf.crt', $password, $info);
```
You can add an draw with signature
```php
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);
```
You can also add segment or points for create the signature picture
```php
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
```
### Pdf response
```php
generateByRessourceResponse(WordToPdfGenerator::getName(), 'test.docx', $data, [$signature]);
//or
return $generator->generateResponse('MYMODELCODE', $data, [$signature]);
```
### Pdf Merger
The pdfMerge is the class of instance return by generator
```php
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
```
You can't signed a pdfMerger you have to pass by TcpdfFpdi. An PdfMerger instance can never be signed
You can continue to sign an TcpdfFpdi with $generator->signeTcpdfFpdi($pdf, $signature)
You can also use directly the signature instance for sign a Pdfmerger or TcpdfFpdi
```php
generate('MYMODELCODE', $data);
$signature->signe($pdfMerger)->Output('My pdf', 'D');
```
or
```php
generate('MYMODELCODE', $data);
$pdf = $pdfMerger->toTcpdfFpdi();
$pdf = $signature->signeTcpdfFpdi($pdf);
$pdf = $signature2->signeTcpdfFpdi($pdf);
$pdf->Output('My pdf', 'D');
```
You can't use several sign with PdfMerger
## ieterable data
create a .docx file and create 2 table (1 line , 3 cells)
- first table cells 1 write ${eleves.nom} , cells 2 write ${eleves.etablissement.nom}, cells 3 write
${@img[eleves.logo]}
- second table cells 1 write ${users.[nom]}, cells 2 write ${users.[adresse][rue]}, cells 3 write ${@img[users.[logo]]}
save it with myiterable.docx
use the Lle\PdfGeneratorBundle\Lib\PdfIterable class
```php
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);
```
show it
Warning only the first level of data can to be an PdfIterable, you can't use ${etablissement.eleves}:
```php
$etablissement,
'eleves' => PdfIterable($etablissement->getEleves())
];
```
# Understand the property (word to pdf)
The property is read with propertyAccesor (Symfony) but the first is beetween two "[]": [first].rest
```
the vars ${eleve.etablissement.nom} -> $propertyAccess->getValue($params, '[eleve].etablissement.nom')
the vars ${eleve.etablissement[nom]} -> $propertyAccess->getValue($params, '[eleve].etablissement[nom]')
```
!!! warning use the same systeme if you create your own type !!!
## Merge several model
```php
generateByRessourceResponse(
TcpdfGenerator::getName(),
[MyTcpdfClass::class,AnotherTcpdfClass::class],
$data);
```
```php
generateByRessourceResponse(
[TcpdfGenerator::getName(),WortdToPdfGenerator::getName()],
[MyTcpdfClass::class,'mydoc.docx'],
$data);
```
in bdd:
```sql
INSERT INTO `lle_pdf_model` (`code`, `path`, `type`) VALUES
('RELANCE_1ANS', 'mydoc.docx,App\\Service\\Pdf\\LotInvitation', 'word_to_pdf,tcpdf')
```
The default type is always the first (here "word_to_pdf")
if none type is defined the type is lle_pdf_generator.default_generator config
## Migrate to pdf generator v3
There is few breaks in backward compatibility in v3. Here is the steps to migrate :
- `@LlePdfGeneratorBundle/Resources/routing/routes.yaml` is now in `@LlePdfGeneratorBundle/Resources/config/routes.yaml`
- The class `Lle\PdfGeneratorBundle\Entity\PdfModelCustomFileTrait` is replaced by `Lle\PdfGeneratorBundle\Entity\PdfModelTrait` and now the trait include the `$file` property
- The route `lle_pdf_generator_show_ressource` change to `lle_pdf_generator_show_model`
- The route `lle_pdf_generator_show_pdf` change to `lle_pdf_generator_download_model`