我正在尝试在 Laravel 项目中基于 HTML 代码生成 PDF,我使用 Composer 安装了
spatie/laravel-pdf ^1.5
。问题似乎出在木偶操作者身上,但我不确定问题可能是什么。
Pdf 模型更新函数中的 $data 变量以字符串形式发送 HTML 代码,该代码经过修改,然后由 spatie/laravel-pdf 包转换为 Pdf,但是当尝试生成 PDF 时,会出现错误:未找到傀儡师。使用
npm i -g puppeteer
安装 Puppeteer 不会更改错误的输出。
{
"name": "spatie/laravel-pdf",
"version": "1.5.0",
"source": {
"type": "git",
"url": "https://github.com/spatie/laravel-pdf.git",
"reference": "8c880bf8d386b9580fcac6c5b99b2d3a6b1becbf"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/spatie/laravel-pdf/zipball/8c880bf8d386b9580fcac6c5b99b2d3a6b1becbf",
"reference": "8c880bf8d386b9580fcac6c5b99b2d3a6b1becbf",
"shasum": ""
},
"require": {
"illuminate/contracts": "^10.0|^11.0",
"php": "^8.2",
"spatie/browsershot": "^4.0",
"spatie/laravel-package-tools": "^1.16.1",
"spatie/temporary-directory": "^2.2.1"
},
"require-dev": {
"ext-imagick": "*",
"larastan/larastan": "^2.7.0",
"laravel/pint": "^1.13.7",
"nunomaduro/collision": "^7.10",
"orchestra/testbench": "^8.18",
"pestphp/pest": "^2.30",
"pestphp/pest-plugin-arch": "^2.5",
"pestphp/pest-plugin-laravel": "^2.2",
"phpstan/extension-installer": "^1.3.1",
"phpstan/phpstan-deprecation-rules": "^1.1.4",
"phpstan/phpstan-phpunit": "^1.3.15",
"spatie/image": "^3.3.2",
"spatie/laravel-ray": "^1.33",
"spatie/pdf-to-image": "^2.2",
"spatie/pdf-to-text": "^1.52.1",
"spatie/pest-expectations": "^1.5",
"spatie/pest-plugin-snapshots": "^2.1",
"spatie/pixelmatch-php": "^1.0",
"wnx/sidecar-browsershot": "^2.0"
},
"type": "library",
"extra": {
"laravel": {
"providers": [
"Spatie\\LaravelPdf\\PdfServiceProvider"
],
"aliases": {
"LaravelPdf": "Pdf"
}
}
},
"autoload": {
"files": [
"src/Support/functions.php"
],
"psr-4": {
"Spatie\\LaravelPdf\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Freek Van der Herten",
"email": "[email protected]",
"role": "Developer"
}
],
"description": "Create PDFs in Laravel apps",
"homepage": "https://github.com/spatie/laravel-pdf",
"keywords": [
"laravel",
"laravel-pdf",
"spatie"
],
"support": {
"issues": "https://github.com/spatie/laravel-pdf/issues",
"source": "https://github.com/spatie/laravel-pdf/tree/1.5.0"
},
"time": "2024-04-08T11:11:24+00:00"
},
"require": {
"php": "^8.2",
"awcodes/filament-tiptap-editor": "^3.3",
"filament/filament": "^3.2",
"laravel/framework": "^11.0",
"laravel/tinker": "^2.9",
"spatie/laravel-pdf": "^1.5"
},
进口
use Spatie\LaravelPdf\Facades\Pdf as PdfGen;
use Spatie\Browsershot\Browsershot;
protected static function boot()
{
parent::boot();
static::updated(function ($data) {
$html = $data->content;
$html = '<div class="tiptap-editor"><div class="ProseMirror">'.$html.'</div></div>';
$html = '<style>'. file_get_contents('css/awcodes/tiptap-editor/tiptap.css') .'</style>'.$html;
$html = '<style>'.file_get_contents('https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css').'</style>'.$html;
$dom = new \DOMDocument();
$dom->loadHTML($html);
$images = $dom->getElementsByTagName('img');
foreach ($images as $image) {
$src = $image->getAttribute('src');
$image->setAttribute('src', public_path($src));
$type = pathinfo(public_path($src), PATHINFO_EXTENSION);
$data = file_get_contents(public_path($src));
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
$image->setAttribute('src', $base64);
}
$html = $dom->saveHTML();
$html = str_replace('<html>', '', $html);
$html = str_replace('<head>', '', $html);
if (file_exists('output.pdf'))
unlink('output.pdf');
$pdf = PdfGen::html($html)->save('output.pdf');
file_put_contents('output.html', $html);
});
}
仅使用普通的->save()
$pdf = PdfGen::html($html)->save('output.pdf');
错误:
The command "node "C:\Users\user\homestead\code\filament2\vendor\spatie\browsershot\src/../bin/browser.cjs" "{""url"":""file:\/\/C:\\Users\\user\\AppData\\Local\\Temp\\1426089947-0494949001713261683\\index.html"",""action"":""pdf"",""options"":{""path"":""output.pdf"",""args"":[],""viewport"":{""width"":800,""height"":600},""displayHeaderFooter"":false,""printBackground"":true}}"" failed. Exit Code: 1(General error) Working directory: C:\Users\user\homestead\code\filament2\public Output: ================ Error Output: ================ 'node' is not recognized as an internal or external command, operable program or batch file.
$pdf = PdfGen::html($html)->setNodeBinary("C:\\Program Files\\nodejs\\node.exe")->save('output.pdf'); // Verified that this is using the correct node path.
错误:
Call to undefined method Spatie\LaravelPdf\PdfBuilder::setNodeBinary()
$pdf = PdfGen::html($html)->withBrowsershot(function (Browsershot $browsershot) {
$browsershot->setNodeBinary("C:\\Program Files\\nodejs\\node.exe");
})->save('output.pdf');
错误:
The command ""C:\Program Files\nodejs\node.exe" "C:\Users\user\homestead\code\filament2\vendor\spatie\browsershot\src/../bin/browser.cjs" "{""url"":""file:\/\/C:\\Users\\user\\AppData\\Local\\Temp\\831105329-0121756001713262165\\index.html"",""action"":""pdf"",""options"":{""path"":""output.pdf"",""args"":[],""viewport"":{""width"":800,""height"":600},""displayHeaderFooter"":false,""printBackground"":true}}"" failed. Exit Code: 1(General error) Working directory: C:\Users\user\homestead\code\filament2\public Output: ================ Error Output: ================ [Error: ENOENT: no such file or directory, mkdtemp 'undefined\temp\puppeteer_dev_chrome_profile-XXXXXX'] { errno: -4058, code: 'ENOENT', syscall: 'mkdtemp', path: 'undefined\\temp\\puppeteer_dev_chrome_profile-XXXXXX' }
添加 ->setNpmBinary() 和 ->newHeadless()
The command ""C:\Program Files\nodejs\node.exe" "C:\Users\ic10\homestead\code\filament2\vendor\spatie\browsershot\src/../bin/browser.cjs" "{""url"":""file:\/\/C:\\Users\\ic10\\AppData\\Local\\Temp\\805633131-0807926001713262416\\index.html"",""action"":""pdf"",""options"":{""path"":""output.pdf"",""args"":[],""viewport"":{""width"":800,""height"":600},""displayHeaderFooter"":false,""newHeadless"":true,""printBackground"":true}}"" failed. Exit Code: 1(General error) Working directory: C:\Users\ic10\homestead\code\filament2\public Output: ================ Error Output: ================ [Error: ENOENT: no such file or directory, mkdtemp 'undefined\temp\puppeteer_dev_chrome_profile-XXXXXX'] { errno: -4058, code: 'ENOENT', syscall: 'mkdtemp', path: 'undefined\\temp\\puppeteer_dev_chrome_profile-XXXXXX' }
在 ->withBrowsershot() 函数中使用 ->setIncludePath()
$pdf = PdfGen::html($html)->withBrowsershot(function (Browsershot $browsershot) {
$browsershot->setIncludePath("C:\\Program Files\\nodejs\\node.exe");
})->save('output.pdf');
错误:
The command "node "C:\Users\user\homestead\code\filament2\vendor\spatie\browsershot\src/../bin/browser.cjs" "{""url"":""file:\/\/C:\\Users\\user\\AppData\\Local\\Temp\\2023162371-0794294001713262550\\index.html"",""action"":""pdf"",""options"":{""path"":""output.pdf"",""args"":[],""viewport"":{""width"":800,""height"":600},""displayHeaderFooter"":false,""printBackground"":true}}"" failed. Exit Code: 1(General error) Working directory: C:\Users\user\homestead\code\filament2\public Output: ================ Error Output: ================ 'node' is not recognized as an internal or external command, operable program or batch file.
barryvdh/laravel-dompdf
。集成和使用非常简单。