承接 atwx/silverstripe-headless-pdf 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

atwx/silverstripe-headless-pdf

Composer 安装命令:

composer require atwx/silverstripe-headless-pdf

包简介

Headless Chrome PDF generation for SilverStripe

README 文档

README

A Silverstripe module for printing a given html-template into a pdf file using headless chromium in php. This module enables the creation of zugferd compatible e-invoices. For this the additional module horstoeko/zugferd is required.

Installation

This module can be installed via Composer with

composer require atwx/silverstripe-headless-pdf

Requirements

This module requires the following modules

php: "^8.3",
silverstripe/framework: "^6.0",
chrome-php/chrome: "^1.15"

Configuration

The browser-factory will search for the environment variable CHROME_PATH when starting. To prevent it from guessing, this variable should be set in your .env (configuration instruction from chrome-php/chrome).

If the browser has trouble rendering the PDF (mostly timeouts), you can try different navigation wait events. This can be set in the WAITING_MODE variable in the .env. All available events can be found here. For each constant the string version has to be used. In this module the default value is 'networkIdle'.

This pdf module uses hashes created with the sha256 algorithm for protecting the html. Therefore you need to set the HEADLESS_PDF_HASH_KEY variable in your .env otherwise the pdf creation return an 403 http error. Any other authentication method has to be disabled for the HeadlessPDFController->renderPdfTemplate function.

Additionally if you are using ddev you can set the IS_DDEV variable in your .env to "true". By doing this sandboxing is disabled and certificate errors are ignored allowing printing PDFs locally. To use headless chrome in ddev you need the headless chrome add-on by gebruederheitz.

Usage

To print a pdf you need to create a new instance of HeadlessPDFService and call its generatePdf function. This function takes up to 5 arguments:

  • a link for rendering the html-template which will then be printed as a pdf,
  • a string containing the filename,
  • a bool getPath for getting the pdf as a string (default) or as a path to a temporary file,
  • optional xml-data for creating zugferd compatible e-invoices (this requires additionally the horstoeko/zugferd module),
  • and an optional array for php-chrome options changing the default pdf-options defined in the HeadlessPDFService.

The link given as first argument needs to point to

/pdf/renderPdfTemplate/<optional ID>

calling the HeadlessPDFController with the following get-parameters:

  • template containing the full template name/path (called with renderWith($template)),
  • hash generated with HeadlessPDFController::generateHash,
  • optional className to use an object in the template corresponding to $ID in the link. This object is callable as $TemplateObject in the template,
  • optional controller to use an iterable list of dataobjects in the template callable as $TemplateDatalist. This list can be defined in a getPdfDatalist function in the given controller,
  • optional variation containing a string callable as Variation in the template to enable similar pdf versions without the need to create a new template.

For the correct rendering of the pdf received as a string you need to set the two HTTP-headers

$this->getResponse()->addHeader('Content-Type', 'application/pdf')
$this->getResponse()->addHeader('Content-Disposition', 'inline; filename="' . <filename> . '.pdf"')

统计信息

  • 总下载量: 10
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 8
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2026-05-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固