Search code examples
laravelpdf-generationwkhtmltopdf

Laravel 5.1 Snappy pdf image not rendering in pdf file


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

your logo here image appeared in 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.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);

image is not rendered only a rectangle appeared

Thanks,

K


Solution

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