php mPDF 5.7.1 - 图像显示为损坏的 [x]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22092683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
mPDF 5.7.1 - image displays as a broken [x]
提问by Mr.TK
I have a small issue with mPDF (version 5.7.1).
我对 mPDF(版本 5.7.1)有一个小问题。
This code should generate PDF with image file:
此代码应生成带有图像文件的 PDF:
$mpdf = new mPDF();
$html = '<img src="https://www.google.pl/images/srpr/logo11w.png"/>';
$mpdf->WriteHTML($html);
$mpdf->debug = true;
$output = $mpdf->Output();
exit();
Well there is no image but an [x]instead.
好吧,没有图像,而是一个[x]。
I've googled enough to get to the conclusion that it has to be done this way but I also tried realpath to the file. Still nothing.
我已经用谷歌搜索了足够多的结论,它必须以这种方式完成,但我也尝试了文件的真实路径。依然没有。
The only thing I haven't tried is <img src="logo11w.png">and copying the image into the folder because I don't know into which folder I should copy file logo11w.png.
我唯一没有尝试的是<img src="logo11w.png">将图像复制到文件夹中,因为我不知道应该将文件复制到哪个文件夹中logo11w.png。
Any suggestions?
有什么建议?
回答by ddn
I had the same problem with PNG images being displayed as [X] when to generate PDFs with mPDF.
当使用 mPDF 生成 PDF 时,PNG 图像显示为 [X] 时遇到了同样的问题。
I added: $mpdf->showImageErrors = true;
我补充说: $mpdf->showImageErrors = true;
After: $mpdf = new Mpdf();
后: $mpdf = new Mpdf();
and got the error message:
并收到错误消息:
GD library required for PNG image (alpha channel)#
PNG图像所需的GD库(alpha通道)#
So after running apt-get install php5-gdgenerating a PDF with a PNG worked like a charm!
因此,在运行apt-get install php5-gd生成带有 PNG 的 PDF 后,它就像一个魅力!
回答by pevac
Including images is kinda tricky in mPDF. I had some issues as well. I found more kind of problems.
在 mPDF 中包含图像有点棘手。我也遇到了一些问题。我发现了更多类型的问题。
At first you should turn on debug variable:
首先你应该打开调试变量:
$mpdf = new mPDF();
$mpdf->showImageErrors = true;
Usualy people don't have installed GD module for PHP. On linux machine, execute:
通常人们没有为 PHP 安装 GD 模块。在linux机器上,执行:
sudo apt-get install php5-gd
sudo service apache2 restart
On Windows servers, php_gd2.dll is included in a standard PHP installation, but is not enabled by default. To enable it, uncomment the extension=php_gd2.dllline in your php.inifile (remove the # from the beginning of that line) and restart the PHP extension. [1]
在 Windows 服务器上,php_gd2.dll 包含在标准 PHP 安装中,但默认情况下不启用。要启用它,请取消注释php.ini文件中的extension=php_gd2.dll行(删除该行开头的 #)并重新启动 PHP 扩展。[ 1]
If you get this error you probably see [x] image:
如果您收到此错误,您可能会看到 [x] 图像:
mPDF error: IMAGE Error (http://www.domain.com/directory/image.jpg): Could not find image file
mPDF 错误:图像错误(http://www.domain.com/directory/image.jpg):找不到图像文件
Check the url if your image exists and if the image is accessible. If yes then you can try to change absolute URL to relative. You can try both versions:
检查 url 是否存在您的图像以及图像是否可访问。如果是,那么您可以尝试将绝对 URL 更改为相对 URL。您可以尝试两个版本:
<img src="directory/image.jpg">
<img src="./directory/image.jpg">
Actually I also had a problem with PNG formats. Converted PNG image to JPG worked fine.
实际上,我也遇到了 PNG 格式的问题。将 PNG 图像转换为 JPG 效果很好。
Linking in mPDF templates should be same like for whole your framework/system.
mPDF 模板中的链接应该与整个框架/系统相同。
回答by Dimitri L.
I have encountered the same problem after migrating the script.
我在迁移脚本后遇到了同样的问题。
The problem was, that the tmpdirectory inside the mpdf folder was not writable. I hope it helps someone.
问题是,tmpmpdf 文件夹内的目录不可写。我希望它可以帮助某人。
回答by Krupal Patel
For me, it is working as of now. Hope this will help someone.
对我来说,它现在正在工作。希望这会帮助某人。
Solution : Try relative path of image instead of URL. Image must be hosted on the same server.
解决方案:尝试使用图像的相对路径而不是 URL。图像必须托管在同一台服务器上。
Ex: /var/www/mysite/image/xyz.jpg
例如:/var/www/mysite/image/xyz.jpg
回答by KoalaBear
Ok. After the last comment on question (14:30). Here the exact same code that IS working with mPDF 5.4. And it saves it's output on scripts directory as test.pdf. Firewall issues?
好的。在对问题的最后评论 (14:30) 之后。这里使用与 mPDF 5.4 完全相同的代码。并将其输出保存在脚本目录中作为 test.pdf。防火墙问题?
Edited
已编辑
I have the following directories / files:
我有以下目录/文件:
- images
- wallpaper01.jpg
- wallpaper02.jpg
- index.php (source code)
- 图片
- 壁纸01.jpg
- 壁纸02.jpg
- index.php(源代码)
So these image files are relatively starting from the script directory as it looks like.
所以这些图像文件相对来说是从脚本目录开始的。
require_once __DIR__ . DIRECTORY_SEPARATOR . 'MPDF/mpdf.php';
try {
$mpdf = new mPDF();
$mpdf->WriteHTML('<img src="images/wallpaper01.jpg" alt="" width="480">');
$mpdf->WriteHTML('<img src="images/wallpaper02.jpg" alt="" width="480">');
$mpdf->Output(__DIR__ . DIRECTORY_SEPARATOR . 'test.pdf', 'F');
} catch(Exception $e) {
echo $e;
}
回答by Santos L. Victor
You can try this:
你可以试试这个:
$mpdf->imageVars['myvariable'] = file_get_contents('alpha.png');
or
或者
$html = '<img src="var:myvariable"/>';
$mpdf->WriteHTML($html);
after there, you should do:
在那里之后,你应该这样做:
$mpdf->Image('var:myvariable', 0, 0);
read more about this in documentation: mPDF Load Image
在文档中阅读更多相关信息: mPDF Load Image
回答by Sergei Martinov
In my project i fix problem and solution is:
在我的项目中,我解决了问题,解决方案是:
Set src absolute path on the server example:
src="/var/www/myproject/images/logo.png"if image is on the same server.
If image is from external server src is absolute path example:
src="https://www.google.bg/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png".
Hope this will help someone.
在服务器示例上设置 src 绝对路径:
src="/var/www/myproject/images/logo.png"如果图像在同一台服务器上。如果图像来自外部服务器 src 是绝对路径示例:
src="https://www.google.bg/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png". 希望这会帮助某人。
回答by Tomas Grecio Ramirez
It's enough The solve is :
就够了 解决方法是:
$mpdf->curlAllowUnsafeSslRequests = true;
It's all
都是这样
回答by JuliSmz
In my case, the problem was just the usage of transparents PNG using the 6.0 version. This bug was fixed on the mpdf 6.1 version.
就我而言,问题只是使用 6.0 版本使用透明 PNG。此错误已在 mpdf 6.1 版本上修复。
I successfully solved the problem downloading the 6.1 version from hereand overriding the mpdf files on my project.
我成功解决了从这里下载 6.1 版本并覆盖我项目中的 mpdf 文件的问题。
回答by user2706194
change to remove the second line before initializing $html its undefined at that time. new code will be as
在初始化 $html 之前更改为删除第二行,当时它是 undefined 。新代码将作为
$mpdf = new mPDF();
$html= "<img src='https://www.google.pl/images/srpr/logo11w.png' alt=''>";
$mpdf->WriteHTML($html);
$mpdf->debug = true;
$output = $mpdf->Output();
exit();

