Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting an image with PHP and FPDF

Tags:

I'm trying to insert an image but do not want to specify the x and y coordinates. Is that possible?

$pdf->Image($image1, 5, 70, 33.78); 

I want to be able to specify the size (33.78) but not the x and y so that it moves based on the content.

$pdf->Write( 70, $reportTitle ); $pdf->Ln( 45 ); $pdf->SetFont( 'Arial', '', 12 ); $pdf->Write( 6, $reportSubtitle );  /**   Create product 1 **/ $pdf->Ln( 10 ); $pdf->SetFont( 'Arial', '', 12 ); $pdf->Write( 6, $prod1title ); $pdf->Ln( 30 ); $pdf->SetFont( 'Arial', '', 10 ); $pdf->Write( 5, $prod1sub ); $pdf->Ln( 30 ); $pdf->Image($image1, 5, 70, 33.78); 

The above is the code I use. If $reportSubtitle is two or three lines, it pushes $prod1title and $$prod1sub down, and inevitably under the image that is fixed. Is there no way to have the image act like the product title and subtitle and be pushed down too while still declaring the size?

like image 330
Carson Avatar asked Aug 12 '10 19:08

Carson


People also ask

How do you insert an image into FPDF?

$image1 = "img/products/image1. jpg"; Then ceate a cell, position it, then rather than setting where the image is, use the variable you created above with the following: $this->Cell( 40, 40, $pdf->Image($image1, $pdf->GetX(), $pdf->GetY(), 33.78), 0, 0, 'L', false );

What is use of FPDF in php?

FPDF is a PHP class which allows generating PDF files with PHP code. It is free to use and it does not require any API keys. FPDF stands for Free PDF. It means that any kind of modification can be done in PDF files.

How do you align images in FPDF?

You can set an X value to the pdf with determines the position on the X axis. $pdf->Cell(0,0,"Test",0,0,'R',true); $pdf->Ln(); $pdf->SetX(100); //The next cell will be set 100 units to the right $pdf->Cell(100,0,"Test",0,0,'R',true); $pdf->Ln();

How do I add a watermark to a photo in FPDF?

Put your watermark code in the Header() function as shown in this example. That way the watermark will be set on AddPage() , i.e. before the actual content is output, making the watermark appear in the "background".


1 Answers

I figured it out, and it's actually pretty straight forward.

Set your variable:

$image1 = "img/products/image1.jpg"; 

Then ceate a cell, position it, then rather than setting where the image is, use the variable you created above with the following:

$this->Cell( 40, 40, $pdf->Image($image1, $pdf->GetX(), $pdf->GetY(), 33.78), 0, 0, 'L', false ); 

Now the cell will move up and down with content if other cells around it move.

Hope this helps others in the same boat.

like image 143
Carson Avatar answered Sep 20 '22 06:09

Carson