austenc's avatar

Using Knp-Snappy with Laravel Homested -- css file not working?

Hey all, I've got a view that I'm trying to render to a pdf with knp-snappy. I'm using laravel-snappy and everything seems to work as long as I don't have a CSS file included in my view / layout.

The Problem When I include css with something like below it doesn't work:

<head>
    {{ HTML::style('path/to/file.css') }}
</head>

It just says pdf could not be generated or something similar.

When I inline all of my css inline like this, it works great:

<head>
    <style type="text/css">/* my css here */</style>
</head>

I found this thread in which OP seems to be having the same issue. The solution seems less than ideal though. I'd rather not 'inject' the contents of my css file into my view since that seems a bit hacky. Has anyone else ran into this issue?

Note that I am running on a homestead environment and viewing the site via a url like http://mysite.dev:8000/pdf. Perhaps port 8000 is throwing snappy off? I have already confirmed the css file is being included with an absolute (full-url) path when the view is rendered. Also, I'm able to render the view just fine (sans pdf) without inlining the css, it seems to be a problem isolated to generating the pdf and not finding the css file is causing it to choke.

Thanks in advance to anyone who might have some knowledge about this!

Update: I created a github issue related to this on the wrapper repository. Not sure where the issue lies exactly but will report back here with any further progress.

0 likes
9 replies
hromby's avatar

Hey :-) Make sure that you reference the full filesystem path to the CSS and not the full path on the webserver.

austenc's avatar

@hromby -- thanks for your input. Maybe I'm missing something here, but using the full (filesystem) path doesn't seem to load the css at all. Here's what I've got now:

<link rel="stylesheet" href="{{ public_path().'/css/style.min.css' }}">

Which yields:

<link rel="stylesheet" href="/home/vagrant/Code/project/public/css/style.min.css">

CSS doesn't get loaded from that url... what am I missing?

hromby's avatar

Here's the code I'm using to refer to the paths. I got tired of switching them around when testing in the browser vs. rendering as PDF. It does look a lot like what you've already got though.

    /**
     * Handle the given report from an internal request by running the data and rendering it as HTML.
     *
     * @param   Report  $report  the report to handle.
     * @return  string           the HTML.
     */
    public function handleFromInternal(Report $report)
    {
        $company = strtolower(str_replace(
            [' ', ',', 'æ', 'ø', 'å'],
            ['', '', 'a', 'o', 'a'],
            $report->company->name
        ));

        $paths = [
            'bootstrap' => public_path('reporting/css/bootstrap.css'),
            'style' => public_path('reporting/companies/' . $company . '/style.css'),
            'background' => public_path('reporting/companies/' . $company . '/background.jpg'),
            'logo' => public_path('reporting/companies/' . $company . '/logo.png'),
            'positive_arrow' => public_path('reporting/img/arrow-pos.svg'),
            'negative_arrow' => public_path('reporting/img/arrow-neg.svg'),
            'root' => public_path().'/',
            'company' => $company,
            'base_style' => public_path('reporting/css/base.css'),
        ];

        $pages = $this->getPages($report, $paths);

        return $this->view->make('reporting.base', compact('report', 'pages', 'paths'))->render();
    }

    /**
     * Handle the given report from an external (web) request by running the data and rendering it as HTML.
     *
     * @param   Report  $report  the report to handle.
     * @return  string           the HTML.
     */
    public function handleFromExternal(Report $report)
    {
        $company = strtolower(str_replace([' ', ','], '', $report->company->name));

        $paths = [
            'bootstrap' => asset('reporting/css/bootstrap.css'),
            'style' => asset('reporting/companies/' . $company . '/style.css'),
            'background' => asset('reporting/companies/' . $company . '/background.jpg'),
            'logo' => asset('reporting/companies/' . $company . '/logo.png'),
            'positive_arrow' => asset('reporting/img/arrow-pos.svg'),
            'negative_arrow' => asset('reporting/img/arrow-neg.svg'),
            'root' => asset(''),
            'company' => $company,
            'base_style' => asset('reporting/css/base.css'),
        ];

        $pages = $this->getPages($report, $paths);

        return $this->view->make('reporting.base', compact('report', 'pages', 'paths'))->render();
    }
Barryvdh's avatar

Are you using the built-in PHP server or a real server (Apache/Vagrant/Nginx)? The built-in php server can handle 1 request and is blocking, so cannot load assets during the same request.

Hesesses's avatar

I got external css files working inside the blade view with this:

<link type="text/css" media="all" rel="stylesheet" href="{{ public_path('css/pdf.css'); }}">
1 like
DivDax's avatar

Or set the option:

$snappy->setOption('user-style-sheet', '/your/file.css');
austenc's avatar

Thanks for all the great replies! Due to my need for exact printing precision, I had to use fpdf / tcpdf for this project instead of snappy. Given that fact, I'm not sure whose answer to accept. Thanks for your help everyone!

Please or to participate in this conversation.