Image not found or type unknown Dompdf 0.8.1 and CodeIgniter

佐手、 提交于 2019-12-11 07:35:47

问题


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.


回答1:


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 ( $image_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!



来源:https://stackoverflow.com/questions/48655579/image-not-found-or-type-unknown-dompdf-0-8-1-and-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!