kennethormandy/craft-api2pdf

使用api2pdf.com生成PDF

1.0.0-beta.1 2023-01-12 20:13 UTC

This package is auto-updated.

Last update: 2024-09-13 00:07:10 UTC


README

Craft Api2Pdf icon

Api2Pdf插件用于Craft CMS

使用Api2Pdf.com轻松生成PDF

要求

此插件需要Craft CMS 3.1.x或更高版本,或Craft 4.0.0或更高版本。

安装

此插件Craft 4版本的当前标记为测试版发布。您可以使用以下方式安装:

composer require kennethormandy/craft-api2pdf:^1.0.0-beta.1

否则,像平常一样使用Composer要求插件

composer require kennethormandy/craft-api2pdf

然后,在Craft CMS控制面板中,转到设置→插件,点击Craft Api2Pdf的“安装”按钮。或者运行

./craft install/plugin api2pdf

设置

要运行此插件,所需的唯一设置是来自Api2Pdf的API密钥,它(并且可能应该)设置为环境变量

Craft Api2Pdf settings panel in the Craft CMS admin area

动作

Twig

选项

  • filename
  • redirect

所有针对无头Chrome的高级选项也都支持。

示例

操作 generate-from-url

获取Api2Pdf的JSON响应

<form method="post" action="" accept-charset="UTF-8">
  <input
    type="hidden"
    name="action"
    value="api2pdf/pdf/generate-from-url"
  />
  <input type="hidden" name="url" value="https://example.com" />
  {{ csrfInput() }}
  <input type="submit" value="Generate PDF from URL" />
</form>

直接重定向到PDF URL

<form method="post" action="" accept-charset="UTF-8">
  
  <input
    type="hidden"
    name="action"
    value="api2pdf/pdf/generate-from-url"
  />

  <!-- Set the URL (your site or any public URL) to use -->
  <input type="hidden" name="url" value="https://example.com" />

  <!-- Redirect to the PDF URL -->
  <input type="hidden" name="options[redirect]" value="1" />

  {{ csrfInput() }}
  <input type="submit" value="Generate PDF from URL with a redirect" />
</form>

操作 generate-from-html

直接重定向到使用HTML字符串制作的PDF

<form method="post" action="" accept-charset="UTF-8">

  <input
    type="hidden"
    name="action"
    value="api2pdf/pdf/generate-from-html"
  />

  <!-- Set the custom HTML string -->
  <input type="hidden" name="html" value="<p>Hello from action with HTML</p>" />

  <!-- Redirect to the PDF URL -->
  <input type="hidden" name="options[redirect]" value="1" />

  {{ csrfInput() }}
  <input type="submit" value="Generate with redirect from HTML" />
</form>

提供可编辑的文件名

<form method="post" action="" accept-charset="UTF-8">

  <input
    type="hidden"
    name="action"
    value="api2pdf/pdf/generate-from-html"
  />

  <!-- Set the custom HTML string -->
  <input type="hidden" name="html" value="<p>Hello</p>" />

  <!-- Set the filename to the value of the form input -->
  <input type="text" name="options[filename]" value="test.pdf" />

  {{ csrfInput() }}
  <input type="submit" value="Generate from HTML" />
</form>

操作 merge

将两个托管PDF(由此插件生成或其他方式),合并成一个

传递给此函数的这些URL需要是托管PDF。无论它们是否以前通过此插件生成,或者是否在您的服务器上,只要它们可以通过URL访问即可。

{% set urls = [
  'https://example.com/one.pdf',
  'https://example.com/two.pdf'
] %}

<form method="post" action="" accept-charset="UTF-8">
  <input
    type="hidden"
    name="action"
    value="api2pdf/pdf/merge"
  />
  {% for url in urls %}
    <input type="hidden" name="urls[]" value="{{ url }}" />
  {% endfor %}
  {{ csrfInput() }}
  <input class="btn" type="submit" value="Merge PDFs" />
</form>

Twig generateFromUrl 函数

{% set result = craft.api2pdf.generateFromUrl('https://example.com') %}

{% if result and result.success %}
  {{ result.pdf }}
{% endif %}

Twig generateFromHtml 函数

{% set result = craft.api2pdf.generateFromHtml('<h1>Hello</h1>') %}

{% if result and result.success %}
  {{ result.pdf }}
{% endif %}

更详细的示例

{% set options = {
  redirect: true,
  filename: "test.pdf"
} %}
{% set result = craft.api2pdf.generateFromHtml('<h1>Hello</h1>', options) %}

{% if result and result.success %}

  {# Display the URL #}
  <p>{{ result.pdf }}</p>

  {# The other pieces of metadata available #}
  <ul>
    <li>{{ result.mbIn|round }}mb</li>
    <li>{{ result.mbOut|round }}mb</li>
    <li>US${{ result.cost|round }}</li>
    <li>{{ result.responseId }}</li>
  </ul>
  
{% else %}
  <p>{{ result.error }}</p>
{% endif %}

Twig merge 函数

{% set urls = [
  'https://example.com/one.pdf',
  'https://example.com/two.pdf'
] %}
{% set result = craft.api2pdf.merge(urls) %}

{% if result and result.success %}
  {{ result.pdf }}
{% else %}
  {{ result.error }}
{% endif %}

注意

  • 此插件是为Api2Pdf API的第1版构建的,但可能当它退出测试版时,会添加对v2端点的支持,Api2Pdf客户端库也会更新
  • 此插件仅支持用于PDF生成的无头Chrome。如果您有兴趣添加对另一个端点的支持,我愿意讨论拉取请求(但请注意,v2 API也仅支持无头Chrome)。

许可证

MIT许可证(MIT)

版权所有 © 2019–2020 Kenneth Ormandy Inc.