Ver Mensaje Individual
  #1 (permalink)  
Antiguo 02/07/2009, 13:00
osini
 
Fecha de Ingreso: junio-2003
Ubicación: Granada
Mensajes: 27
Antigüedad: 20 años, 10 meses
Puntos: 1
Generar un PDF con TCPDF con dos columnas usando HTML

Hola.

Estoy trabajando con la clase TCPDF de PHP para generar PDF. La idea es generar un documento con una tabla en dos columnas. Ya casi lo he conseguido pero me encuentro con 2 problemas que no se solucionar.

1.- Cuando la tabla pasa de página al imprimir la cabecera en la segunda página hace un pequeño descuadre. Lo podéis ver en este ejemplo (un php que genera un PDF)

www innovacionweb com/pru/boletinsin.php

Ir a la pagina 2, donde se ve el fallo.

2.- El otro fallo se produce al poner en la tabla un cellpadding="5". Aunque ajusto el ancho de la tabla para tener en cuenta el padding de cada celda la tabla ya no aparece bien a partir de la 2 pagina, incluso genera una tercera que no es necesaria. En el siguiente ejemplo lo podéis ver:

www innovacionweb com/pru/boletincon.php

El código que copio a continuación es el del segundo ejemplo.

Código PHP:
<?php

// libreria para PDFs
require_once("./tcpdf/config/lang/eng.php");
require_once(
"./tcpdf/tcpdf.php");

// extend TCPF with custom functions
class MYPDF extends TCPDF {
    
    
//number of colums
    
protected $ncols 2;
    
    
// columns width
    
protected $colwidth 125;
    
    
//Current column
    
protected $col 0;
    
    
//Ordinate of column start
    
protected $y0;
        
    
//Set position at a given column
    
public function SetCol($col) {
        
$this->col $col;
        
// space between columns
        
if ($this->ncols 1) {
            
$column_space round((float)($this->$this->original_lMargin $this->original_rMargin - ($this->ncols $this->colwidth)) / ($this->ncols 1));
        } else {
            
$column_space 0;
        }
        
//echo $column_space . ' - w:  ' . $this->w . ' - l: ' . $this->original_lMargin . ' - r: ' . $this->original_rMargin;
        
        // X position of the current column
        
if ($this->rtl) {
            
$x $this->$this->original_rMargin - ($col * ($this->colwidth $column_space));
            
$this->SetRightMargin($this->$x);
            
$this->SetLeftMargin($x $this->colwidth);
        } else {
            
$x $this->original_lMargin + ($col * ($this->colwidth $column_space));
            
$this->SetLeftMargin($x);
            
$this->SetRightMargin($this->$x $this->colwidth);
        }
        
//$this->x = $x;
        
$this->$x $this->cMargin// use this for html mode
        
if ($col 0) {
            
$this->$this->y0;
        }
    }
    
    
//Method accepting or not automatic page break
    
public function AcceptPageBreak() {
        if(
$this->col < ($this->ncols 1)) {
            
//Go to next column
            
$this->SetCol($this->col 1);
            
//Keep on page
            
return false;
        } else {
            
$this->AddPage();
            
//Go back to first column
            
$this->SetCol(0);
            
//Page break
            
return false;
        }
    }
    
    
// Set chapter title
    
public function ChapterTitle($num$label) {
        
$this->SetFont('helvetica'''14);
        
$this->SetFillColor(200220255);
        
$this->Cell(06'Chapter '.$num.' : '.$label01''1);
        
$this->Ln(4);
        
// Save ordinate
        
$this->y0 $this->GetY();
    }
    
    
// Print chapter body
    
public function ChapterBody($file) {
        
// store current margin values
        
$lMargin $this->lMargin;
        
$rMargin $this->rMargin;
        
// get esternal file content
        //$txt = file_get_contents($file, false);
        
$txt $file;
        
// Font
        
$this->SetFont('times'''9);
        
        
// Output text in a column
        //$this->MultiCell($this->colwidth, 5, $txt, 0, 'J', 0, 1, '', '', true, 0, false);
        
$this->writeHTMLCell($this->colwidth5''''$txt000true);
        
//$this->writeHTML($txt, true, false, false, false, '');
        
        
$this->Ln();
        
// Go back to first column
        
$this->SetCol(0);
        
// restore previous margin values
        
$this->SetLeftMargin($lMargin);
        
$this->SetRightMargin($rMargin);
    }
    
    
//Add chapter
    
public function PrintChapter($num,$title,$file) {
        
$this->AddPage();
        
        if (
$title != "")
            
$this->ChapterTitle($num,$title);
        else
            
$this->y0 $this->GetY();
            
        
$this->ChapterBody($file);
    }
}

// crea el documento PDF
//$pdf = new TCPDF("L", PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf = new MYPDF("L"PDF_UNITPDF_PAGE_FORMATtrue'UTF-8'false);

// información de documento
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor("Autor");
$pdf->SetTitle("Boletín de ofertas");
$pdf->SetKeywords('boletin, PDF, banco, tiempo, ofertas');

// valores que se muestran en la cabecera de cada pagina
$pdf->SetHeaderData(""0"Boletín de ofertas"'Sección');

// fuentes y tamaño para los textos de la cabecera y el pie
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN''PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA''PDF_FONT_SIZE_DATA));

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

//set margins
$pdf->SetMargins(PDF_MARGIN_LEFTPDF_MARGIN_TOPPDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

//set auto page breaks
$pdf->SetAutoPageBreak(TRUEPDF_MARGIN_BOTTOM);

//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); 

//set some language-dependent strings
$pdf->setLanguageArray($l); 

// ---------------------------------------------------------

$txtContenidoBoletin = <<<EOD
<table border="1" width="340" cellpadding="5">
<thead>
<tr>
<th>Anuncios</th>
<th>Contactos</th>
</tr>
</thead>
<tbody>
<tr>
<th colspan="2">Arte, manualidades y decoración</th>
</tr>
<tr>
<td>1 Talleres de reciclaje<br />2 otra cosa<br />3 y otra cosa</td>
<td>Irene: correo-sdfsdf.com</td>
</tr>
<!-- la tabla sigue... -->
<tbody>
</table>
EOD;

$pdf->PrintChapter(1''$txtContenidoBoletin);

// ---------------------------------------------------------

//Close and output PDF document
$pdf->Output('boletin.pdf''I');

//============================================================+
// END OF FILE                                                 
//============================================================+
?>
¿Alguien sabe si se trata de un bug? ¿Cómo lo puedo solucionar? ¿Alguien ha hecho algo similar con otra librería para generar PDFs?

Gracias y un saludo.