Jump to content

generating pdf dynamically through fpdf


mythri

Recommended Posts

Hello,

 

I am trying to generate pdf dynamically using fpdf. But i am getting it very difficult. Only half of the result i could get. I want to generate my pdf like this

 

post-168283-0-73258500-1423301482_thumb.jpg

 

And now i am getting like this

 

post-168283-0-94491100-1423301518_thumb.jpg

 

Here is the code for pdf

<?php


require('../fpdf/fpdf.php');
error_reporting(-1);


$id = $_GET['order_id'];
$db = new mysqli('localhost','root','','dbnme'); // use your credentials

class INVPDF extends FPDF
{
    var $id;
    var $today;
    var $widths;
    var $heads;
    var $aligns;
    var $formats;
    var $db;
    var $invTotal = 0;
   



       
        function INVPDF($invno, $db)
        {
            parent::fpdf();
            $this->id = $invno;
            $this->db = $db;
            $this->today = date('jS M Y');
            $this->heads = array('Item', 'UOM', 'Price', 'Qty', 'Disc %', 'Tax', 'Frt', 'Total' );
            $this->widths = array (45, 15, 35, 15, 15, 20, 25, 30);
            $this->aligns = array ('L','C','L','C','C','R','C', 'C');
            $this->formats = array (0,0,0,0,0,1,0,0);
        }

        //Page header
        function Header()
        {        
                //Arial bold 15
                //Title
                include("../connect.php");




                $ss1 = "select orders.sales_order_id, orders.company_id, lead_address.address, lead_address.address_category, lead_address.country, lead_address.state, lead_address.company_id, lead_address.city,lead_address.pin from orders INNER JOIN lead_address ON orders.company_id=lead_address.company_id where lead_address.address_category='Billing' AND orders.sales_order_id='".$_GET['order_id']."'";
                $mq1 = mysql_query($ss1) or die(mysql_error());
                $rr1 = mysql_fetch_array($mq1);

                $billing = $rr1['address'];
                list($line1, $line2, $line3) = explode(',',$billing);
                $country = $rr1['country'];
                $state = $rr1['state'];
                $city = $rr1['city'];
                $pin = $rr1['pin'];
                //list($line1, $line2, $country, $state, $city, $pin) = explode(',',$address);
                $ss2 = "select orders.sales_order_id, orders.company_id, lead_address.address, lead_address.address_category, lead_address.country, lead_address.state, lead_address.company_id, lead_address.city,lead_address.pin from orders INNER JOIN lead_address ON orders.company_id=lead_address.company_id where lead_address.address_category='Shipping' AND orders.sales_order_id='".$_GET['order_id']."'";

                $mq2 = mysql_query($ss2) or die(mysql_error());
                $rr2 = mysql_fetch_array($mq2);

                $shipping = $rr2['address'];

                $country1 = $rr2['country'];
                $state1 = $rr2['state'];
                $city1 = $rr2['city'];
                $pin1 = $rr2['pin'];

                $this->setXY(10,20);
               // $this->Cell(0,10,'Quotation '.$this->id,0,2,'L');
                $this->Image('logo.png',20,6,15);
                $this->setXY(12,20);
                //
                $this->SetFont('OpenSans','',7);
                $this->Cell(0,10,'Company Name. ',0,2,'L');
                $this->Cell(0,0,'Address1, address2',0,2,'L');
                $this->Cell(0,8,'city, stte',0,2,'L');

                $this->Cell(0,2,'sales@company.com',0,2,'L');
                //$this->Image('images/logo.png',10,6,60);

                $this->SetFont('OpenSans','',7);
                $this->setXY(12,50);
                $this->Cell(0,-2,'Shipping Address',$this->id,0,2,0,'L');
                $this->SetFont('OpenSans','',7);

                $this->setXY(12,52);
                $this ->MultiCell(57,22,'', 'LRTB', 'L', 1);
                //$this->Cell(0,8,$name,$this->id,0,2,0,'L');
                $this->setXY(12,55);
                $this->Cell(0,-1,$shipping,$this->id,0,2,0,'L');



                $this->setXY(12,53);
                $this->Cell(0,10,$country." , ".$state,$this->id,0,2,0,'L');

                $this->setXY(12,52);
                $this->Cell(0,20,$city." , ".$pin,$this->id,0,2,0,'L');

                $this->SetFont('OpenSans','',7);
                $this->setXY(140,52);
                $this ->MultiCell(57,22,'', 'LRTB', 'L', 1);

                $this->setXY(140,34);
                $this->Cell(0,30,'Billing Address',$this->id,0,2,0,'L');

                $this->SetFont('OpenSans','',7);
                $this->setXY(140,35);
                $this->Cell(0,40,$line1." , ".$line2, $this->id,0,2,0,'L');

                $this->setXY(140,35);
                $this->Cell(0,49,$line3, $this->id,0,2,0,'L');

                $this->setXY(140,33);
                $this->Cell(0,62,$city1." , ".$pin1,$this->id,0,2,0,'L');

                $this->setXY(140,33);
               $this->Cell(0,70,$country1." , ".$state1,$this->id,0,2,0,'L');

                      $this->SetFont('OpenSans','',7);
                $this->setXY(10,5);
                $this->Cell(0,10,'QUOTATION:  '.$this->id,0,2,'R');



                $this->SetFont('OpenSans','',7);
                $until = date ('jS F Y', strtotime($this->today));
                $this->Cell(0,0,'Date: '.$until,0,2,'R');

                $this->SetFont('OpenSans','',7);
               $this->Cell(0,10,'Due Date: Due on receipt',0,2,'R');


                //Line break
                $this->Ln(60);
                for ($i=0; $i<9; $i++)
                {
                    $this->Cell ($this->widths[$i], 8, $this->heads[$i], 1, 0, 'C', 1);
                }
                $this->Ln(;
        }

        //Page footer
       function Footer()
        {

            # $this->SetY(-50);          // Uncomment to position at 5 cm from bottom
            //Arial italic 8
            $this->SetFont('OpenSans','',;
            $w = array_sum(array_slice($this->widths,0,3));
            $this->Cell($w,12,'',1);
            $this->Cell($this->widths[3],12,'',1,0,'C');

            $this->setLeftMargin(45 + array_sum(array_slice($this->widths,0,4)));

            $this->Cell($this->widths[5],4,'Sub Total',1);
            $this->Cell($this->widths[6],4,number_format($this->invTotal,2),1,0,'R');
            $this->Cell($this->widths[7],4,'USD',1,1,'C');

            $this->Cell($this->widths[5],4,'Tax',1);
            $this->Cell($this->widths[6],4,number_format('Tax amount should come',2),1,0,'R');
            $this->Cell($this->widths[7],4,'INR',1,1,'C');

             $this->Cell($this->widths[5],4,'Freight',1);
            $this->Cell($this->widths[6],4,number_format('Freight amount',2),1,0,'R');
            $this->Cell($this->widths[7],4,'INR',1,1,'C');

            $this->setLeftMargin(10);

            $this->Cell($this->widths[5],4,'Total',1);
            $this->Cell($this->widths[6],4,number_format('Overall Toatl',2),1,0,'R');
            $this->Cell($this->widths[7],4,'INR',1,1,'C');



            $this->SetFont('OpenSans','',6);
            $this->Cell($w,10,'',0,0,'L');
            $this->Cell(30,4,'Private Limited Company - TIN: 345sddd - PAN: sf43534',0,0,'C');
            //$this->Cell($w,10,'');
            $this->Cell(-30,12,'SRVC TAX: gddddddddddd - CIN: sdgdgdgdfgfdgfg',0,0,'C');
            $this->Cell(30,20,'This document has been electronically generated and requires no physical signature or stamp.',0,0,'C');



        }

        function makeInvoice()
          {


            $sql =  "select before_order_line_items.item, before_order_line_items.description, before_order_line_items.uom, before_order_line_items.selling_price, before_order_line_items.quantity, before_order_line_items.discount, before_order_line_items.tax, before_order_line_items.freight, before_order_line_items.tax_amount, before_order_line_items.total, items.name as iname, taxes.tax_id, taxes.name as tname, taxes.rate from before_order_line_items inner join items on before_order_line_items.item=items.item_id inner join taxes on before_order_line_items.tax=taxes.tax_id where before_order_line_items.sales_order_id = '".$_GET['order_id']."' ";

            //echo $sql;    

            $res = $this->db->query($sql) or die($this->db->error);
            $this->SetFont('OpenSans','',;
            if ($res->num_rows > 0)
            {

                while ($r = $res->fetch_row())
                {

                    $this->invTotal += $r[10];


                    foreach ($r as $c => $value) {
                        //echo $value;
                       if ($this->formats[$c]) {
                           $value = number_format($value);
                          // echo $value;

                        }
                        $this->Cell($this->widths[$c],10,$value,'LR',0, $this->aligns[$c]);


                    }
                    $this->Ln();
                    //$amount = number_format($amount+$value);

                }
            }            
        }

} # invpdf class

$invno = $_GET['order_id'];
//Instantiation of inherited class
$pdf = new INVPDF($invno, $db);

$pdf->Open();
$pdf->AliasNbPages();
$pdf->setLeftMargin(10);
$pdf->setRightMargin(10);
$pdf->AddFont('OpenSans','','Opensans-Regular.php');
$pdf->SetFont('OpenSans','',7);
$pdf->SetDrawColor(102);
$pdf->SetFillColor(220);
$pdf->AddPage();
$pdf->makeInvoice();
$pdf->Output();
?>
 

Can somebody please help me how to do it? or is there any example code for something similar to this.

Thanks

Edited by mac_gyver
extra code removed
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.