php TCPDF 不显示带有 writeHTML 的图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13702891/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:54:48  来源:igfitidea点击:

TCPDF don't display image with writeHTML

phptcpdf

提问by tampe125

this question seems an evergreen on TCPDF...
I have an issue that's driving me crazy.

这个问题在 TCPDF 上似乎是常青树......
我有一个让我发疯的问题。

I have an html code that I use as "template" for my PDF, here I have my company logo.
Everything works fine on localhost (Windows), but when I move online, the image is not shown.
Pay attention: I don't get any error (ie the Unable to get imageerror) on my PDF the image is simple blank!
Infact if I click on the PDF on the position where the images it's supposed to be, I can select it, and Adobe enables the option "Copy image".

我有一个 html 代码,用作我的 PDF 的“模板”,这里有我的公司徽标。
在本地主机(Windows)上一切正常,但是当我在线移动时,图像没有显示。
注意:我的 PDF 上没有任何错误(即无法获取图像错误),图像只是空白!
事实上,如果我在 PDF 应该放置的图像位置上单击它,我可以选择它,并且 Adob​​e 会启用“复制图像”选项。

Obviously the image exists, is here, and permission are correct.
If I try to surf there, or view the generated HTML page, everything is fine.

显然图像存在,在这里,并且许可是正确的。
如果我尝试在那里冲浪,或查看生成的 HTML 页面,一切都很好。

This is the PHP code:

这是PHP代码:

$pdf->SetMargins($params->get('pdfMarginLeft', 15), $params->get('pdfMarginTop', 27), $params->get('pdfMarginRight', 15));
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$pdf->SetFont('helvetica', '', 8);
$pdf->AddPage();

$pdf->writeHTML($html, true, false, true, false, '');

$pdf->lastPage();

Then this is my HTML code (I just skipped everything except the image):

然后这是我的 HTML 代码(我只是跳过了除图像之外的所有内容):

<img alt="logo black" src="../images/logo_black.png" height="60" width="210" />

I've tried with the url (relative and absolute) and with the path (relative and absolute), the problem still occurs. Any ideas?

我已经尝试过使用 url(相对和绝对)和路径(相对和绝对),问题仍然存在。有任何想法吗?

回答by DACrosby

This wasn't your problem, but it's a possible solution for people with a similar issue in the future. Please make sure the HTML attributes have double quotes.

这不是您的问题,但对于将来遇到类似问题的人来说,这是一个可能的解决方案。请确保 HTML 属性有双引号。

$html = "<img src='...' />"; // This will not work

$html = '<img src="..." />'; // This will work

回答by 000

As things are working locally so you may try changing the image type from pngto jpgand check after modifying your code accordingly and uploading the jpgon the server.

由于事情在本地工作,因此您可以尝试将图像类型从png更改为jpg,并在相应地修改您的代码并将jpg上传到服务器上后进行检查。

回答by FreshFish

You can convert image type "jpg/png" to base64. <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgA...."/>this may help you !

您可以将图像类型“jpg/png”转换为 base64。 <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgA...."/>这可能会帮助你!

回答by user6295680

In my case IMG tag not working until I write full path to file

在我的情况下,IMG 标签在我写入文件的完整路径之前不起作用

Not working

不工作

<img src="/pdfrender/XXX.jpg" width="50" height="50">

Working (localhost example)

工作(本地主机示例)

<img src="http://site.local/pdfrender/XXX.jpg" width="50" height="50">

回答by Barrett

In my case, I tried every solution above to no avail. It turned out I was missing width and height attributes. Adding those in, and using a root path ended up working for me:

就我而言,我尝试了上述所有解决方案均无济于事。原来我缺少宽度和高度属性。添加这些并使用根路径最终对我有用:

<img src="/images/image.png" width="50" height="50"/>

<img src="/images/image.png" width="50" height="50"/>

回答by carla

I had to 'resave' the images:

我不得不“重新保存”图像:

$image = 'images/logo_example.png';    
imagepng(imagecreatefrompng($image),$image);

Then it worked.

然后它起作用了。

回答by Michael Sebastian

I implemented a str_replace for the image src, and that works ok now.

我为图像 src 实现了一个 str_replace,现在可以正常工作了。

$html = str_replace("../images", $_SERVER["DOCUMENT_ROOT"] . '/images', $html);