Laravel 5.1 Snappy pdf图像未在pdf文件中呈现 [英] Laravel 5.1 Snappy pdf image not rendering in pdf file

查看:75
本文介绍了Laravel 5.1 Snappy pdf图像未在pdf文件中呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using barryvdh/laravel-snappy to 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.

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

问题:

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

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

$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);

但是当我对第二个文件执行完全相同的操作时,pdf不会渲染图像.(当我打开链接 http://www.example.org/logo/logo2.png 在浏览器中,我得到的图像).我想念什么?

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.png in browser I get the image). What am I missing?

$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);

谢谢

K

推荐答案

我认为问题出在哪里,访问图像的途径是通过auth,即使用户在访问快照时也已登录,wkhtmltopdfexe在完全不同的会话中运行.现在正确的解决方法是将图像嵌入发送到snappy的html中而不是链接中,我不确定该怎么做?欢迎任何建议.

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.

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

Update: I as able to convert the image to data:image/png;base64, and embed it in 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');

这篇关于Laravel 5.1 Snappy pdf图像未在pdf文件中呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆