0

I am using TCPDF's writeHTML method to insert HTML code in the PDF. If I insert an inline image (an image inside a paragraph) tag, it breaks the line. Look at the image below:

enter image description here

As you can see, the text on the right of the image is not on the same level as the text on the left of the image. I need both texts to be aligned. How do I fix this? Here is my PHP code:

$html = '<p>This is a test ';
$html .= '<img src="C:\\path_to_image\\Untitled.png"> ';
$html .= 'This is a test</p>';
$pdf->AddPage();
$pdf->writeHTML($html, true, false, true, false, '');
MrSmile
  • 1,217
  • 2
  • 12
  • 20
  • You can fix it by creating a table instead of p(paragraph) tag. – Azhy Jul 13 '18 at 20:06
  • @Azhy That fixes the problem with the line breaking but it creates 2 new problems. 1) The width of each "td" tags does not take the width of the child elements (the text), so you end up with weird spacing if the text is not long enough. 2) if the text on the left/right of the image is too large, only the height of the table will change and the document will look weird. How do I know when to cut the text and insert a td tag? – NewDeveloper123 Jul 16 '18 at 13:25
  • Try to align the tavle to center and write a big width size or just fix it using javascript or try to add a class of width:auto to all of (td)s, and please vote my comments if you get what did you want – Azhy Jul 16 '18 at 14:40

1 Answers1

0

Perhaps try applying an in-line style to the image like so:

$html = '<p>This is a test ';
$html .= '<img src="C:\\path_to_image\\Untitled.png" style="vertical-align: middle;"> ';
$html .= 'This is a test</p>';
$pdf->AddPage();
$pdf->writeHTML($html, true, false, true, false, '');

I added a style attribute style="vertical-align: middle;" to the <img> tag which instructs it to align itself to the center of it's parent <p>.

This would work in plain old HTML as per this image:

enter image description here

Please let me know if it works for you.

Simon K
  • 1,503
  • 1
  • 8
  • 10