springworks / pdf-maker
使用api2pdf v2创建PDF
Requires
- php: ^8.2
- api2pdf/api2pdf.php: 2.0.2
- craftcms/cms: ^5.0.0
Requires (Dev)
- craftcms/phpstan: dev-main
- craftcms/rector: dev-main
README
使用来自 api2pdf.com 的 v2 API 无头chrome服务创建PDF。您还可以在 Formie 或 Craft Commerce 中使用 Api2Pdf 渲染PDF。
要求
此插件需要Craft CMS 3.0.0-beta.23或更高版本。
Formie集成需要Formie 1.5或更高版本。
Commerce集成需要Commerce 2.0或更高版本。
安装
要安装此插件,请按照以下说明操作。
-
打开您的终端并转到您的Craft项目
cd /path/to/project
-
然后告诉Composer加载插件
composer require springworks/pdf-maker
-
在控制面板中,转到设置 → 插件,并点击Pdf Maker的“安装”按钮。
设置
唯一必需的设置是有效的Api2Pdf API密钥。建议使用 环境变量 设置此密钥。
插件将自动检测是否安装并启用了Formie或Craft Commerce,并在检测到时会提供额外的设置以覆盖这些插件的PDF生成。
默认PDF创建选项在配置文件中设置。将 config.php
重命名为 pdf-maker.php
并将其放置在您的 config
文件夹中。根据需要更改默认选项。
<?php return [ 'options' => [ 'pdf' => [ "landscape" => false, // Set to `true` for landscape PDFs "width" => "8.27in", // width of the page in inches "height" => "11.69in", // height of the page in inches "marginTop" => ".4in", // top margin of the page in inches "marginBottom" => ".4in", // bottom margin of the page in inches "marginLeft" => ".4in", // left margin of the page in inches "marginRight" => ".4in", // right margin of the page in inches ], 'image' => [ "fullPage" => true, "viewPortOptions" => [ "width" => 1920, // Viewport width in pixels "height" => 1080 // Viewport height in pixels ] ] ] ];
有关可用的选项的详细信息,请参阅以下内容
- PDF: https://app.swaggerhub.com/apis-docs/api2pdf/api2pdf/2.0.0#/ChromeAdvancedPdfOptions
- 图像: https://app.swaggerhub.com/apis-docs/api2pdf/api2pdf/2.0.0#/ChromeAdvancedImageOptions
示例
对于以下任何方法,您都可以通过在表单中作为隐藏输入传递一个 options
数组来覆盖默认配置选项,例如
<input type="hidden" name="options[landscape]" value="1" /> <input type="hidden" name="options[width]" value="8.5in" /> <input type="hidden" name="options[height]" value="11in" />
从URL生成PDF
<form method="post" action="" accept-charset="UTF-8"> {{ csrfInput() }} <input type="hidden" name="action" value="pdf-maker/pdf/pdf-from-url"> <input type="hidden" name="url" value="https://example.com" /> {# Set the filename (optional) #} <input type="hidden" name="filename" value="test.pdf" /> {# Redirect to the PDF URL (optional) #} <input type="hidden" name="redirect" value="1" /> <input type="submit" value="Create PDF" /> </form>
从HTML生成PDF
<form method="post" action="" accept-charset="UTF-8"> {{ csrfInput() }} <input type="hidden" name="action" value="pdf-maker/pdf/pdf-from-html"> <input type="hidden" name="html" value="<p>HTML content for PDF</p>" /> {# Set the filename (optional) #} <input type="hidden" name="filename" value="test.pdf" /> {# Redirect to the PDF URL (optional) #} <input type="hidden" name="redirect" value="1" /> <input type="submit" value="Create PDF" /> </form>
从模板生成PDF
要从模板生成PDF,请将隐藏的 template
输入设置为渲染路径的散列值。路径被散列以防止在浏览器中被篡改。例如,如果您想使用一个名为 page
的模板,该模板位于 templates
文件夹中的 _pdfs
文件夹中,则您将隐藏的 template
的值设置为 {{ '_pdfs/page'|hash }}
。
您可以通过将它们作为隐藏的 variables[variableName]
输入设置变量来将变量传递到模板中。这些值也必须被散列。因此,例如,如果您的模板需要一个 entryId
来告诉它渲染哪个条目,您可以像这样传递它
<input type="hidden" name="variables[entryId]" value="{{ entry.id|hash }}" />
完整示例
<form method="post" action="" accept-charset="UTF-8"> {{ csrfInput() }} <input type="hidden" name="action" value="pdf-maker/pdf/pdf-from-template"> <input type="hidden" name="template" value="{{ 'path/to/template'|hash }}" /> <input type="hidden" name="variables[entryId]" value="{{ entry.id|hash }}" /> <input type="hidden" name="variables[someVariable]" value="{{ 'value'|hash }}" /> {# Set the filename (optional) #} <input type="hidden" name="filename" value="test.pdf" /> {# Redirect to the PDF URL (optional) #} <input type="hidden" name="redirect" value="1" /> <input type="submit" value="Create PDF" /> </form>
合并PDF
{% set urls = [ 'https://example.com/one.pdf', 'https://example.com/two.pdf' ] %} <form method="post" action="" accept-charset="UTF-8"> {{ csrfInput() }} <input type="hidden" name="action" value="pdf-maker/pdf/merge"> {% for url in urls %} <input type="hidden" name="urls[]" value="{{ url }}" /> {% endfor %} {# Set the filename (optional) #} <input type="hidden" name="filename" value="test.pdf" /> {# Redirect to the PDF URL (optional) #} <input type="hidden" name="redirect" value="1" /> <input type="submit" value="Create PDF" /> </form>
从URL生成图像
<form method="post" action="" accept-charset="UTF-8"> {{ csrfInput() }} <input type="hidden" name="action" value="pdf-maker/pdf/image-from-url"> <input type="hidden" name="url" value="https://example.com" /> {# Set the filename (optional) #} <input type="hidden" name="filename" value="test.png" /> {# Redirect to the PDF URL (optional) #} <input type="hidden" name="redirect" value="1" /> <input type="submit" value="Create image" /> </form>
从HTML生成图像
<form method="post" action="" accept-charset="UTF-8"> {{ csrfInput() }} <input type="hidden" name="action" value="pdf-maker/pdf/image-from-html"> <input type="hidden" name="html" value="<p>HTML content for image</p>" /> {# Set the filename (optional) #} <input type="hidden" name="filename" value="test.png" /> {# Redirect to the PDF URL (optional) #} <input type="hidden" name="redirect" value="1" /> <input type="submit" value="Create image" /> </form>
从模板生成图像
要从模板生成图片,请将隐藏的 template
输入设置为要渲染的模板路径的哈希值。路径被哈希处理以防止在浏览器中被篡改。例如,如果您想使用名为 page
的模板,该模板位于您的 templates
文件夹中的 _pdfs
文件夹内,您应将隐藏的 template
的值设置为 {{ '_pdfs/page'|hash }}
。
您可以通过将它们作为隐藏的 variables[variableName]
输入设置变量来将变量传递到模板中。这些值也必须被散列。因此,例如,如果您的模板需要一个 entryId
来告诉它渲染哪个条目,您可以像这样传递它
<input type="hidden" name="variables[entryId]" value="{{ entry.id|hash }}" />
完整示例
<form method="post" action="" accept-charset="UTF-8"> {{ csrfInput() }} <input type="hidden" name="action" value="pdf-maker/pdf/image-from-template"> <input type="hidden" name="template" value="{{ 'path/to/template'|hash }}" /> <input type="hidden" name="variables[entryId]" value="{{ entry.id|hash }}" /> <input type="hidden" name="variables[someVariable]" value="{{ 'value'|hash }}" /> {# Set the filename (optional) #} <input type="hidden" name="filename" value="test.png" /> {# Redirect to the PDF URL (optional) #} <input type="hidden" name="redirect" value="1" /> <input type="submit" value="Create image" /> </form>
由 Steve Rowling 提供