Laravel 5.1 Snappy pdf 图像未在 pdf 文件中呈现

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33310537/
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:33:55  来源:igfitidea点击:

Laravel 5.1 Snappy pdf image not rendering in pdf file

laravelpdf-generationwkhtmltopdf

提问by karmendra

I am using barryvdh/laravel-snappyto generate pdf file. I have two image files 1. yourlogohere.png is in public/image/ folder and 2. logo2.png is in folder other than public i.e. storage/app/logo and to get this file I defined a route (www.example.org/logo/logo.png) and use following code to access it.

我正在使用barryvdh/laravel-snappy生成 pdf 文件。我有两个图像文件 1. yourlogohere.png 位于 public/image/ 文件夹中,2. logo2.png 位于 public 以外的文件夹中,即 storage/app/logo 并为了获取此文件,我定义了一个路径(www.example.org /logo/logo.png) 并使用以下代码访问它。

public function logo($filename)
{
    $file = Storage::disk('local_logo')->get($filename);
    $mime = 'image/png';
    return (new Response($file, 200))->header('Content-Type', $mime);
}

Problem:

问题:

When I use following code to generate pdf from the html containing the first file, pdf contains the yourlogohere.png image

当我使用以下代码从包含第一个文件的 html 生成 pdf 时,pdf 包含 yourlogohere.png 图像

$snappy = App::make('snappy.pdf');
$html='<img src="http://www.example.org/images/yourlogohere.png" class="img-responsive" alt="Your Logo Here">';
$snappy->generateFromHtml($html, $path,[],$overwrite = true);

your logo here image appeared in pdf

您的徽标图片出现在 pdf 中

But when I do exact same thing for the second file, pdf does not render the image.(When I open the link http://www.example.org/logo/logo2.pngin browser I get the image). What am I missing?

但是当我对第二个文件做完全相同的事情时,pdf 不会呈现图像。(当我http://www.example.org/logo/logo2.png在浏览器中打开链接时,我得到了图像)。我错过了什么?

$snappy = App::make('snappy.pdf');
$html='<img src="http://www.example.org/logo/logo2.png" class="img-responsive" alt="Your Logo Here">';
$snappy->generateFromHtml($html, $path,[],$overwrite = true);

image is not rendered only a rectangle appeared

图像没有被渲染,只出现了一个矩形

Thanks,

谢谢,

K

回答by Marius Stanciu

You can also do:

你也可以这样做:

<img src="data:image/jpeg;base64,
{{ base64_encode(@file_get_contents(url('your.image.url'))) }}">

回答by karmendra

I think I got the what the problem is, the route to access the image is via auth, even when user is logged in while accessing the snappy, the wkhtmltopdf exe runs in a shell that is totally different session. Now the right fix would be to be embed the image in the html that is sent to snappy instead of the link, Which I am not sure how I will do? Any suggestions welcome there.

我想我明白了问题所在,访问图像的路由是通过身份验证的,即使用户在访问 snappy 时登录,wkhtmltopdf exe 也会在完全不同的会话的 shell 中运行。现在正确的解决方法是将图像嵌入到发送到 snappy 而不是链接的 html 中,我不确定我会怎么做?欢迎任何建议。

Update:I as able to convert the image to data:image/png;base64, and embed it in html.

更新:我能够将图像转换为 data:image/png;base64,并将其嵌入到 html 中。

$html = view('mytemplate.default', compact('variable1', 'variable2'))->render();

/*Convert logo image to base64 before pdf conversion*/

//search for <img src="http://example.org/mytemplate/logo/logo1.png">" and replace the src with data:image/png;base64,
$search = '/(<img\s+src=["\'])([^"\']+)(\/mytemplate\/logo\/)(.*)(\.)(.*?)(["\']\s+[^>]+>)/'; 

$html = preg_replace_callback($search, function ($matches) use ($invoicedetail) {

    $filename = $matches[4] . $matches[5] . $matches[6];
    $file = Storage::disk('local_logo')->get('yourlogohere.png');
    $mime = "image/png";
    $mytemplate = MyTemplate::where('logo_filename', '=', $filename)->first();
    if (!empty($mytemplate)) {
        $file = Storage::disk('local_logo')->get($mytemplate->logo_filename);
        $mime = $mytemplate->logo_mime;
    }
    $base64 = 'data:' . $mime . ';base64,' . base64_encode($file);
    return $matches[1] . $base64 . $matches[7];
}, $html);

$pdf_filename = 'template' . $mytemlpate->id . '.pdf';
$path = storage_path('app' . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . $filename);
$snappy = App::make('snappy.pdf');