本文介绍了生成的图片为mpdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用zend barcode生成了barcode,并在kohana 3.3.1 controller中生成了它,看起来像这样.

I have generated barcode using zend barcode and out it in a kohana 3.3.1 controller and it looks like this.

<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Barcode extends Controller_Base {

public function after()
{
    $this->response->headers('Content-Type','image/gif');
    parent::after();
}

public function action_Letter()
{
    Helper_Barcode::generate_barcode(Enum_Post::LETTER);
}

}

它在查看网站上效果很好,但是当我在mpdf view中使用它时,例如:

It works great on view sites but when I'm using it in mpdf view like:

<div><img src="/Barcode/Letter"></div>   

它给我错误:

mPDF error: IMAGE Error (/Barcode/Letter): Error parsing image file - image type not recognised, and not supported by GD imagecreate

有人知道怎么了吗?

推荐答案

我也遇到了通过POST将图像解析到mPDF库或进行AJAX调用的问题.当我发送一些带有标签的HTML字符串时,它显示了以下错误:

I had also problems parsing images to mPDF library vía POST or making an AJAX call. When I send some HTML String with an tag on it, it showed me the following error:

"mPDF错误:图像错误( http://www.xxxxxx.com/folder/my_image .jpg ):解析图像文件时出错-无法识别图像类型,并且GD imagecreate不支持该图像类型."

"mPDF error: IMAGE Error (http://www.xxxxxx.com/folder/my_image.jpg): Error parsing image file - image type not recognised, and not supported by GD imagecreate"

我的解决方案是,而不是发送HTML代码中的标签,而是发送自定义标识符,例如:

My solution was, instead of sending tags in my HTML code, send a custom identifier like:

<body> 
code code code 

insert_image:my_image.jpg 

code code code 
</body>

—>所有这些html都将在POST字段中发送

—> All this html will be sent in a POST field

然后,在将使用mPDF的PHP中,我用正确的标签替换了该自定义代码:

Then, in the PHP that will use mPDF I replaced that custom code with the correct tags:

<?php
$content_html = $_POST[‘my_html_code_to_pdf']; // THE POSTED FIELD WITH ALL MY HTML CODE

$content_html = preg_replace("/insert_image*:([a-z_0-9.]*)/", " <img src='http://www.xxxxxx.com/folder/$1'/> ", $content_html);

$mpdf=new mPDF();
$mpdf->WriteHTML($content_html);
$mpdf->Output();
exit;
?>

它奏效了!

希望这会有所帮助!

这篇关于生成的图片为mpdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 03:14