sensiolabs / gotenberg-bundle
为 Symfony 提供的 Gotenberg 支持
Requires
- php: >=8.1
- ext-json: *
- symfony/config: ^6.4 || ^7.0
- symfony/dependency-injection: ^6.4 || ^7.0
- symfony/filesystem: ^6.4 || ^7.0
- symfony/http-client: ^6.4 || ^7.0
- symfony/http-foundation: ^6.4 || ^7.0
- symfony/http-kernel: ^6.4 || ^7.0
- symfony/mime: ^6.4 || ^7.0
- symfony/string: ^6.4 || ^7.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.41
- phpstan/extension-installer: ^1.3
- phpstan/phpstan: ^1.10
- phpstan/phpstan-symfony: ^1.3
- phpunit/phpunit: ^10.4
- symfony/framework-bundle: ^6.4 || ^7.0
- symfony/monolog-bundle: ^3.10
- symfony/stopwatch: ^6.4 || ^7.0
- symfony/twig-bundle: ^6.4 || ^7.0
- symfony/var-dumper: ^6.4 || ^7.0
Suggests
- monolog/monolog: Enables logging througout the generating process.
- symfony/monolog-bundle: Enables logging througout the generating process.
- symfony/twig-bundle: Allows you to use Twig to render templates into PDF
README
警告
此包为实验性,未来版本可能有所变动。
这是什么?
此包允许您从 URL、HTML、Markdown 或任何 Office 文件本地生成、流式传输和保存 PDF。具体选项取决于源。
它还可以帮助您使用截图从 URL、HTML 和 Markdown 本地生成、流式传输和保存图片。
如何安装
使用 composer 安装此包
composer require sensiolabs/gotenberg-bundle
使用 Symfony Flex
如果在安装期间接受 Symfony Flex 配置,则包将被注册,创建配置模板文件,更新 .env 文件并添加 GOTENBERG_DSN,同时也会创建 Dockerfile 来获取 gotenberg 镜像,您需要配置 .env 文件中与 GOTENBERG_DSN 相关的端口。
主机地址也可能需要更新,因为默认为主机名。如果您的脚本在容器中运行,则主机为 gotenberg。
现在您可以根据需要调整配置文件。
不使用 Symfony Flex
注意
要使用此包,您首先需要安装和配置 Gotenberg 8.x。
通过将包添加到项目 config/bundles.php 文件中注册的包列表来启用该包
// config/bundles.php return [ // ... SensioLabs\GotenbergBundle\SensioLabsGotenbergBundle::class => ['all' => true], ];
基本用法
您可以从 URL、HTML、Markdown 或任何 Office 文件本地生成 PDF。
URL
注入 GotenbergPdfInterface 后,只需调用方法 url,这将返回一个 UrlPdfBuilder 实例。
UrlPdfBuilder 允许您将您想要转换为 PDF 的页面的 URL 传递给 url 方法。
namespace App\Controller; use Sensiolabs\GotenbergBundle\GotenbergPdfInterface; class YourController { public function yourControllerMethod(GotenbergPdfInterface $gotenberg): Response { return $gotenberg->url() ->url('https://sensiolabs.com/fr/') ->generate() ->stream() // will return directly a stream response ; } }
提示
有关更多信息,请参阅 Gotenberg 文档。
Twig
警告
传递给 Gotenberg 的每个 twig 模板都需要以下结构。
即使是页眉或页脚部分。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title>My PDF</title> </head> <body> <!-- Your code goes here --> </body> </html>
namespace App\Controller; use Sensiolabs\GotenbergBundle\GotenbergPdfInterface; class YourController { public function yourControllerMethod(GotenbergPdfInterface $gotenberg): Response { return $gotenberg->html() ->content('twig_simple_pdf.html.twig', [ 'my_var' => 'value' ]) ->generate() ->stream() // will return directly a stream response ; } }
如果模板需要链接到静态资源(例如图像),此包提供了一个 {{ gotenberg_asset() }} Twig 函数来生成正确的路径并将其自动添加到构建器中。
此函数类似于 asset() Twig 函数,并从您的应用的 assets 文件夹中获取资源。如果您的文件在其他文件夹中,您可以在配置文件 config/sensiolabs_gotenberg.yml 中覆盖 assets_directory 的默认值。提供的路径可以是相对路径,也可以是绝对路径。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title>PDF body</title> </head> <body> <main> <h1>Hello world!</h1> <img src="{{ gotenberg_asset('public/img/ceo.jpeg') }}" alt="CEO"/> <img src="{{ gotenberg_asset('public/img/admin.jpeg') }}" alt="Admin"/> </main> </body> </html>
提示
有关更多信息,请参阅 Gotenberg 文档。
截图
您可以从 URL、HTML 和 Markdown 本地生成截图。
URL
注入 GotenbergScreenshotInterface 后,只需调用方法 url,这将返回一个 UrlScreenshotBuilder 实例。
UrlScreenshotBuilder 允许您将您想要转换为截图的页面的 URL 传递给 url 方法。
namespace App\Controller; use Sensiolabs\GotenbergBundle\GotenbergScreenshotInterface; class YourController { public function yourControllerMethod(GotenbergScreenshotInterface $gotenberg): Response { return $gotenberg->url() ->url('https://sensiolabs.com/fr/') ->generate() ->stream() ; } }
Twig
注入 GotenbergScreenshotInterface 后,只需调用方法 html,这将返回一个 HtmlScreenshotBuilder 实例。
HtmlScreenshotBuilder 允许您将您想要转换为截图的页面的内容传递给 content 方法。
namespace App\Controller; use Sensiolabs\GotenbergBundle\GotenbergScreenshotInterface; class YourController { public function yourControllerMethod(GotenbergScreenshotInterface $gotenberg): Response { return $gotenberg->html() ->content('twig_simple_pdf.html.twig', [ 'my_var' => 'value' ]) ->generate() ->stream() ; } }
提示
有关更多信息,请参阅 Gotenberg 文档。
高级用法
- 添加页眉/页脚
- HTML构建器
- Markdown构建器
- URL构建器
- Office构建器(以下列出可用的转换扩展)
123,602,abw,bib,bmp,cdr,cgm,cmx,csv,cwk,dbf,dif,doc,docm,docx,dot,dotm,dotx,dxf,emf,eps,epub,fodg,fodp,fods,fodt,fopd,gif,htm,html,hwp,jpeg,jpg,key,ltx,lwp,mcw,met,mml,mw,numbers,odd,odg,odm,odp,ods,odt,otg,oth,otp,ots,ott,pages,pbm,pcd,pct,pcx,pdb,pdf,pgm,png,pot,potm,potx,ppm,pps,ppt,pptm,pptx,psd,psw,pub,pwp,pxl,ras,rtf,sda,sdc,sdd,sdp,sdw,sgl,slk,smf,stc,std,sti,stw,svg,svm,swf,sxc,sxd,sxg,sxi,sxm,sxw,tga,tif,tiff,txt,uof,uop,uos,uot,vdx,vor,vsd,vsdm,vsdx,wb2,wk1,wks,wmf,wpd,wpg,wps,xbm,xhtml,xls,xlsb,xlsm,xlsx,xlt,xltm,xltx,xlw,xml,xpm,zabw - 合并构建器
- 转换构建器
- PDF定制(除了LibreOffice和合并之外的所有构建器都适用)
截图
分析器
内置分析器面板,帮助您在开发过程中使用。
鸣谢
此包受到了Gotenberg PHP的启发。
许可
MIT许可(MIT):有关更多详细信息,请参阅许可文件。
常见问题解答(FAQ)
我的PDF/Screenshot为空白,但没有错误!
可能是因为Gotenberg正在尝试访问一个无效的URL(当使用`->url()`或`->route()`模式时)。例如,如果Gotenberg尝试访问`https://:8001`上的页面,但SSL是本地提供的,那么Chromium将无法授权访问该网站。要修复此问题,您可以按照以下方式更新您的Gotenberg docker服务:--- a/compose.yaml +++ b/compose.yaml @@ -1,6 +1,9 @@ services: gotenberg: image: 'gotenberg/gotenberg:8' + command: + - 'gotenberg' + - '--chromium-ignore-certificate-errors'
也可能是因为从Gotenberg的角度来看,您的Symfony应用的URL不可达。假设您正在使用symfony CLI以本地模式运行您的项目,同时Gotenberg在Docker中运行。您需要像这样配置request_context:
--- a/config/packages/gotenberg.yaml +++ b/config/packages/gotenberg.yaml @@ -6,5 +6,5 @@ framework: sensiolabs_gotenberg: http_client: 'gotenberg.client' + request_context: + base_uri: 'http://host.docker.internal:8000' # 8000 is the port Symfony CLI is running my app on.