2

I'm trying to create an export button which converts an HTML page to PDF then downloads it.

I'm using Windows 10, XAMPP, Laravel 5.8, wkhtmltopdf.

Here is the error screenshot : enter image description here

Snappy Config:

'pdf' => [
    'enabled' => true,
    'binary'  => '"F:\wkhtmltopdf\bin\wkhtmltopdf.exe"',   
    'timeout' => false,
    'options' => [],
    'env'     => [],
],
'image' => [
    'enabled' => true,
    'binary'  => '"F:\wkhtmltopdf\bin\wkhtmltoimage.exe"',
    'timeout' => false,
    'options' => [],
    'env'     => [],
],

Export Code :

    $export_data = $this->exportData($checklist, $request);

    $pdf_content = view('checklist.pdf.content')->with($export_data);

    $pdf = PDF::loadHTML($pdf_content);
    $pdf->setPaper('letter');

    if ($checklist->weekly_monthly) {
        $pdf->setOrientation('landscape');
    }

    $pdf->setOption('header-spacing', 30);
    $pdf->setOption('header-html', view('checklist.pdf.header')->with($export_data));
    $pdf->setOption('footer-html', view('report.pdf.footer'));

    return $pdf->download($save_path);

1 Answer 1

2

Check your view checklist.pdf.content. In my experience with wkhtmltopdf, 99% of the time, the view was the issue.

Make sure that if you are using any image or external stylesheet in your view, their paths are properly defined. In case they are not, your HTML will work fine in your browser but using it for wkhtmltopdf will throw an error like this one.

In order to give paths for your images and external stylesheets, use following format:

<head>
    <link href="{{public_path()}}/css/app.css" rel="stylesheet">
</head>
<body>
    <img src="{{public_path()}}/images/test.png" />
</body>

Of course, the exact path might differ based upon your folder configuration but they must be publically accessible and they should be accessed using the remote path.

I hope it helps

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, my external stylesheet wasn't using the public_path and also there was an onload() event in my view which was causing the problem. I guess any DOM Event also causes these problems.
yup they do, glad i could help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.