devture / browserless
用于与 Browserless.io API 通信的库(生成 PDF 等)
1.1.0
2024-01-10 18:13 UTC
Requires
- php: >=8.0
- guzzlehttp/guzzle: >=6.3,<8.0
- symfony/uid: >=6.4.0,<8.0-dev
README
这是一个与 Browserless.io API 交互的库。
目前,此库仅支持以下 API
-
/pdf - 从 URL 或内联 HTML 生成 PDF(类似于 wkhtmltopdf,但更好 -- 更新的浏览器引擎等)
-
/workspace - 将(HTML 和其他)文件持久保存到 Browserless 工作区
先决条件
您需要使用自己的自托管 Browserless 实例(请参阅如何使用 Docker)或他们的托管服务(请参阅 定价)。
您可以使用以下 docker-compose.yml
设置
version: '2.1' services: browserless: # Using a tag other than latest is recommended image: docker.io/browserless/chrome:latest restart: unless-stopped # Matches the owner (`blessuser:blessuser`) of `/usr/src/app` user: 999:999 environment: MAX_CONCURRENT_SESSIONS: 10 WORKSPACE_DIR: "/workspace" WORKSPACE_DELETE_EXPIRED: "true" # To render PDFs from HTML via a `file://` protocol (using `createPdfFromHtmlRequestUsingFileProtocol()`), # we enable ALLOW_FILE_PROTOCOL. # If you don't need this, it's better to disable it (remove the line below). ALLOW_FILE_PROTOCOL: "true" TOKEN: SOME_TOKEN_HERE # Not exposing the port is recommended, if PHP is running in a sidecar container ports: - "127.0.0.1:3000:3000" tmpfs: - /tmp - /workspace - /home/blessuser/.cache
用法
创建 Browserless API 客户端
$browserlessApiUrl = 'http://localhost:3000'; // Or 'http://browserless:3000', etc. $browserlessToken = 'SOME_TOKEN_HERE'; // Can be null for unsecured instances $browserlessTimeoutSeconds = 15; $client = new \Devture\Component\Browserless\Client( new \GuzzleHttp\Client(), $browserlessApiUrl, $browserlessToken, $browserlessTimeoutSeconds, );
从 URL 生成 PDF
$url = 'https://devture.com'; $pdfCreationRequest = new \Devture\Component\Browserless\Model\PdfCreationRequest(); $pdfCreationRequest->setUrl($url); $pdfCreationRequest->setOptions([ 'printBackground' => true, 'format' => 'A4', 'landscape' => true, ]); $pdfBytes = $client->createPdfFromRequest($pdfCreationRequest);
从内联 HTML 生成 PDF
$html = '<html><body>Some <strong>HTML</strong> here</body></html>'; $pdfCreationRequest = new \Devture\Component\Browserless\Model\PdfCreationRequest(); $pdfCreationRequest->setHtml($html); $pdfCreationRequest->setOptions([ 'printBackground' => true, 'format' => 'A4', 'margin' => [ 'top' => '20mm', 'bottom' => '10mm', 'left' => '10mm', 'right' => '10mm', ], ]); $pdfBytes = $client->createPdfFromRequest($pdfCreationRequest); // Alternatively, save this as a local workspace file and load it from there using the `file://` protocol. // (This allows you to access other files you may have mounted on the filesystem). // $pdfBytes = $client->createPdfFromHtmlRequestUsingFileProtocol($pdfCreationRequest);
从一组工作区保存的文件生成 PDF
这需要 Browserless 以 ALLOW_FILE_PROTOCOL: "true"
运行(请参阅上面的示例 docker-compose.yml
文件)。
// These calls save files into the Browserless workspace directory (e.g. `/workspace/<UUID>.<extension>`). $workspaceFileLogo = $client->createWorkspaceFile(file_get_contents('/path/to/logo.jpg'), 'jpg'); $workspaceFileStyles = $client->createWorkspaceFile(file_get_contents('/path/to/styles.css'), 'css'); $html = ' <html> <head> <link rel="stylesheet" href="file://' . $workspaceFileStyles->getPath() . '" /> </head> <body> <img src="' . $workspaceFileLogo->getPath() . '" alt="Logo" /> Some <strong>HTML</strong> here </body> </html>'; $pdfCreationRequest = new \Devture\Component\Browserless\Model\PdfCreationRequest(); $pdfCreationRequest->setHtml($html); $pdfCreationRequest->setOptions([ 'printBackground' => true, 'format' => 'A4', ]); // We need to use `createPdfFromHtmlRequestUsingFileProtocol()` here, // because we can only access files via the `file://` protocol // if the HTML is also served from a `file://`-accessed file. $pdfBytes = $client->createPdfFromHtmlRequestUsingFileProtocol($pdfCreationRequest); // Optionally, clean up. Because we have `WORKSPACE_DELETE_EXPIRED` enabled, // workspace files will be auto-cleaned at some point anyway, but.. $client->deleteWorkspaceFile($workspaceFileLogo); $client->deleteWorkspaceFile($workspaceFileStyles);
替代方案
-
SynergiTech/chrome-pdf-php 库也可以通过 Browserless 渲染 PDF
-
通过 knplabs/knp-snappy 调用的 wkhtmltopdf