silvioiannone/electron-pdf-php

dev-master 2019-01-23 09:48 UTC

This package is auto-updated.

Last update: 2024-09-06 21:31:44 UTC


README

electron-pdf-phpelectron-pdf的PHP包装器。它简单地提供了一个PHP接口来使用electron-pdf命令行工具。

要求

  • Linux或MacOS
  • Electron PDF

安装

首先全局安装electron-pdf

npm install -g electron-pdf

使用composer安装electron-pdf-php

composer require silvioiannone/electron-pdf-php

使用方法

使用方法非常简单

$generator = new SI\ElectronPdfPhp\Generator();
$pdf = $generator->fromHtml($html)
    ->content();

设置

在实例化新的生成器实例时,可以以下设置传递给构造函数

  • executable => string - electron-pdf可执行文件的路径。默认: 'electron-pdf'
  • proxyWithNode => bool - 使用node执行命令。在某些情况下可能需要这样做,例如抛出错误env: node: command not found。默认: false
  • graphicalEnvironment => bool - 服务器是否有图形环境。这仅在安装了Xvfb的Linux机器上有效。默认: false
  • marginsType => int - 指定要使用的页边距类型
    • 0: 默认页边距
    • 1: 没有页边距(electron-pdf默认设置)
    • 2: 最小页边距

示例

$generator = new SI\ElectronPdfPhp\Generator([
    'executable' => '/usr/local/bin/electron-pdf'
    ...
]);

指定源

从URL生成PDF

// Generate the PDF from a URL
$pdf = $generator->from('http://google.com')
    ->content();

从HTML生成PDF

// Generate the PDF from HTML
$pdf = $generator->fromHtml('<h1>Test<h1>')
    ->content();

指定目标

将PDF保存到文件

$generator->from('http://google.com')
    ->to('/home/downloads/test.pdf');

获取PDF内容

$pdf = $generator->from('http://google.com')
    ->content();