使用Dompdf保存PDF文件

9
使用Dompdf将数据保存为PDF文件:
此功能可以正常工作:
$pdf = \App::make('dompdf.wrapper');
$pdf->loadHTML('<h1>Test</h1> ');
return $pdf->stream();

现在,当尝试时
$pdf = \App::make('dompdf.wrapper');
$pdf->loadHTML('<h1>Test</h1> ');
file_put_contents("test.pdf", $pdf->output());

获取错误:
file_put_contents(test.pdf): failed to open stream: Permission denied

我需要为保存文件或其他东西创建额外的文件夹吗?

谢谢,P


非常简单,您没有足够的权限写入文件夹/目录?请检查您的Web用户是否具有所需的读/写文件夹和文件的权限。 - Epodax
嗨,谢谢你的时间!没问题,我也尝试使用特定路径,但是出现了相同的错误...但是我在文档中找不到这个文件将保存在哪个文件夹中,所以无法知道要更改什么权限... - pavlenko
你可以查看以下链接以获取更好的解决方案。https://stackoverflow.com/questions/60795346/how-to-save-dompdf-file-to-storage-and-name-the-file-dynamicly-in-laravel/74942859#74942859 - pankaj
4个回答

11
要将生成的pdf保存到文件中,可以使用output()函数,例如:
$dompdf = new Dompdf();
$dompdf->loadHtml('<h1>hello world</h1>');
$output = $dompdf->output();
file_put_contents('filename.pdf', $output);

您可以使用一个路径:file_put_contents('/home/website/public_html/aap_path/temp/filename.pdf', $output); - Adrian P.
是的,您也可以使用绝对路径而不是相对路径。 - Pedro Lobito

2

1

试试这个

$pdf->load_html('<h1>Test</h1>');
$pdf->render();
$pdf->stream("data.pdf");

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接