Ver Mensaje Individual
  #3 (permalink)  
Antiguo 23/08/2014, 12:22
Cristian23CL4PTP
 
Fecha de Ingreso: agosto-2013
Ubicación: Talca
Mensajes: 40
Antigüedad: 10 años, 8 meses
Puntos: 1
Respuesta: generar una factura en pdf desde Php

Antes que nada un saludo.
Te recomiendo usar TCPDF que es la librería que estoy usando yo.

Aquí te dejo un código de ejemplo, que use en un sistemita pasado;

Código PHP:
Ver original
  1. <?php
  2.  
  3.    
  4.     require_once('TCPDF/tcpdf.php');
  5.     require_once('TCPDF/lang/spa.php');
  6.  
  7.     #Conexion a Base de datos
  8.    
  9.     class MYPDF extends TCPDF {
  10.  
  11.         private $objclass;
  12.      
  13.         public function ColoredTable($header,$data) {
  14.  
  15.             $this->SetFillColor(255, 0, 0);
  16.             $this->SetTextColor(255);
  17.             $this->SetDrawColor(128, 0, 0);
  18.             $this->SetLineWidth(0.3);
  19.             $this->SetFont('', 'B');
  20.  
  21.             $w = array(40, 55, 35, 22, 35);
  22.             $num_headers = count($header);
  23.             for($i = 0; $i < $num_headers; ++$i) {
  24.                 $this->Cell($w[$i], 7, $header[$i], 1, 0, 'C', 1);
  25.             }
  26.             $this->Ln();
  27.  
  28.             $this->SetFillColor(224, 235, 255);
  29.             $this->SetTextColor(0);
  30.             $this->SetFont('');
  31.  
  32.             $fill = 0;
  33.             foreach($data as $row) {
  34.                 $this->Cell($w[0], 6, $row['producto_venta'], 'LR', 0, 'C', $fill);
  35.                 $this->Cell($w[1], 6, $row['nombreprod_venta'], 'LR', 0, 'C', $fill);
  36.                 $this->Cell($w[2], 6, $row['producto_valor'], 'LR', 0, 'C', $fill);
  37.                 $this->Cell($w[3], 6, $row['sum(cantidad_venta)'], 'LR', 0, 'C', $fill);
  38.                 $this->Cell($w[4], 6, $row['sum(total_venta)'], 'LR', 0, 'C', $fill);
  39.                 $this->Ln();
  40.                 $fill=!$fill;
  41.             }
  42.             $this->Cell(array_sum($w), 0, '', 'T');
  43.         }
  44.     }
  45.  
  46.     $pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  47.     $db = new VentaDb();
  48.  
  49.  
  50.     $idCliente = $_GET['idCliente'];
  51.  
  52.     $sql = mysql_query("SELECT nombre_cliente, apellido__cliente, edad_cliente FROM cliente WHERE idCliente = ".$idCliente." ",$conexionBD);
  53.  
  54.     #Detalles del cliente
  55.  
  56.     $pdf->SetCreator(PDF_CREATOR);    
  57.     $pdf->SetHeaderData("logo.png", PDF_HEADER_LOGO_WIDTH, 'Detalle Cliente', "Cliente: ".$sql['nombre_cliente']." ".$sql['apellido__cliente']." \nEdad: ".$sql['edad_cliente']."");
  58.     $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
  59.     $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));    
  60.     $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  61.     $pdf->SetMargins(8, 40, PDF_MARGIN_RIGHT);
  62.     $pdf->SetHeaderMargin(5);
  63.     $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
  64.     $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);    
  65.     $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  66.     $pdf->setLanguageArray($l);
  67.     $pdf->SetFont('helvetica', '', 12);  
  68.     $pdf->AddPage();
  69.      
  70.     $query = mysql_query("SELECT producto_venta, nombreprod_venta, valor_venta, sum(cantidad_venta), sum(total_venta) FROM venta INNER JOIN detalleunico d ON
  71.    d.idunico = venta.factura INNER JOIN producto p ON p.idproducto = venta.producto_venta WHERE detalleunico.cliente = ".$idCliente." GROUP BY producto_venta", $conexionBD)
  72.  
  73.     while($rows = mysql_fetch_array($query)):
  74.          $data[] = $rows;
  75.     endwhile;
  76.  
  77.     $pdf->ColoredTable($header, $data);    
  78.     $pdf->Output('Detalle de Factua Cliente '.$idCliente.'.pdf', 'I'); #Si se quiere descargar el PDF directamente, en la ultima linea poner 'D'
  79.    
  80. ?>

Descarga la librería desde su pagina oficial http://sourceforge.net/projects/tcpdf/files/

Última edición por Cristian23CL4PTP; 23/08/2014 a las 22:01