GhostCode
GhostCode

Reputation: 578

Adding inline image to text in FPDF

I currently have a PDF being generated with FPDF that pulls its data from a DB. Most of my data has blocks where images should be placed. For example: "This is a ((image)) test" where ((image)) will be replaced with an image.

I know we have the $pdf->Image() function in FPDF, but is there a way to perform a "search and replace" with all of those image blocks in FPDF so that my text will register with images inline the way they're supposed to be laid out in the DB data?

Hopefully I'm being clear enough. Any help is much appreciated!

Upvotes: 2

Views: 12664

Answers (2)

designil
designil

Reputation: 763

I successfully came up with the way to draw inline image in FPDF. What I did is copying FPDF->Image method and update PDF's current Y value with the image's height.

Here is the code: https://gist.github.com/woraperth/eea4b5e7d07b40cb46a13103603e2053#file-inlineimg-php

Usage example (change 100 to image's width)

$pdf->Cell( 40, 40, $pdf->InlineImage("ds.jpg", $pdf->GetX(), $pdf->GetY(), 100), 0, 0, 'L', false );
$pdf->Ln(20);
$pdf->Cell( 40, 40, $pdf->InlineImage("ds.jpg", $pdf->GetX(), $pdf->GetY(), 100), 0, 0, 'L', false );
$pdf->Ln(20);
$pdf->Cell( 40, 40, $pdf->InlineImage("ds.jpg", $pdf->GetX(), $pdf->GetY(), 100), 0, 0, 'L', false );

Hope it helps anyone who is looking for the answer :)

Upvotes: 1

Scott
Scott

Reputation: 3765

I recommend this FPDF Script which writes out standard HTML into the PDF being created:

http://fpdf.org/en/script/script42.php

First replace your ((image)) text with <img src="http://www.example.com/image.gif"> then execute:

 $pdf->WriteHTML("This is a <img src="http://www.example.com/image.gif"> test");

This will also allow you to handle other HTML formatting.

Upvotes: 0

Related Questions