barryvdh / laravel-dompdf

A DOMPDF Wrapper for Laravel
MIT License
6.71k stars 967 forks source link

Image not found or type unknown #303

Closed karlpatrickespiritu closed 2 years ago

karlpatrickespiritu commented 7 years ago

After updating the version to 0.7.0. The images (.png) are broken ("Image not found or type unknown").

imgnotfoumd

williamzh721 commented 3 years ago

Yo estoy intentando con el siguiente código, pero siempre me sale el error de Image not found or type unknown <?php require_once("pdf2/autoload.inc.php"); ob_start(); include(dirname('FILE').'/gen_certificacionpdf.php'); use Dompdf\Dompdf; use Dompdf\Options; $dompdf = new Dompdf(); $options = new Options(); $options->setIsRemoteEnabled(true); $dompdf = new Dompdf($options); $html = ob_get_clean(); $dompdf->loadHtml($html); $dompdf->output(); $dompdf->setPaper('letter', 'portrait'); $dompdf->render(); $dompdf->stream("Certificacion_contrato"); ?>

aleruide commented 3 years ago

Yo tengo el mismo problema. Antes no me fallaba y desde que tuve que actualizar la versión de php me falla. He cambiado a varias versiones superiores a la que tenía al principio y me sigue fallando. He añadido el código de $Options y sigue igual. Todas las posibilidades que habéis comentado y sigo sin encontrar la solución al problema

RayhanYulanda commented 3 years ago

I'm running on SSL in local it doesn't work and give me this error "Image not found or type unknown" but when i tried to turn off the SSL in local it works perfectly! It run as well in server by the way

danielbehrendt commented 3 years ago

I've got the same problem and all the above mentioned solutions didn't work. The problem seems to be that my images may contain query strings (when trying to embed signed images from S3). My current workaround is to make these images public and don't sign them.

Jenry-MA commented 3 years ago

This work for me.

<img src="data:image/png;base64,{{ base64_encode(file_get_contents(public_path('img/example.png'))) }}">

Reference link: https://github.com/barryvdh/laravel-dompdf/issues/761

Dlogon commented 3 years ago

I used @josesotomayor suggest. I created a custom class with the method that gets the base64.

`<?php namespace App\Custom;

class Archivos { public static function imagenABase64($ruta_relativa_al_public) { $path = $ruta_relativa_al_public; $type = pathinfo($path, PATHINFO_EXTENSION); $data = \File::get($path);

  $base64 = "";
  if ($type == "svg") {
      $base64 = "data:image/svg+xml;base64,".base64_encode($data);
  } else {
      $base64 = "data:image/". $type .";base64,".base64_encode($data);
  }
  return $base64;
}

}`

Then in the view i did this.

@php use App\Custom\Archivos; @endphp <img src="{{Archivos::imagenABase64('assets/img/logo_empresa_contrato.jpg')}}" width="200px" >

Thanks, works for me too

Nexample-G commented 3 years ago

Real and Working solution https://github.com/Nexample-G/Image-not-found-or-type-unknown-dompdf-

vsalguero commented 3 years ago

What solved it for me was using public_path('path/to/image.png').

<img src="{{ public_path('path/to/image.png') }}" /> Excelent! this option solved my problem 👏👏

ARCHI207 commented 3 years ago

Enabling remote by setting options helped me fix this. ... $html = ''; $options = new Options(); $options->setIsRemoteEnabled(true); $dompdf = new Dompdf($options); $dompdf->loadHtml($html); $dompdf->output(); ...

Thank you.. this worked for me.

alcarazolabs commented 2 years ago

After Internet shutdown it happened. It worked for me:

 //Obtener compra
        $purchase = Purchase::findOrFail($id);
        //crear objeto pdf para mostrar número de página. Se habilito php para dompdf. "enable_php" => true en config/dompdf.php
        $pdf = app('dompdf.wrapper');

        $contxt = stream_context_create([
            'ssl' => [
                'verify_peer' => FALSE,
                'verify_peer_name' => FALSE,
                'allow_self_signed' => TRUE,
            ]
        ]);

        $pdf = \PDF::setOptions(['isHTML5ParserEnabled' => true, 'isRemoteEnabled' => true]);

        $pdf->getDomPDF()->setHttpContext($contxt);

        //Cargar vista/tabla html y enviar varibles con la data
        $pdf->loadView('purchase.pdfReportsViews.invoice', compact('purchase', 'pdf'));
        //Establecer orientación horizontal al pdf
        $pdf->setPaper('A4', 'landscape');

        //descargar la vista en formato pdf 
        $fecha = date('Y-m-d');
        return $pdf->download("MyPdf.pdf");
stale[bot] commented 2 years ago

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.

mubahood commented 2 years ago

if you are using laravel, consider excluding public folder in .htacccess from folders that laravel control. do it like this

# Exclude directory from rewriting RewriteRule ^(public/) - [L]

SynysterGrowly commented 2 years ago

I use {{URL::to('/storage/'.$data)}} for dinamic image by user

jagp-afimex commented 2 years ago

Hello, the next works for me:

  1. Put your image inside the storage/app/public folder.

  2. Get your image using the get method from the Storage's facade. use Illuminate\Support\Facades\Storage; $image = Storage::get("public/image.png");

  3. Covert it to base64. $img_to_base64 = base64_encode($image);

  4. Use it in your HTML code. <img src="data:image/png;base64, {{ $img_to_base64 }}">

stale[bot] commented 2 years ago

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.

SiimplyInc commented 2 years ago

Solution: Convert to base 64 the image:

Example:

<'img' src="data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUA AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO 9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Example" />

For those who are like me and had no idea what base64 was, it's a file format. I found a simple png to base64 converter here https://onlinepngtools.com/convert-png-to-base64 and then just copy the output and replace the following section with the new output

"data:image/png;base64, PASTE OUPUT HERE"

gtresnandika commented 1 year ago

Hi..Im facing the same problems..my solution is to know where is your image mine, using laravel 7 having upload form and placed on storage/app/uploads/..../image.png so, for me, im using {{storage_path('app/upload/image.png')}}

Aaron-Reyes-Developer commented 1 year ago

si no te funciona prueba descomentando la linea de codigo de php inti tienes que ir a xampp apretar config en apache (PHP php.ini) buscas la linea de codigo "openssl" (con ctrl+b buscas) y descomentas ;extension=openssl -> extension=openssl y tambien ;extension=gd -> extension=gd

y en el dompdf de el proyecto que estas haciendo

use Dompdf\Dompdf;
use Dompdf\Options;
ob_start();
$options = new Options();
$options->set('chroot', realpath(''));
$dompdf = new Dompdf($options);
$dompdf->load_html("<img src='ruta.png'>);
$dompdf->render();
$dompdf->stream('Nombre.pdf', array('Attachment'=>false));
harinderratton commented 1 year ago

{{ public_path('path/to/image.png') }}

It worked like a charm, Remember it will only work after when pdf is downloaded

Jeffersonjcl commented 1 year ago

Guys, I had this problem after uploading the system to the Hostinger server, where the report was normal showing the image but the server does not show the image, has anyone had this problem? How did you solve it?

felixpenrose commented 11 months ago

You have to ask yourself, is the PDF package (that runs on the backend) able to access the image on the URL when calling from the backend. (I know it sounds stupid obvious).

I was running this from inside a docker container. The image I was trying to fetch was using the URL specified in the APP_URL configuration, that worked from the outside world, as that domain was mapped in my /etc/hosts file on my development machine.

The hostname was not mapped inside the container though.

Temporarily hardcoding the domain in the image URL to "localhost" confirmed, that it indeed was the case.

In my case updating the /etc/hosts inside the docker container solved it.

Are you using custom port numbers for your docker containers, you have another problem.

@KristianI

You've hit the nail on the head of the problem I'm having. What did you need to add to your /etc/hosts to get this working?

I've tried a few variations. My docker containers are within a reverse proxy network and I have an internal alias set up from a previous project but I just can't get the right combination.

This is my internal network. I can run a curl dompdftest-internal/test-image.jpg --output img.jpg which works, but similar things are not working when setting the image src="" properties...

    networks:
      default:
        aliases:
          - dompdftest-internal
felixpenrose commented 11 months ago

I've just figured this out ⬆️ I can simply replace my image urls to equal https://dompdftest-internal/my-image.jpg instead of https://dompdftest.local which is where my web browser points.

Just to add clarity this is my dockers apache container

  webserver:
    env_file: .env
    platform: ${WEB_PLATFORM}
    container_name: ${PROJECT_NAME}-webserver
    image: httpd:2.4-alpine
    restart: 'no'
    depends_on:
      - app
    tty: true
    expose:
      - 80
    volumes:
      - ${PROJECT_ROOT}:/var/www
      - ./docker/apache/default.conf:/usr/local/apache2/conf/httpd.conf
      - ./docker/apache/custom-config.conf:/usr/local/apache2/conf/extra/custom-config.conf
    networks:
      default:
        aliases:
          # use this hostname when sending requests to this container
          # from other containers within the same network.
          - ${PROJECT_NAME}-web-internal
SameeraPrasantha commented 10 months ago

$options = new Options(); $options->setIsRemoteEnabled(true); $dompdf = new Dompdf($options);

my solution and it's works.!

txg commented 6 months ago

Works for me: change file extension from .jpg to .jpeg

GonzoTBA commented 6 months ago

https://github.com/dompdf/dompdf

See "Limitations". There you have the solution (worked for me).

GuilhermeSTrydeR commented 5 months ago

Just Replace ( dompdf\src\Image\Cache.php ) From dompdf 0.8.5 To All New Version Dompdf !

dompdf 0.8.5 : https://github.com/dompdf/dompdf/releases/tag/v0.8.5

you can see here : https://youtu.be/bM3y5TY-7_k

works for me! in my case, the directory doesn't exists! i just download a new DOMPDF and replace it!

muradbdinfo commented 5 months ago

this settings solved my error..

image

muradbdinfo commented 5 months ago

<img width="150" height="150" src="data:image/png;base64,{{ base64_encode(file_get_contents(public_path($Downloadapplicant->pictures))) }}" style="border-radius: 5px;"

     return $pdf->setOptions(['dpi' => 150, 'defaultFont' => 'sans-serif','isPhpEnabled' => true,'setIsRemoteEnabled' =>true])
       ->setWarnings(false)
       ->download($row->name . '.pdf');

       above code solved my error...