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

jjwdesign picture jjwdesign · Feb 27, 2012 · Viewed 44.6k times · Source

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".

$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.

Answer

henna picture henna · Mar 20, 2012

I am using html img tag and its working well.

$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);