Spy.Murad
Spy.Murad

Reputation: 51

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.

Upvotes: 2

Views: 9666

Answers (3)

Mohd Ibrahim
Mohd Ibrahim

Reputation: 1

No need for all the boiler code. DomPDF recognises image's full public url. Simply just link the image as https://www.domain_name.com/images/image_name.png and you'll be fine

Upvotes: 0

Leonardo Quintero
Leonardo Quintero

Reputation: 1

I had the same issue with pdf and i solved using this function

function encode_img_base64($img_path = false): string
{
    if($img_path){
        $path = $img_path;
        $type = pathinfo($path, PATHINFO_EXTENSION);
        $data = file_get_contents($path);
        return 'data:image/' . $type . ';base64,' . base64_encode($data);
    }
    return '';
}

Upvotes: 0

Enomatix24
Enomatix24

Reputation: 156

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!

Upvotes: 4

Related Questions