how to increase width according to value count - php

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.

Related

Word wrap in a cell in FPDF using 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);

FPDF Cell with custom border

I wanted to show data with custom position. the currency is align left and the number align right.
So i'm trying to make a cell that looks like 1 cell but it's made of 2 cell .
Here is part of my code
$pdf->Cell(5, 10, ' $ ', 'L', 0, 'L');
$pdf->Cell(45, 10, $r_purchases_t['purchase_total'], 'R', 0, 'R');
It's result is this :
|$ 10,000.00|
Problem : It has no bottom border,how can i make custom cell border just left and bottom side ?
If possible, i want some code like $pdf->Cell(5, 10, ' $ ', 'L'||'B', 0, 'L');
SOLVED,
Apparently you can just combine border using , in border parameter.
So to add left and bottom border of the cell just using this code
$pdf->Cell(5, 10, ' $ ', 'L,B', 0, 'L');
$pdf->Cell(45, 10, ' $ '. $r_purchases_t['purchase_total'], 'L', 0, 'L');
Try This and adjust width of cell accordin to need.

sort in cakephp / fpdf

I have a small script created in CakePHP where I can generate quotes, however the quote items seem to be sorted in a non specific way on my PDF. What is the best way to have this sorted by ID in an ascending order ?
Find below my related code snippet which will list all items on my quote:
foreach ($items as $item) {
$itemSubTotal = $item['quantity'] * $item['unit_price'];
$discount_rate=$item['discount_rate'];
$unit_price=$item['unit_price'];
$subTotal += $itemSubTotal;
$itemDiscount=$itemSubTotal*$discount_rate/100;
$discount+=$itemDiscount;
//$itemTax = $itemSubTotal * ($item['tax_rate'] / 100);
$itemTax = ($itemSubTotal - $itemDiscount) * ($item['tax_rate'] / 100);
$tax += $itemTax;
$itemSubTotal = $itemSubTotal*((100-$discount_rate) / 100);
$itemSubTotal = number_format($itemSubTotal, 2, '.', ',');
$y+=5;
$pdf->setXY(5, $y);
$pdf->MultiCell(10, 5, $i++, 0, 'L');
$pdf->setXY(15, $y);
//$pdf->MultiCell(30, 5, $item['title'], 0, 'L');
$pdf->Cell(30, 5, $item['title'], 0, 2, 'L');
$pdf->setXY(45, $y);
//$pdf->MultiCell(80, 5, $item['details'], 0, 'L');
$pdf->Cell(30, 5, $item['details'], 0, 2, 'L');
$pdf->setXY(125, $y);
$pdf->MultiCell(20, 5, $item['quantity'], 0, 'R');
$pdf->setXY(145, $y);
$pdf->MultiCell(15, 5, number_format($unit_price, 2, '.', ','), 0, 'R');
$pdf->setXY(160, $y);
$pdf->MultiCell(20, 5, number_format($discount_rate, 2, '.', ','), 0, 'R');
$pdf->setXY(180, $y);
$pdf->MultiCell(25, 5, $itemSubTotal, 0, 'R');
}
Some expert help would be greatly appreciated, thank you
I have not tried this, but it should work. Have a look at cake 3 collections and specifically sortBy
If you wrap your $items in a collection, then sort them, then iterate through them you will achieve your goal.
Something like:
use Cake\Collection\Collection;
$collection = new Collection($items);
$sortedByField = $collection->sortBy('field')
If this doesn't work have a play with the other methods like groupBy

Fpdf Send To back Change order of Cell

How to send cell() to the back because it currently show to pdf based on order in the code. This is the example.
$pdf->SetXY(10, 20);
$pdf->SetFillColor(240);
$pdf->Cell(10, 10, '', 0, 1, 'L', true); // Show up 1st: Cell 1
$pdf->SetXY(10, 20);
$pdf-> SetFillColor(100);
$pdf->Cell(10, 10, '', 0, 1, 'L', true); // Show up 2nd: Cell 2
So, how can I set which Cell will come first/send to the back without follow the order?

CakePHP calculation not correct

I have a small script that generates quotes. I can add items with related discount or tax values, however the calculation does not seem to be correct as it's not subtracting the discount value. Based upon the screenshot I will attach the item subtotal should amount to 155,584.00
Please refer to my script below as well as a screenshot
foreach ($items as $item) {
$itemSubTotal = $item['quantity'] * $item['unit_price'];
$discount_rate=$item['discount_rate'];
$unit_price=$item['unit_price'];
$subTotal += $itemSubTotal;
$itemDiscount=$itemSubTotal*$discount_rate/100;
$discount+=$itemDiscount;
$itemTax = $itemSubTotal * ($item['tax_rate'] / 100);
$tax += $itemTax;
$itemSubTotal = number_format($itemSubTotal, 2, '.', ',');
$y+=5;
$pdf->setXY(5, $y);
$pdf->MultiCell(10, 5, $i++, 0, 'L');
$pdf->setXY(15, $y);
$pdf->Cell(30, 5, $item['title'], 0, 2, 'L');
$pdf->setXY(45, $y);
$pdf->Cell(30, 5, $item['details'], 0, 2, 'L');
$pdf->setXY(125, $y);
$pdf->MultiCell(20, 5, $item['quantity'], 0, 'R');
$pdf->setXY(145, $y);
$pdf->MultiCell(15, 5, number_format($unit_price, 2, '.', ','), 0, 'R');
$pdf->setXY(160, $y);
$pdf->MultiCell(20, 5, number_format($discount_rate, 2, '.', ','), 0, 'R');
$pdf->setXY(180, $y);
$pdf->MultiCell(25, 5, $itemSubTotal, 0, 'R');
How can I get this fixed? Some expert advise would be greatly appreciated
You haven't modified the $itemSubTotal variable with the discount. you should do this before printing it:
$itemSubTotal = $itemSubTotal*($discount_rate/100);
also, according to my calculation, the item subtotal would be 83,776.00

Categories