Laravel-Snappy/wkhtmltopdf doesnt work with Javascript and external assets

Viewed 1375

Ive been stuck here for hours trying to figure out whats wrong with this thing. It works when I exclude/comment out the javascript part but not when its uncommented. Another problem I faced is adding external assets to the view using {{ asset('js/app.js') }}. I tried {{ url(...) }}, {{ public_path(...) }} since it worked on some people after my research but chrome returns an error Not allowed to load local resource, i just need this to add jquery. If theres an alternative way to adding jquery pls tell me, tried CDN to no avail

I switched from dompdf to snappy BECAUSE I heard it works with javascript but now im stuck. I really need help.

version: Windows 64-bit: wkhtmltopdf 0.12.6 (with patched qt)

Controller

<?php

namespace App\Http\Controllers;

use Barryvdh\Snappy\Facades\SnappyPdf as PDF;

class AsdController extends Controller
{
    public function index()
    {
       
        $pdf = PDF::loadView('asd.index');
        $pdf->setOption('enable-javascript', true);
        $pdf->setOption('javascript-delay', 13500);
        $pdf->setOption('enable-smart-shrinking', true);
        $pdf->setOption('no-stop-slow-scripts', true);
        return $pdf->download('asd.pdf');
    }
}

VIEW

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>

<h1 id="sss">This is a Heading</h1>
<p>This is a paragraph.</p>

</body>
<script>
    document.getElementById('sss').innerHTML = 'HAHA';
</script>
</html>

the error WITHOUT external assets but with javascript

The exit status code '-1073741819' says something went wrong: stderr: "Loading pages (1/6)
 [> ] 0%
[======> ] 10%
[==============================> ] 50%
" stdout: "" command: "C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf" 
--lowquality --enable-javascript --javascript-delay "13500" --enable-smart-shrinking 
--no-stop-slow-scripts 

the error WITH external assets and javascript

The exit status code '-1073741819' says something went wrong: stderr: "Loading pages (1/6)
 [> ] 0%
[======> ] 10%
[==============================> ] 50%
Warning: Failed to load http://localhost:8000/js/app.js (ignore) 
 " stdout: "" command: "C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf" 
--lowquality --enable-javascript --javascript-delay "13500" 
--enable-smart-shrinking --no-stop-slow-scripts 
1 Answers

I had the same problem on Windows x64 machine.

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'     => [],
    ],
Related