michelle1404 Posted June 12, 2014 Share Posted June 12, 2014 Hello, I am trying to print the invoice in pdf, using fpdf. i am not getting how to do it. I am able to display line items , but its not in proper format. I tried changing X, Y positions, but its not going into my head . Also i want to display Logo and address in left side, invoice number and order date in right side. Here is the code i have done so far... Please please help me in this <?php require('includes/fpdf/fpdf.php'); //Connect to your database include("connect/DB_Connect.php"); $id = $_GET['id']; //Select the Products you want to show in your PDF file $result=mysql_query("select invoice_no, order_no, order_date, completion_date, artwork, customer_po, order_quantity, comments, order_amount from invoice where invoice_no='".$id."' ") or die(mysql_error()); $number_of_products = mysql_numrows($result); //Initialize the 3 columns and the total $column_order_date = ""; $column_order_no = ""; $column_artwork = ""; $column_cpo = ""; $column_order_quantity = ""; $column_order_amount = ""; $column_comments = ""; $total = 0; //For each row, add the field to the corresponding column while($row = mysql_fetch_array($result)) { $order_date = $row["order_date"]; $order_no = $row["order_no"]; $artwork = $row['artwork']; $cpo = $row['customer_po']; $oquantity = $row['order_quantity']; $oamount = $row['order_amount']; $comments = $row['comments']; //$price_to_show = number_format($row["Price"],',','.','.'); $column_order_date = $column_order_date.$order_date."\n"; $column_order_no = $column_order_no.$order_no."\n"; $column_artwork = $column_artwork.$artwork."\n"; $column_cpo = $column_cpo.$cpo."\n"; $column_order_quantity = $column_order_quantity.$oquantity."\n"; $column_order_amount = $column_order_amount.$oamount."\n"; $column_comments = $column_comments.$comments."\n"; //Sum all the Prices (TOTAL) $total = $total+$oamount; } mysql_close(); //Create a new PDF file $pdf=new FPDF(); $pdf->AddPage(); //Fields Name position $Y_Fields_Name_position = 20; //Table position, under Fields Name $Y_Table_Position = 26; //First create each Field Name //Gray color filling each Field Name box $pdf->SetFillColor(232,232,232); //Bold Font for Field Name $pdf->SetFont('Arial','B',12); $pdf->SetY($Y_Fields_Name_position); $pdf->SetX(5); $pdf->Cell(25,6,'Order Date',1,0,'L',1); $pdf->SetX(30); $pdf->Cell(50,6,'Graphixide Order No',1,0,'L',1); $pdf->SetX(75); $pdf->Cell(40,6,'Artwork',1,0,'L',1); $pdf->SetX(110); $pdf->Cell(20,6,'PO #',1,0,'L',1); $pdf->SetX(130); $pdf->Cell(20,6,'Quantity',1,0,'L',1); $pdf->SetX(150); $pdf->Cell(25,6,'Amount',1,0,'L',1); $pdf->SetX(175); $pdf->Cell(25,6,'Comments',1,0,'L',1); //Now show the 3 columns $pdf->SetFont('Arial','',12); $pdf->SetY($Y_Table_Position); $pdf->SetX(5); $pdf->MultiCell(25,6,$column_order_date,1); $pdf->SetY($Y_Table_Position); $pdf->SetX(30); $pdf->MultiCell(80,6,$column_order_no,1); $pdf->SetY($Y_Table_Position); $pdf->SetX(75); $pdf->MultiCell(40,6,$column_artwork,1); $pdf->SetY($Y_Table_Position); $pdf->SetX(110); $pdf->MultiCell(20,9,$column_cpo,1); $pdf->SetY($Y_Table_Position); $pdf->SetX(130); $pdf->MultiCell(20,6,$column_order_quantity,1); $pdf->SetY($Y_Table_Position); $pdf->SetX(150); $pdf->MultiCell(75,6,$column_order_amount,1); $pdf->SetY($Y_Table_Position); $pdf->SetX(175); $pdf->MultiCell(25,10,$column_comments,1); $pdf->SetY($Y_Table_Position); $pdf->SetX(150); $pdf->MultiCell(130,6,'$ '.$total,1,'R'); //Create lines (boxes) for each ROW (Product) //If you don't use the following code, you don't create the lines separating each row $i = 0; while ($i < $number_of_products) { $pdf->SetX(45); $pdf->MultiCell(120,6,'',1); $i = $i +1; } $pdf->Output(); ?> Here is the output of this code If i use headers and footers like this class PDF extends FPDF { function Header() { // Logo $this->Image('images/logo.png',10,6,30); // Arial bold 15 $this->SetFont('Arial','B',15); // Move to the right $this->Cell(80); // Title $this->Cell(30,10,'Title',1,0,'C'); // Line break $this->Ln(20); } // Page footer function Footer() { // Position at 1.5 cm from bottom $this->SetY(-15); // Arial italic 8 $this->SetFont('Arial','I',; // Page number $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C'); } } Only logo displays, not any contents, Quote Link to comment https://forums.phpfreaks.com/topic/289124-printing-pdf-in-php-using-fpdf/ Share on other sites More sharing options...
ginerjm Posted June 12, 2014 Share Posted June 12, 2014 FPDF is really not that hard. Try to design your document in terms of the rows and columns instead of absolute coordinates, except where you want to do something like a logo in a corner or something like that. Picture your line items as a set of columns. When you call Cell for each data item, specify the width of that column regardless of the size of the actual data being output. Of course you need to consider the data size when you choose the column width. Do not use SetX for every output after you have done this - simply let fpdf handle the positioning and only set the 'ln' parm to '1' when you are done with a line. Think of designing a spreadsheet. What I do is set up some php vars for each column size: ( I use inches - you seem to be using mm) $lnht = .2; $col1 = 1.5; $col2 = .5; $col3 = 1.5; ... ... Then my output lines look like: $pdf->Cell($col1,$lnht,$col1_value,0,0,'L'); $pdf->Cell($col2,$lnht,$col2_value,0,0,'L'); $pdf->Cell($col3,$lnht,$col3_value,0,1,'L'); Note the last Cell call triggers a new line with the '1' value.I only use setx, sety for special documents layout. For straight up forms with simple rows and columns this works great for me. Keep playing - it'll come to you. Quote Link to comment https://forums.phpfreaks.com/topic/289124-printing-pdf-in-php-using-fpdf/#findComment-1482533 Share on other sites More sharing options...
Barand Posted June 12, 2014 Share Posted June 12, 2014 Also, select only the columns you want to output in the query and in the order you want them to appear. That way it's a simple loop to output the cell values EG $db = new mysqli(HOST,USERNAME,PASSWORD,'test'); // use your credentials class INVPDF extends FPDF { var $id; var $today; var $widths; var $heads; var $aligns; var $formats; var $db; //constructor function INVPDF($invno, $db) { parent::fpdf(); $this->id = $invno; $this->db = $db; $this->today = date('jS M Y'); $this->heads = array('Order Date', 'Graphixide Order No', 'Artwork', 'PO #', 'Quantity', 'Amount', 'Comments'); $this->widths = array (25, 40, 40, 20, 20, 25, 25); $this->aligns = array ('C','C','L','C','C','R','L'); $this->formats = array (0,0,0,0,0,1,0); } //Page header function Header() { //Arial bold 15 //Title $this->SetFont('Arial','B',15); $this->setXY(35,20); $this->Cell(0,10,'INVOICE '.$this->id,0,2,'R'); $this->SetFont('Arial','B',10); $until = date ('jS F Y', strtotime($this->today)); $this->Cell(0,4,$until,0,2,'R'); //Line break $this->Ln(5); for ($i=0; $i<7; $i++) { $this->Cell ($this->widths[$i], 8, $this->heads[$i], 1, 0, 'C', 1); } $this->Ln(; } //Page footer function Footer() { //Position at 1.5 cm from bottom $this->SetY(-15); //Arial italic 8 $this->SetFont('Arial','I',9); //Page number $this->Cell(0,4,$this->PageNo().' / {nb}',0,0,'R'); } function makeInvoice() { $sql = "SELECT DATE_FORMAT(order_date, '%d-%m-%Y') as odate , order_no , artwork , customer_po , order_quantity , order_amount , comments FROM invoice WHERE invoice_no={$this->id} "; $res = $this->db->query($sql) or die($this->db->error); $this->SetFont('Arial','',9); if ($res->num_rows > 0) { while ($r = $res->fetch_row()) { foreach ($r as $c => $value) { if ($this->formats[$c]) { $value = number_format($value, 2); } $this->Cell($this->widths[$c],5,$value,'LR',0, $this->aligns[$c]); } $this->Ln(5); } } } } # invpdf class $invno = 123; //Instantiation of inherited class $pdf = new INVPDF($invno, $db); $pdf->Open(); $pdf->AliasNbPages(); $pdf->setLeftMargin(10); $pdf->setRightMargin(10); $pdf->SetFont('Helvetica','',; $pdf->SetDrawColor(102); $pdf->SetFillColor(220); $pdf->AddPage(); $pdf->makeInvoice(); $pdf->output(); Quote Link to comment https://forums.phpfreaks.com/topic/289124-printing-pdf-in-php-using-fpdf/#findComment-1482556 Share on other sites More sharing options...
michelle1404 Posted June 13, 2014 Author Share Posted June 13, 2014 (edited) @ Barand: Thank you very much. Its exactly the way i wanted it. I am able to put the logo in header. Thanks Sorry for asking you again , please can you guide me how to display the result. Now only the table headers are getting displayed. $this->SetFont('Arial','',9); if ($res->num_rows > 0) { while ($r = $res->fetch_row()) { foreach ($r as $c => $value) { if ($this->formats[$c]) { $value = number_format($value, 2); } $this->Cell($this->widths[$c],5,$value,'LR',0, $this->aligns[$c]); } $this->Ln(5); } within this function, where do it initialize the columns . And i have to display my pdf like this ... i struggled lot to make it happen . but only errors i got. Its really screwed me Edited June 13, 2014 by michelle1404 Quote Link to comment https://forums.phpfreaks.com/topic/289124-printing-pdf-in-php-using-fpdf/#findComment-1482595 Share on other sites More sharing options...
Barand Posted June 13, 2014 Share Posted June 13, 2014 Executing the query and looping through the result set should print the data. The code I posted was working, giving this from my test data DATA mysql> select * from invoice; +----+------------+----------+------------+-----------------+--------------------+-------------+----------------+-----------------+--------------+ | id | invoice_no | order_no | order_date | completion_date | artwork | customer_po | order_quantity | comments | order_amount | +----+------------+----------+------------+-----------------+--------------------+-------------+----------------+-----------------+--------------+ | 1 | 123 | A1234 | 2014-04-15 | 2014-06-09 | Logo designing | B985 | 1 | A few words | 2500.00 | | 2 | 123 | A1235 | 2014-04-16 | 2014-06-10 | Brochure designing | B986 | 1 | NULL | 10000.00 | | 3 | 123 | A1236 | 2014-04-17 | 2014-06-11 | Photography | B987 | 1 | Proofs attached | 2000.00 | +----+------------+----------+------------+-----------------+--------------------+-------------+----------------+-----------------+--------------+ OUTPUT Quote Link to comment https://forums.phpfreaks.com/topic/289124-printing-pdf-in-php-using-fpdf/#findComment-1482598 Share on other sites More sharing options...
michelle1404 Posted June 13, 2014 Author Share Posted June 13, 2014 (edited) @ Barand: Thanks . I got it why its not displaying. Actually I am getting. Quote was mission for id in where clause. Can you please suggest me how to ass dynamic contents in header? Edited June 13, 2014 by michelle1404 Quote Link to comment https://forums.phpfreaks.com/topic/289124-printing-pdf-in-php-using-fpdf/#findComment-1482605 Share on other sites More sharing options...
michelle1404 Posted June 13, 2014 Author Share Posted June 13, 2014 (edited) Whenever u have time please have a look into it. Thanks Edited June 13, 2014 by michelle1404 Quote Link to comment https://forums.phpfreaks.com/topic/289124-printing-pdf-in-php-using-fpdf/#findComment-1482606 Share on other sites More sharing options...
Barand Posted June 13, 2014 Share Posted June 13, 2014 You would get the header information by querying your customer table. Quote Link to comment https://forums.phpfreaks.com/topic/289124-printing-pdf-in-php-using-fpdf/#findComment-1482612 Share on other sites More sharing options...
michelle1404 Posted June 13, 2014 Author Share Posted June 13, 2014 @Barand: Yup. I got It! But i am not getting how to print the footer details in this format . Can u please give me an idea in this..... Quote Link to comment https://forums.phpfreaks.com/topic/289124-printing-pdf-in-php-using-fpdf/#findComment-1482628 Share on other sites More sharing options...
michelle1404 Posted June 13, 2014 Author Share Posted June 13, 2014 Kindly help me with this footer section... I am not able to do it. . Please Quote Link to comment https://forums.phpfreaks.com/topic/289124-printing-pdf-in-php-using-fpdf/#findComment-1482638 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.