16

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.

hakre
  • 193,403
  • 52
  • 435
  • 836
jjwdesign
  • 3,272
  • 8
  • 41
  • 66

3 Answers3

26

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);
henna
  • 368
  • 3
  • 13
  • 2
    Unfortunately, I'm adding a content sensitive image (a signature) to the PDF document. I can not have the image pulled into the document with a path. – jjwdesign Mar 21 '12 at 15:12
  • even if image is generated on the fly, you can include in pdf. I have pdf in which some of images are always there and some just generated just before pdf is created and they are deleted once pdf is generated. I don't get you - "I can not have the image pulled into the document with a path." where image is exist then? – henna Mar 22 '12 at 01:42
  • 4
    It can not be a web based reference path. I am pulling in the image from a local file path. Relative to the bottom of the previous html write seems to be the best for my situation. – jjwdesign May 08 '12 at 18:38
  • When using tag - iss there a way to overflow image over the text or overflow images over each other? – Meglio Jun 29 '12 at 15:14
  • @henna @jjwdesign means pulling the image from a filepath located below the root of a website - invisible to browsers. I just tried to do the same but that doesn't work. It indeed seems that your approach works, but only for files which are accessible from the webroot and up. There are occasions in which you don't want to place info accessible from a webpage. Delicate info should not even be written there in temporary files. A shame because using the `` tag is way easier to use than tcpdf's `Image()` method. – Tobias Beuving Dec 28 '14 at 21:50
13

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 NOT at the web level.

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.

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:

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

$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'>");
Doug
  • 1,850
  • 23
  • 50
  • Or rewrite this to `SplFileObject`? Then there is no need for `explode()` only because of the file's extension. – Roland Nov 17 '17 at 12:25
  • Here, my PDF contains an `` tag so, I cannot do 2nd option. Still, I don't want TCPDF to appear in web statistics and it is silly, anyway. – Roland Nov 17 '17 at 12:29
  • You could use SplFileObject but it doesn't make it any cleaner. Added an SplFileObject example to answer. – Doug Jul 18 '19 at 16:19
  • 2
    Somehow some TCPDF versions need just the base64 string in the img src, prefixed with an "@". So: `$pdf->writeHTML("");` I just found this out through this answer: https://stackoverflow.com/a/54520065/3090890 (this was the only working solution in my case) – Piemol Dec 18 '19 at 12:06
  • 1
    You legend, you! – rockstardev Dec 10 '20 at 15:22
1

Answer is here

Please make sure the HTML attributes have double quotes.

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

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

$pdf->writeHTML($html, true, false, true, false, '');
Nuri Akman
  • 792
  • 3
  • 18
  • 41