Word wrap in a cell in FPDF using PHP? - php

I am trying to print some words in a cell but wordwrap in not working in a cell, what should i do?
$pdf->setXY(124, 36 + ($i * 5.1));
$pdf->SetFillColor(238, 236, 225);
$pdf->SetFont('Arial', '', 8);
$pdf->Cell(40, 6, $resultArrayIndex['pubtitle'], 0, 0, 'L', True);
Here is my output -
and here what i am trying to achieve - trying to wrap word within a cell. (Any help or hint is appreciated)
Edited after comments and sugggestions
After all u suggesting me use Multicell, I used it but it's still not helpful
not equal gap with each multicell and even sometimes it's uneven size text and gap
My code after your suggestion and i am using it in for-loop
$pdf->setXY(17, 36+($i * 6.9));
$pdf->SetFillColor(255,255,255);
$pdf->SetFont('Arial', '', 8);
$pdf->MultiCell(19, 4.6, formatPubDate($resultArrayIndex['pubdate']), 1);
$pdf->setXY(42, 36 + ($i * 6.9));
$pdf->SetFont('Arial', '', 8);
$pdf->MultiCell(50.5, 4.6, ($resultArrayIndex['title']), 1);
$pdf->setXY(124, 33.7 + ($i * 9.0));
$pdf->SetFont('Arial', '', 8);
$pdf-> MultiCell(27, 2.9 , $resultArrayIndex['pubtitle'],1);

Related

FPDF - PHP - Different styles on center cell

My problem is that I have a center text in a cell and I want the word "Client:" in bold and the rest in regular, like is centered I cant print "client:" first and after that print the name, neither use "write" function because is centered, please help.
$pdf->SetTextColor(102, 106, 117);
$pdf->SetFont('Arial', 'B', 15);
$pdf->Cell(626,25,"Client: ".$name,0,0,'C',0);
We have to calculate position of centered text like follows:
require("fpdf.php");
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetTextColor(102, 106, 117);
$fullCellWidth = $pdf->GetPageWidth();
$pdf->SetFont("Arial", "", 15);
$regularCell = "some name";
$regularWidth = $pdf->GetStringWidth($regularCell);
$pdf->SetFont("Arial", "B", 15);
$boldCell = "Client: ";
$boldWidth = $pdf->GetStringWidth($boldCell);
$centerIndentX = ($fullCellWidth - $boldWidth - $regularWidth) / 2;
$pdf->SetX($centerIndentX);
$pdf->Cell($boldWidth, 25, $boldCell, 0, 0, "L");
$pdf->SetX($centerIndentX + $boldWidth);
$pdf->SetFont("Arial", "", 15);
$pdf->Cell($regularWidth, 25, $regularCell, 0, 0, "L");
$pdf->Output();
The output PDF example - part of screenshot:

TCPDF Cell text uppercase and color change

I have an TCPDF file where i want to set 1 cell with fill-background color, and to change the text in that cell to Upper-Case and color to white
$pdf->SetFont($pdfFont, 'B', 10);
$pdf->SetFillColor(59,78,135);
$pdf->Cell(50, 6, Lang::trans('supportticketsclient'), 0, 1, 'L', true);
I know i have to add "strtoupper" for uppercase letters but don't know where, and SetTextColor or something similar to it, but when i set that
$pdf->SetTextColor(0,0,0);
My whole pdf color is changed.
you just need to use the same function to change the colour back to the original(or a new colour)
$pdf->SetFont($pdfFont, 'B', 10);
$pdf->SetFillColor(59,78,135);
$pdf->SetTextColor(0,0,0);
$pdf->Cell(50, 6, strtoupper(Lang::trans('supportticketsclient')), 0, 1, 'L', true);
$pdf->SetTextColor(255,255,255);//change back to black
I have the same problem on changing the text in a cell to Upper-Case..
What I did was convert it on my query
$appName = $row['appFname']." ".$row['appMname']. " ".$row['appLname'];
$appNameUPPER = strtoupper($appName);
then I used that variable on my cell
$pdf->Cell(179,26,''.$appNameUPPER.'', 'B','', 'C', '','','','','','B');
then it works! Try converting it on your query~
It's my first time to answer here hope it works on you (^_^)/

how to increase width according to value count

First of all i am really sorry for my english. I try to explain my wanted. I use fpdf to make dynamic invoice in pdf. I have more than one product to add one invoice and i must list them sub-bottom. But products counts change in all invoices. Some invoice has 5 products but some invoice has 2 products. My problem is i use code below but all lines overrided.
$invoices_query = mysql_query("SELECT * FROM invoice_bookings WHERE invoice_code = '$invoice_code'");
while ($invoices = mysql_fetch_array($invoices_query)) {
$customers_name = $invoices['customer_name'];
$pdf->Ln(0);
$pdf->Cell(21,123,$invoice_number,0,0,'L',0); // empty cell with left,top, and right borders
$pdf->Cell(30,123,$customers_name,0,0,'L',0);
$pdf->Cell(20,123,$date,0,0,'L',0);
$pdf->Cell(20,123,$time,0,0,'L',0);
$pdf->Cell(30,123,$suburb_from,0,0,'L',0);
$pdf->Cell(34,123,$suburb_to,0,0,'L',0);
$pdf->Cell(10,123,'% ' . $tax_percent,0,0,'L',0);
$pdf->Cell(25,123,'$ ' . $invoice_price,0,0,'R',0);
}
You can see value of margin. It's 123 as you can see. I must increase this value to 132 for example. And for other product 142 i must make. For example there are 5 products in this invoice i must make like this ;
first 123
second 133
third 143
fourth 153
fifth 163
but i really don't know how can i do this.
As this. Add a counter, initialize it, and always incrase with 10.
$cnt = 123; //Init the counter
while ($invoices = mysql_fetch_array($invoices_query)) {
$customers_name = $invoices['customer_name'];
$pdf->Ln(0);
$pdf->Cell(21, $cnt, $invoice_number, 0, 0, 'L', 0); // empty cell with left,top, and right borders
$pdf->Cell(30, $cnt, $customers_name, 0, 0, 'L', 0);
$pdf->Cell(20, $cnt, $date, 0, 0, 'L', 0);
$pdf->Cell(20, $cnt, $time, 0, 0, 'L', 0);
$pdf->Cell(30, $cnt, $suburb_from, 0, 0, 'L', 0);
$pdf->Cell(34, $cnt, $suburb_to, 0, 0, 'L', 0);
$pdf->Cell(10, $cnt, '% ' . $tax_percent, 0, 0, 'L', 0);
$pdf->Cell(25, $cnt, '$ ' . $invoice_price, 0, 0, 'R', 0);
$cnt = $cnt + 10; //Incrase it in the loop.
}
NOTE
Do not use mysql functions, they are deprecated. Use mysqli or PDO instead.
Avoid sql injections by escaping your variables comes from outside, or use prepared statements.

i want to use pdf function in php multicell in one line parallel cell text left top align

into cell tag how can i use text in left top if the text block is large.
there is the main problem in Multicell i can't use twomulticell parallel .
$pdf->MultiCell(60, 6, "".$row['particular'], 1, 'L', FALSE);
$pdf->Cell(40,50,"".$row['quantity'],1,0,"l");
The MultiCell method has no $ln parameter like the Cell method (Just a side note: Internally a MultiCell creates the lines by several Cell calls). If you need to stay at the same line with a multicell you have to do this with your own logic. E.g.:
$y = $pdf->GetY();
$x = $pdf->GetX();
$width = 60;
$pdf->MultiCell($width, 6, 'particular', 1, 'L', FALSE);
$pdf->SetXY($x + $width, $y);
$pdf->Cell(40,50, 'quantity', 1, 0, "l");

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