douhua1890 2018-02-07 03:38
浏览 126

未找到图像或输入未知的Dompdf 0.8.1和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.

  • 写回答

1条回答 默认 最新

  • dongzhao1865 2018-07-10 09:48
    关注

    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 ("
    ", "", base64_encode ( $binary_image ) );
            return $img_src;
        }
        return false;
    }
    

    I hope this helps someone out there!

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大