Laravel 5.1 Snappy pdf image not rendering in pdf file

最后都变了- 提交于 2021-02-07 18:20:31

问题


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

Problem:

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

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

Thanks,

K


回答1:


You can also do:

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



回答2:


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.

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


来源:https://stackoverflow.com/questions/33310537/laravel-5-1-snappy-pdf-image-not-rendering-in-pdf-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!