laravel-dompdf icon indicating copy to clipboard operation
laravel-dompdf copied to clipboard

image not found or type unknown

Open elessa237 opened this issue 3 years ago • 9 comments
trafficstars

I use dompdf version 2.0 and symfony 5.4.

when rendering the pdf I have an error while displaying the image "image not found or type unknown".

my dompdf code

`<?php

namespace App\Infrastructure\Prints;

use Dompdf\Dompdf; use Dompdf\Options;

/**

  • @author Elessa Maxime [email protected]

  • @package App\Infrastructure\Prints */ class PrintService { public function print(string $html, string $name, string $orientation = 'portrait', string $format = 'A5'): void { $options = new Options(); $options->setIsRemoteEnabled(true); $options->setIsHtml5ParserEnabled(true); $options->set('defaultFont', 'Courier');

     $pdf = new Dompdf($options);
    
     $pdf->loadHtml($html);
    
     $pdf->setPaper($format, $orientation);
    
     $pdf->render();
    
     $pdf->stream($name.".pdf", [
         "Attachment" => false
     ]);
    

    } }`

my img tag in my vu twig

<img src="{{ asset('aris_logo.png') }}" style="margin-top: 20px;height: 40px; width: 60px;" alt=""/>

l'image se trouve dans /public

elessa237 avatar Aug 17 '22 10:08 elessa237

This is only a wrapper, dompdf package it not here

PaolaRuby avatar Aug 25 '22 13:08 PaolaRuby

we can work around this situation if we use base64 images.

WoneyBranga avatar Sep 15 '22 19:09 WoneyBranga

Most likely it is not the issue with the package but the problem in your dev environment.

I assume it happens because your local environment fails to verify the SSL certificate (at least that happened to me). If you turn on the 'show_warnings' flag in your dompdf configuration file you will see the actual error. And if you get the same error as below you need to create your own root certificate. file_get_contents(): SSL operation failed with code 1.

In my case, I am using MAMP so I followed instructions that I found on stack overflow and it worked as a charm.

I strongly suggest fixing this problem instead of using base64 images as a workaround.

Tadaz avatar Oct 10 '22 14:10 Tadaz

Try adding this on config/dompdf.php

'options' =>[

    ///
    'http_context' => [
        'ssl'=>[
            'verify_peer'=>false,
            'verify_peer_name'=>false,
            'allow_self_signed'=>true
        ]
    ],

Source: dompdf/Options.php#L408-L410

PaolaRuby avatar Oct 10 '22 16:10 PaolaRuby

@elessa237 I ran into the same issue now and I found out that the reason for this was because the package could not access the image on my local machine. A quick walk-around is to upload the image to some online server and use that link instead (at least for test purposes).

Using setWarnings(true) allows you see what the issue might be.

Pdf::loadView('view-name')
            ->setPaper('a2', 'portrait')
            ->setWarnings(true)
            ->save($filePath);

Emmarex avatar Oct 17 '22 14:10 Emmarex

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Any issues with PDF rendering itself that are not directly related to this package, should be reported on https://github.com/dompdf/dompdf instead. When having doubts, please try to reproduce the issue with just dompdf. If you believe this is an actual issue with the latest version of laravel-dompdf, please reply to this issue so we can investigate further. Thank you for your contribution! Apologies for any delayed response on our side.

stale[bot] avatar Jun 18 '23 08:06 stale[bot]

To solve my problem, I simply used an absolute URL and it works very well. . I would like to remind you that I am using Symfony, so I'm using Twig templates.

elessa237 avatar Jun 26 '23 12:06 elessa237

we can work around this situation if we use base64 images.

Merci pour cette reponse :

$imageData = base64_encode(file_get_contents('images/lgg.png'));

florella-yannis avatar Jun 30 '23 11:06 florella-yannis

we can work around this situation if we use base64 images.

Merci pour cette reponse :

$imageData = base64_encode(file_get_contents('images/lgg.png'));

Working 100%!

dansp89 avatar Aug 27 '23 05:08 dansp89