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

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

Related

SetDrawColor() is behaving like SetFillColor()

SetDrawColor() supposed to color Cell frame but instead another color is rendering as background color. This is the whole code :-
$pdf = new FPDF();
$pdf->SetTextColor(103, 58, 183);
$pdf->SetDrawColor(0, 80, 180);
// Fourth Page --
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(0, 10, "Hello Santanu", 1, 1, 'C', true);
$pdf->Output('helo.pdf', 'D');
You should try using SetFillColor before drawing any cell because it's taking black as default.
$pdf->SetFillColor(0, 0, 0);

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.

mPDF importing page from another document

I'm trying to create PHP document in PHP using mPDF library. I want to import the last page from another PDF document. According to document I use, the imported page is too small (it's on 1/3 of the page) or too big (I see only part of the page). Is there any way to make it fit?
My code:
$mpdf = new mPDF('', '', 0, '', 0, 0, 0, 0, 0, 0, 'L');
$mpdf->WriteHTML($html);
$mpdf->AddPage();
$mpdf->SetImportUse();
$pagecount = $mpdf->SetSourceFile('application/views/default/pdf/nabidka-design.pdf');
$tplId = $mpdf->ImportPage($pagecount);
$mpdf->UseTemplate($tplId);
$mpdf->Output();
Thanks for help
You have to set page format for output PDF file like this:
$mpdf = new mPDF('utf-8', 'A4', '8', '', 10, 10, 7, 7, 10, 10);
Change 'A4' on your PDF source file format.

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)

Inserting an image with PHP and FPDF

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

Categories