Inserting an image with PHP and FPDF - php

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?

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.

You can use $pdf->GetX() and $pdf->GetY() to get current cooridnates and use them to insert image.
$pdf->Image($image1, 5, $pdf->GetY(), 33.78);
or even
$pdf->Image($image1, 5, null, 33.78);
(ALthough in first case you can add a number to create a bit of a space)
$pdf->Image($image1, 5, $pdf->GetY() + 5, 33.78);

$image="img_name.jpg";
$pdf =new FPDF();
$pdf-> AddPage();
$pdf-> SetFont("Arial","B",10);
$pdf-> Image('profileimage/'.$image,100,15,35,35);

Please note that you should not use any png when you are testing this , first work with jpg .
$myImage = "images/logos/mylogo.jpg"; // this is where you get your Image
$pdf->Image($myImage, 5, $pdf->GetY(), 33.78);

// Image URL
$url = 'img/img.png';
// Place the image in the pdf document
$pdf->Cell(30, 30, $pdf => Image($url, 5, $pdf => GetY(), 93.78), 0, 0, 'L', false);
The 93.78 is the size of the image.
5 is the position from the left side.

You can't treat a PDF like an HTML document. Images can't "float" within a document and have things flow around them, or flow with surrounding text. FPDF allows you to embed html in a text block, but only because it parses the tags and replaces <i> and <b> and so on with Postscript equivalent commands. It's not smart enough to dynamically place an image.
In other words, you have to specify coordinates (and if you don't, the current location's coordinates will be used anyways).

Related

FPDF align text LEFT, Center and Right

I've have three cells and i'am trying to align the text to Left, Center and Right.
function Footer()
{
$this->SetY( -15 );
$this->SetFont( 'Arial', '', 10 );
$this->Cell(0,10,'Left text',0,0,'L');
$this->Cell(0,10,'Center text:',0,0,'C');
$this->Cell( 0, 10, 'Right text', 0, 0, 'R' );
}
When I ouput my PDF file, center text get automatically aligned right.
This is how it looks:
Can somebody tell me what I'm doing wrong here and how I can fix this issue?
The new position after a Cell call will be set to the right of each cell if you set the ln-parameter of the Cell method to 0. You have to reset the x-coordinate before the last 2 Cell calls:
class Pdf extends FPDF {
...
function Footer()
{
$this->SetY( -15 );
$this->SetFont( 'Arial', '', 10 );
$this->Cell(0,10,'Left text',0,0,'L');
$this->SetX($this->lMargin);
$this->Cell(0,10,'Center text:',0,0,'C');
$this->SetX($this->lMargin);
$this->Cell( 0, 10, 'Right text', 0, 0, 'R' );
}
}
Though Jan Slabon's answer was really good I still had issues with the center not being exactly centered on my page, maybe we have different versions of the library and that's what accounts for the slight differences, for instance he uses lMargin and on some versions that's not available. Anyway, the way it worked for me is this:
$pdf = new tFPDF\PDF();
//it helps out to add margin to the document first
$pdf->setMargins(23, 44, 11.7);
$pdf->AddPage();
//this was a special font I used
$pdf->AddFont('FuturaMed','','AIGFutura-Medium.ttf',true);
$pdf->SetFont('FuturaMed','',16);
$nombre = "NAME OF PERSON";
$apellido = "LASTNAME OF PERSON";
$pos = 10;
//adding XY as well helped me, for some reaons without it again it wasn't entirely centered
$pdf->SetXY(0, 10);
//with SetX I use numbers instead of lMargin, and I also use half of the size I added as margin for the page when I did SetMargins
$pdf->SetX(11.5);
$pdf->Cell(0,$pos,$nombre,0,0,'C');
$pdf->SetX(11.5);
//$pdf->SetFont('FuturaMed','',12);
$pos = $pos + 10;
$pdf->Cell(0,$pos,$apellido,0,0,'C');
$pdf->Output('example.pdf', 'F');
Seems that there is a parameter for this, so ('R' for right-aligning, 'C' for centering):
$pdf->Cell(0, 10, "Some text", 0, true, 'R');
will right align text.
Also, notice that the first parameter ('width') is zero, so cell has 100% width.

Image to PDF with tcpdf

Hy
I'm trying to convert an image (both png or jpeg) to pdf and then adding crop marks using tcpdf (no limitation on the library, it just seemed the best to me, no problems in changing if would solve the problem).
the best case for me is: I have an input image, I give it to some function of tcpdf, that converts it into a pdf, add the bookmarks in the angles (top-left, top-right, bottom-left, bottom-right) and save the pdf without nowing anything about the sizes of the image...
I tried looking around but coudn't find anything without passing the size of the images to tcpdf, this is what I came up with:
$pagelayout = array('706', '606'); // or array($height, $width)
$pdf = new TCPDF('', 'pt', $pagelayout, true, 'UTF-8', false);
// set document information
$pdf->SetAuthor('JOIN SRL');
// add a page
$pdf->AddPage();
$pdf->Image($input, 0, 0, '', '', 'JPG', 'http://www.tcpdf.org', '', true, 300, '', false, false, 1, false, false, false);
$pdf->Image($input);
$pdf->cropMark('', '', 10, 10, 'TL');
$pdf->cropMark('', '', 10, 10, 'TR');
$pdf->cropMark('', '', 10, 10, 'BL');
$pdf->cropMark('', '', 10, 10, 'BR');
$pdf->Output($output, 'F');
as you can see I had to pass the image size to tcpdf, but this is just a test, I would like to get rid of this informations in this peace of code...
And this dosn't work either because the pdf page comes otu bigger than the image, and hte crop marks don't get show( I think this is quite obvious, I tried not giving coordinates hoping that they would automatically set in the angles of the page, but no luck) .
Does someone have any ideas?
thank's
EDIT
I managed to solve nearly everything. could be fine like this( the code follows) but I wanted to know if there is a way to do the same thing without knowing the image size.
$pdf->setMargins(0, 0, 0);
$pdf->SetAutoPageBreak(false, 0);
$pdf->AddPage();
$pdf->Image($input);
$width = $pdf->getPageWidth();
$height = $pdf->getPageHeight();
$pdf->cropMark(5, 5, 5, 5, 'TL', array(255, 0, 0));
$pdf->cropMark($width - 5, 5, 5, 5, 'TR', array(255, 0, 0));
$pdf->cropMark(5, $height - 5, 5, 5, 'BL', array(255, 0, 0));
$pdf->cropMark($width - 5, $height - 5, 5, 5, 'BR', array(255, 0, 0));
$pdf->Output($output, 'F');
thank's

FPDF - Drawing a line that's centred across the width

Im trying to produce a PDF document using the PHP FPDF library,
Im trying to draw a line horizontal over the page which is indented the same amount both on the left and right sides.
Im having real difficulty trying to accomplish this.
My code is as follows, any help would be greatly appreciated.
$pdf = new FPDF( 'P', 'mm', 'A4' );
$pdf->AddPage();
$pdf->SetDisplayMode(real,'default');
$pdf->SetFillColor(0,0,0);
$pdf->SetFont('Arial','B',16);
$pdf->Image('logo.jpg',20,10,50,33.3);
$pdf->SetDrawColor(188,188,188);
$pdf->Line(20,45,150,45);
Given a portrait, A4 page is 210mm wide, a simple bit of maths should help you resolve this:
$pdf->Line(20, 45, 210-20, 45); // 20mm from each edge
$pdf->Line(50, 45, 210-50, 45); // 50mm from each edge
This is given that your declaration is as you stated in your original question:
$pdf = new FPDF( 'P', 'mm', 'A4' ); // A4, portrait, measurements in mm.
Use the following -
$pdf = new PDF('P','mm','A4'); //Set PDF as Potrait
$pdf->Ln(4); //Break
$pdf->Line(startpoint, 45, endpoint-50, 45); //Set the line
$pdf->Ln(4); //Break

Captcha image generating problems when using font PHP

its working perfect, until i try to use a font for text.
This is my code.. The 3er line commented now is the one that works fine... but in this example im trying to replace this line with imagettftext.. not lucky.
Whats my error?
$newImage = imagecreatefromjpeg( "orsil_secure.jpg" );
$txtColor = imagecolorallocate($newImage, 0, 0, 0);
//imagestring($newImage, 5, 10, 27, $ranStr, $txtColor);
imagettftext($newImage, 5, 10, 27, $txtColor, $font_path, $ranStr);
header( "Content-type: image/jpeg" );
imagejpeg($newImage);
Oh yes in the previous lines is the route to the font here:
// random number
$ranStr = md5(microtime());
$ranStr = substr($ranStr, 0, 6);
// Set Path to Font File
$font_path = 'captcha.TTF';
It looks like you have the wrong parameters in your call to imagettftext.
Presumably, the 5 corresponds to the font used in imagestring, but has no place in imagettftext. You also need to specify the size and angle.
For example:
imagettftext($newImage, 12, 45, 10, 27, $txtColor, $font_path, $ranStr);
^^ ^^
|| ||
|| ------ angle
----------- size
In this example:
size=12px or 12pt, depending on whether you're using GD1 or GD2
angle = 45°
Obviously, you'll want to use your own values here.
If it still isn't working, then it's likely that the path to your font file is wrong. Check to see if it's in the same folder as the PHP script.
I think you are missing a parameter in your imagettftext.
If you look at http://php.net/manual/en/function.imagettftext.php it has size, angle, x and y.
You are missing one of the 4 (im guessing the angle).
So it needs to be something like:
imagettftext($newImage, 5, 0, 10, 27, $txtColor, $font_path, $ranStr);
Where 0 is for angle.

How to add a stroke to only some characters in a string using PHP ImageMagick?

Say I have the following code:
$draw = new ImagickDraw(); // prep text
$draw->setFillColor('#00ff00');
$draw->setFontSize(12);
$draw->setStrokeWidth(4);
$draw->setStrokeColor(new ImagickPixel('#ff0000'));
// etc.
$image = new Imagick(); // prep image
// etc.
// add text to image
$image->annotateImage($draw, 10, 10, 0, 'Hello, World!');
And let's assume that it works (which it does - I've just cut it down here). Is there a simple way for me to, say, change the stroke width on a per-character basis?
I'm looking into using something like $image->queryFontMetrics($text, 'H')) but wondered if there was a simpler way.
Thanks!
$draw = new ImagickDraw(); // prep text
$draw->setFillColor('#00ff00');
$draw->setFontSize(12);
$draw->setStrokeColor(new ImagickPixel('#ff0000'));
// etc.
$image = new Imagick(); // prep image
// etc.
// add text to image
$draw->setStrokeWidth(4);
$image->annotateImage($draw, 10, 10, 0, 'He');
$draw->setStrokeWidth(5);
$image->annotateImage($draw, 12, 10, 0, 'llo, ');
$draw->setStrokeWidth(6);
$image->annotateImage($draw, 15, 10, 0, 'Wor');
$draw->setStrokeWidth(7);
$image->annotateImage($draw, 18, 10, 0, 'ld!');
Would something like that work or would that be too clunky? It could work if the string was always the same and you figured out what the $x dimensions were correctly (the 2nd parameter in the annotateImage() function)

Categories