Laravel HTML 到 PDF 的转换 (DOMPDF)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32712967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:23:14  来源:igfitidea点击:

Laravel HTML to PDF conversion (DOMPDF)

phplaravellaravel-5dompdf

提问by Sarath TS

DOMPDF Wrapper for Laravel 5, It's a html to pdf converter for laravel 5.

Laravel 5 的 DOMPDF 包装器,它是 Laravel 5 的 html 到 pdf 转换器。

I have tried to convert laravel default auth register page to PDF and the path is http://www.careertag.dev/auth/register. Code used for this purpose is given below. But I am getting font issue. Could you please help me to solve this issue?

我尝试将 Laravel 默认身份验证注册页面转换为 PDF,路径为http://www.careertag.dev/auth/register。用于此目的的代码如下。但我遇到字体问题。你能帮我解决这个问题吗?

routes.php

路由文件

Route::post('/screenshot', function(){
    $pdf  =  App::make('dompdf.wrapper');
    //$pdf->loadHTML('<h1>Test</h1>');
    $view =  View::make('auth.register')->render();
    $pdf->loadHTML($view);
    $pdf->stream();
    //return $pdf->stream('invoice');
    return $pdf->download('profile.pdf');
});

Error

错误

ErrorException in font_metrics.cls.php line 343: file_put_contents(C:\xampp\htdocs\careertag\storage\fonts/13350985545a9988387f8a573f409063.ttf): failed to open stream: No such file or directory

回答by Mustafa Ehsan Alokozay

Create fontsfolder inside storagedirectory and make sure storageis writable by web server by running follwing command. This worked for me.

存储目录中创建字体文件夹,并通过运行以下命令确保存储可被 Web 服务器写入。这对我有用。

sudo chmod -R 777 storage

Above (777) can be dangerous, therefore to achieve it in a secure way, try the below commands.

以上 (777) 可能是危险的,因此要以安全的方式实现它,请尝试以下命令。

sudo find storage -type d -exec chmod 755 {} \; 
sudo find storage -type f -exec chmod 644 {} \;

回答by Aman Ullah

Copy the 'fonts' directory of your theme form the location 'project-directory/public/themes/your-theme/path-to-fonts-directory' to the location 'project-directory/storage/fonts-directory' This works for me when I was hanged for long time.

将主题的“字体”目录从位置“项目目录/公共/主题/你的主题/路径到字体目录”复制到位置“项目目录/存储/字体目录”这适用于当我被绞死很长时间的时候。