Laravel Snappy PDF problem generating PDF from view if it imports external content

2.5k Views Asked by At

A simple PDF is easily generated from a blade view when you only have simple HTML code and nothing that has to be imported.

However, if I add the href attribute to external locations, the snappyPDF simply doesn't work.

I am using Laravel 8 Jestream.

Here is part of my blade file:

<!doctype html>
<html lang="es">
<head>
    <link rel="preconnect" href="https://fonts.gstatic.com">
    <link href="https://fonts.googleapis.com/css2?family=Sorts+Mill+Goudy&display=swap" rel="stylesheet">
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Certificate</title>
    <style media="screen">
        body {
            font-family: 'Sorts Mill Goudy', serif;
            text-align:center;
            background-image: url("https://images.pexels.com/photos/242236/pexels-photo-242236.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260");
            background-repeat: no-repeat;
            background-position: left top;
            width:816px;
            height:1056px;
            border: 10px solid #e6e6e6
        }
    </style>
<body>
<div class="some">
    <div class="other">
        <img class="logo" src="https://images.pexels.com/photos/1337386/pexels-photo-1337386.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260" alt="Logo">
    </div>
</div>
</body>
</html>

The problem, as far as I can see, shows up when I add src="https external" or url(https//external) When I try to generate the PDF via Snappy, the pdf just can't get generated.

How do I fix this? What am I missing to make the Snappy PDF work?

1

There are 1 best solutions below

0
On

You need to enable option for external links support.

return \PDF::loadView('yourfile')
      ->setOption('disable-external-links', false)
      ->setOption('enable-local-file-access', true)
      ->setOption('enable-internal-links' , true)

OR

There is another package available with more configuration like Laravel Dompdf. You can able to add external links with "enable_remote = true" config.