jjwdesign jjwdesign - 1 year ago 74
PHP Question

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

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

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

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 Source

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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download