2lenet/pdf-generator-bundle
Composer 安装命令:
composer require 2lenet/pdf-generator-bundle
包简介
Pdf generator
关键字:
README 文档
README
Installation
composer require 2lenet/pdf-generator-bundle
Require: unoserver (for word_to_pdf)
unoserver:
image: registry.2le.net/2le/2le:unoserver
Configuration
config (with default value):
lle_pdf_generator: path: "data/pdfmodel" default_generator: "word_to_pdf" class: 'Lle\PdfGeneratorBundle\Entity\PdfModel'
add routing (for show the ressource
use <a href="{{ path('lle_pdf_generator_show_ressource', {'id': item.id}) }}">)
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:
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:
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 namespace App\Entity use Symfony\Component\Serializer\Annotation\Groups; class Commande { /** * @Groups({"commande"}) */ private $type; }
If you only have on template or one module using several templates, you do not have to declare "data_models" in the pdf_generator.yaml file. By default, the Symfony annotation will be "pdfgenerator". In your entity, you will then have:
<?php namespace App\Entity use Symfony\Component\Serializer\Annotation\Groups; class Commande { /** * @Groups({"pdfgenerator"}) */ private $type; }
Use it
You can use the PDFgenerator with bdd or directly in code
<?php /** * @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'); }
You can create an instance of TcpdfFpdi (Tcpdf and Fpdi) with the PdfMerger
<?php $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');
Use with your entity:
change config "class" of pdf generator "App/Entity/MyModelPdf"
<?php 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; }
Use with bdd
The model is a PdfModel with
a code, a ressource, a libelle, a type and a description
you can create a model with
php bin/console lle:pdf-generator:create-model
!! Warning if you use your own class and this class has other field with constraint the command not work. !!
Create your own type
you can create several type of pdf (already exist tcpdf and word_to_pdf)
- word_to_pdf (the ressource is an path of .docx format Microsoft Word XML)
- tcpdf (the ressource is an class which extend Lle\PdfGeneratorBundle\Lib\Pdf (Tcpdf and Fpdi))
You can create your own type with an class which extend Lle\PdfGeneratorBundle\Generator\AbstractPdfGenerator
AbstractPdfGenerator implements Lle\PdfGeneratorBundle\Generator\PdfGeneratorInterface (autotagged lle.pdf.generator)
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)
an exemple is:
<?php 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'; } }
Use the type tcpdf
An exemple for tcpdf:
<?php 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() { } }
<?php /** * @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); }
You can create an pdf model in bdd with ressource "App\Service\Pdf\MyTcpdfClass" and code MYTCPDF type "tcpdf"
<?php /** * @Route("/pdf") */ public function pdf(PdfGenerator $generator, UserRepository $userRepository) { $data = []; foreach($userRepository->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 /** * @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); }
You can create an pdf model in bdd with ressource "test.docx" and code MYDOC type "word_to_pdf"
<?php /** * @Route("/pdf") */ public function pdf(PdfGenerator $generator, UserRepository $userRepository) { $data = []; foreach($userRepository->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+))?$#)
$generator->generateResponse('MYDOC', [['logo'=> 'logo.png']]);
search to {{lle_pdf_generator.path}}/logo.png so default is data/pdfmodel/logo.png
$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 $password = '***'; $info = [ 'Name' => '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 /*...*/ $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);
You can also add segment or points for create the signature picture
<?php $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
Pdf response
<?php return $generator->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 $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
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 $pdfMerger = $generator->generate('MYMODELCODE', $data); $signature->signe($pdfMerger)->Output('My pdf', 'D');
or
<?php $pdfMerger = $generator->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 $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);
show it
Warning only the first level of data can to be an PdfIterable, you can't use ${etablissement.eleves}:
<?php $data = [ 'etablissement' => $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 return $generator->generateByRessourceResponse( TcpdfGenerator::getName(), [MyTcpdfClass::class,AnotherTcpdfClass::class], $data);
<?php return $generator->generateByRessourceResponse( [TcpdfGenerator::getName(),WortdToPdfGenerator::getName()], [MyTcpdfClass::class,'mydoc.docx'], $data);
in bdd:
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.yamlis now in@LlePdfGeneratorBundle/Resources/config/routes.yaml- The class
Lle\PdfGeneratorBundle\Entity\PdfModelCustomFileTraitis replaced byLle\PdfGeneratorBundle\Entity\PdfModelTraitand now the trait include the$fileproperty - The route
lle_pdf_generator_show_ressourcechange tolle_pdf_generator_show_model - The route
lle_pdf_generator_show_pdfchange tolle_pdf_generator_download_model
2lenet/pdf-generator-bundle 适用场景与选型建议
2lenet/pdf-generator-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 19.45k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2019 年 02 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「pdf」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 2lenet/pdf-generator-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 2lenet/pdf-generator-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 2lenet/pdf-generator-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Provides TCPDF integration for Symfony
A adapter for the mPDF print library
A PDF builder using HTML or DOCX templates.
A PHP library to convert MIDI files to MusicXML, and render them as PDF or SVG.
Representacion Impresa
Turn any PDF into a Pingen-ready A4 letter (generated address cover page + A4 normalisation + safe margins) and send it through the Pingen print & mail API. Laravel-first.
统计信息
- 总下载量: 19.45k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-02-15