sensiolabs/gotenberg-bundle

为 Symfony 提供的 Gotenberg 支持

安装: 791

依赖项: 0

建议者: 0

安全: 0

星标: 21

关注者: 6

分支: 7

开放问题: 14

类型:symfony-bundle

v0.1.0 2024-06-21 21:06 UTC

This package is auto-updated.

Last update: 2024-09-19 14:36:42 UTC


README

Latest Version Total Downloads Monthly Downloads Software License Static analysis Tests

警告

此包为实验性,未来版本可能有所变动。

这是什么?

此包允许您从 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],
];

基本用法

PDF

您可以从 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 文档

高级用法

  1. 配置
  2. 与资源协同工作
  3. 构建器API

PDF

  1. 添加页眉/页脚
  2. HTML构建器
  3. Markdown构建器
  4. URL构建器
  5. Office构建器(以下列出可用的转换扩展)
    123602abwbibbmpcdrcgmcmxcsvcwkdbfdifdocdocmdocxdotdotmdotxdxfemfepsepubfodgfodpfodsfodtfopdgifhtmhtmlhwpjpegjpgkeyltxlwpmcwmetmmlmwnumbersoddodgodmodpodsodtotgothotpotsottpagespbmpcdpctpcxpdbpdfpgmpngpotpotmpotxppmppspptpptmpptxpsdpswpubpwppxlrasrtfsdasdcsddsdpsdwsglslksmfstcstdstistwsvgsvmswfsxcsxdsxgsxisxmsxwtgatiftifftxtuofuopuosuotvdxvorvsdvsdmvsdxwb2wk1wkswmfwpdwpgwpsxbmxhtmlxlsxlsbxlsmxlsxxltxltmxltxxlwxmlxpmzabw
  6. 合并构建器
  7. 转换构建器
  8. PDF定制(除了LibreOffice和合并之外的所有构建器都适用)

截图

  1. HTML构建器
  2. Markdown构建器
  3. URL构建器
  4. 截图定制

分析器

内置分析器面板,帮助您在开发过程中使用。

鸣谢

此包受到了Gotenberg PHP的启发。

许可

MIT许可(MIT):有关更多详细信息,请参阅许可文件

常见问题解答(FAQ)

我的PDF/Screenshot为空白,但没有错误!可能是因为Gotenberg正在尝试访问一个无效的URL(当使用`->url()`或`->route()`模式时)。例如,如果Gotenberg尝试访问`https://localhost: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.