Search code examples
phpcodeigniterdompdf

Image not found or type unknown Dompdf 0.8.1 and CodeIgniter


I want to load an image to PDF from generated image.

I had set isRemoteEnabled to true and generated QRCode working fine

Here is my code.

$this->load->library(array('pdf', 'ciqrcode'));

$data = array('qrlink' => base_url('generateQR?text=123'));
$this->pdf->set_paper('folio');
$this->pdf->loadHtml($this->load->view('pdfoutput', $data, true));
$this->pdf->set_base_path(base_url());
$this->pdf->render();
$this->pdf->stream('QR-PDF', array("Attachment" => false));

And here is my view

<img src="<?= $qrlink ?>" alt="QRcode" width="150px">

But the output say Image not found or type unknown. So what should I do?

Thanks in advance.

Cheers.


Solution

  • I had a similar issue and after many hours searching the inter-web, I found out (thanks to Atiruz ) that Dompdf does NOT render images well with remote urls e.g. http://your-domain.com/com/images/img.png" /> always gave me the same error [Image not found or type unknown]. In my case***strong text*** I had to use a base64 encoded image and it seemed to have resolved my issue.

    I wrote a small snippet that can encode your image into base64 and return a string to use inside your tag.

    Here is how to use the base64 encode data as your img src

    //Use a valid base64 encoded data string
    <img src="your-base64-encoded-image-string" />
    

    Here is the snippet to convert your image into a base64 encoded image data

    function encode_img_base64( $img_path = false, $img_type = 'png' ){
        if( $img_path ){
            //convert image into Binary data
            $img_data = fopen ( $img_path, 'rb' );
            $img_size = filesize ( $img_path );
            $binary_image = fread ( $img_data, $img_size );
            fclose ( $img_data );
    
            //Build the src string to place inside your img tag
            $img_src = "data:image/".$img_type.";base64,".str_replace ("\n", "", base64_encode($binary_image));
    
            return $img_src;
        }
    
        return false;
    }
    

    I hope this helps someone out there!