laravel snappy external link not works

1.7k views Asked by At

I am using laravel snappy fot converting html to pdf but external links not works throws error. there is img tag and src is lorem picsum. I am using windows machine and it is localhost not production. Maybe windows is the problem, i am not sure. But read lots of answers and used all the suggestion for example public_path("image_path"), asset("image-path") but non of them worked.

The exit status code '1' says something went wrong: stderr: "Loading pages (1/6) [> ] 0% [======> ] 10% [================> ] 28% Error: Failed to load https://picsum.photos/200, with network status code 3 and http status code 0 - Host not found [============================================================] 100% Counting pages (2/6)
[============================================================] Object 1 of 1 Resolving links (4/6)
[============================================================] Object 1 of 1 Loading headers and footers (5/6) Printing pages (6/6) [> ] Preparing [============================================================] Page 1 of 1 Done Exit with code 1 due to network error: HostNotFoundError " stdout: "" command: C:\wamp64\bin\wkhtmltopdf\bin\wkhtmltopdf --lowquality --page-height "1000px" --page-width "992px" "C:\Users\hpp_2\AppData\Local\Temp\knp_snappy5f8fcd256bf775.59730728.html" "C:\Users\hpp_2\AppData\Local\Temp\knp_snappy5f8fcd256d5fa6.11170764.pdf".

I think that no need to add full html content because it is long just img tag

<img src="https://picsum.photos/200">  

here is php:

    public function downloadCv(Request $request)
    {
        $width = 992;
        $height = 1000;
        $html = file_get_contents('storage/cv-templates/01.html');
//        $image = public_path().'\images\bg-25.png';
//        dd($image);
//        $html = str_replace('<!-- %img% -->', '<img src="file://'.$image.'" width="200" height="100">', $html);

//        $html = str_replace('fontSrc', storage_path().'/fonts/Montserrat-Light.ttf', $html);
//        return PDF::loadHtml($html)->setOptions(["isRemoteEnabled" => true])->setPaper([0,0, $width, $height])->setWarnings(true)->stream();
//        return PDF::loadView('cv')->setPaper([0,0,$width, $height])->setWarnings(true)->stream();
//        return $html;
//        return view('cv');
        return SnappyPdf::loadHtml($html)
            ->setOption('page-width', $width.'px')
            ->setOption('page-height', $height.'px')
            ->setOption('disable-external-links', false)
            ->setWarnings(true)
            ->inline();
    }
1

There are 1 answers

0
L.J On

I had the same problem on Windows x64 machine. Yes, you have to use the public_path method.

wkhtmltopdf disables local file access by default in the 0.12.6 version.

For those that are using laravel-snappy, add the 'enable-local-file-access' option in the config\snappy.php:

'pdf' => [
        'enabled' => true,
        'binary'  => env('WKHTML_PDF_BINARY', '/usr/local/bin/wkhtmltopdf'),
        'timeout' => false,
        'options' => [
            'enable-local-file-access' => true,
            'orientation'   => 'landscape',
            'encoding'      => 'UTF-8'
        ],
        'env'     => [],
    ],

    'image' => [
        'enabled' => true,
        'binary'  => env('WKHTML_IMG_BINARY', '/usr/local/bin/wkhtmltoimage'),
        'timeout' => false,
        'options' => [
            'enable-local-file-access' => true,
            'orientation'   => 'landscape',
            'encoding'      => 'UTF-8'
        ],
        'env'     => [],
    ],