php TCPDF:如何将图像放入 HTML 块中?

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

TCPDF: How can I place an image into an HTML block?

phppdf-generationtcpdf

提问by jjwdesign

I've been working with TCPDF for a few months now; off and on. It's worked fairly well for most of my HTML templates, but I've always had problems placing images into the PDF's. The images are usually placed into the body, not the header. My placement is either a fixed position from the top-left corner or it is relative to the bottom of the document. In either case, I've had issues. When the text changes in the HTML, I have to reposition the image. Multiple column tables can make things even more difficult. Note: "class pdf extends TCPDF".

我已经和 TCPDF 一起工作了几个月了;断断续续地。它对我的大多数 HTML 模板都运行良好,但我在将图像放入 PDF 时总是遇到问题。图像通常放置在正文中,而不是标题中。我的位置要么是左上角的固定位置,要么是相对于文档底部的位置。无论哪种情况,我都遇到了问题。当 HTML 中的文本更改时,我必须重新定位图像。多列表会使事情变得更加困难。注意:“类 pdf 扩展了 TCPPDF”。

$this->pdf->AddPage();
$this->pdf->writeHTML($pdf_html);
$cur_page = $this->pdf->getPage();
$x_pos = $this->pdf->GetX();
$y_pos = $this->pdf->GetY();
// Place image relative to end of HTML
$this->pdf->SetXY($x_pos, $y_pos - 54);
$this->pdf->Image('myimage.png');

Does anyone know a fool-proof way of placing an Image into a PDF that is generated from HTML. I thought about splitting the HTML into two parts, but I'm not sure if it would work well either.

有谁知道将图像放入从 HTML 生成的 PDF 的万无一失的方法。我想将 HTML 分成两部分,但我不确定它是否也能正常工作。

回答by henna

I am using html img tag and its working well.

我正在使用 html img 标签并且它运行良好。

$toolcopy = ' my content <br>';
$toolcopy .= '<img src="/images/logo.jpg"  width="50" height="50">';
$toolcopy .= '<br> other content';

$pdf->writeHTML($toolcopy, true, 0, true, 0);

回答by Doug

Sorry, I know you've got an accepted answer. However, it does not appear to actually answer your question, with regards to an image that is NOTat the web level.

抱歉,我知道你已经得到了可接受的答案。但是,对于Web 级别的图像,它似乎并没有真正回答您的问题。

Have you considered using file_get_contents(); and the simple rendering the base_64 string. This way you can use an image from any level, without worrying about it being publicly accessible.

您是否考虑过使用 file_get_contents(); 以及简单渲染 base_64 字符串。通过这种方式,您可以使用任何级别的图像,而不必担心它是否可以公开访问。

E.g:

例如:

$imageLocation = '/var/www/html/image.png';
$ext = end(explode(".", $imageLocation);
$image = base64_encode(file_get_contents($imageLocation));
$pdf->writeHTML("<img src='data:image/$ext;base64,$image'>");

Or, without relying on the HTML parser. Which from experience slows rendering of the resulting PDF by far to much you could use:

或者,不依赖 HTML 解析器。从经验来看,您可以使用多少会减慢生成的 PDF 的渲染速度:

$image = file_get_contents('/var/www/html/image.png');
$pdf->Image('@'.$image);


Edit

编辑

For the sake of completeness, and in response to Roland. You could certainly use SplFileObject.

为了完整起见,并回应罗兰。您当然可以使用 SplFileObject。

$image = new SplFileObject('/var/www/html/image.png', 'r');
$imageContents = $image->fread($image->getSize());
$imageExtension = $image->getExtension();
$pdf->writeHTML("<img src='data:image/$imageExtension;base64,$imageContents'>");

回答by Nuri Akman

Answer is here

答案在这里

Please make sure the HTML attributes have double quotes.

请确保 HTML 属性有双引号。

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

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

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