Foros del Web » Programando para Internet » PHP »

Formatear texto fpdf

Estas en el tema de Formatear texto fpdf en el foro de PHP en Foros del Web. Hola. Necesito ayuda para formatear los resultados de un query en fpdf. de este modo Código PHP: $linea = "Código:" . $CODI . "Autor:" . ...
  #1 (permalink)  
Antiguo 23/06/2008, 08:39
 
Fecha de Ingreso: junio-2008
Mensajes: 291
Antigüedad: 15 años, 10 meses
Puntos: 9
Formatear texto fpdf

Hola. Necesito ayuda para formatear los resultados de un query en fpdf. de este modo

Código PHP:
$linea="Código:".$CODI."Autor:".$NOMB."Título:"$LIBRE;
    
$pdf->MultiCell(0,6,$linea,0,1,'L',10); 

donde los textos sean de color rojo y en negrita y el resultado texto normal. Quedaría así:

Código: 13544
Título: titulo Autor: autor
  #2 (permalink)  
Antiguo 23/06/2008, 09:21
Avatar de cfranco  
Fecha de Ingreso: enero-2008
Ubicación: Barcelona - España
Mensajes: 182
Antigüedad: 16 años, 3 meses
Puntos: 6
Respuesta: Formatear texto fpdf

Aqui hay un ejemplo que espero que te sirva:

Imagen, enlaces y tipos de texto
__________________
Salud y Éxitos
PFI Tube
  #3 (permalink)  
Antiguo 23/06/2008, 10:06
 
Fecha de Ingreso: junio-2008
Mensajes: 291
Antigüedad: 15 años, 10 meses
Puntos: 9
Respuesta: Formatear texto fpdf

Upf...no acabo de enterarme. Perdona pero no sé como hacerlo. Este es mi codigo.

<?php
require('fpdf.php');
define('FPDF_FONTPATH','font/');
include("conexion.php");
class PDF extends FPDF
{
var $B;
var $I;
var $U;
var $HREF;

function PDF($orientation='P',$unit='mm',$format='A4')
{
//Llama al constructor de la clase padre
$this->FPDF($orientation,$unit,$format);
//Iniciación de variables
$this->B=0;
$this->I=0;
$this->U=0;
$this->HREF='';
}

function WriteHTML($html)
{
//Intérprete de HTML
$html=str_replace("\n",' ',$html);
$a=preg_split('/<(.*)>/U',$html,-1,PREG_SPLIT_DELIM_CAPTURE);
foreach($a as $i=>$e)
{
if($i%2==0)
{
//Text
if($this->HREF)
$this->PutLink($this->HREF,$e);
else
$this->Write(5,$e);
}
else
{
//Etiqueta
if($e{0}=='/')
$this->CloseTag(strtoupper(substr($e,1)));
else
{
//Extraer atributos
$a2=explode(' ',$e);
$tag=strtoupper(array_shift($a2));
$attr=array();
foreach($a2 as $v)
if(ereg('^([^=]*)=["\']?([^"\']*)["\']?$',$v,$a3))
$attr[strtoupper($a3[1])]=$a3[2];
$this->OpenTag($tag,$attr);
}
}
}
}

function OpenTag($tag,$attr)
{
//Etiqueta de apertura
if($tag=='B' or $tag=='I' or $tag=='U')
$this->SetStyle($tag,true);
if($tag=='A')
$this->HREF=$attr['HREF'];
if($tag=='BR')
$this->Ln(5);
}

function CloseTag($tag)
{
//Etiqueta de cierre
if($tag=='B' or $tag=='I' or $tag=='U')
$this->SetStyle($tag,false);
if($tag=='A')
$this->HREF='';
}

function SetStyle($tag,$enable)
{
//Modificar estilo y escoger la fuente correspondiente
$this->$tag+=($enable ? 1 : -1);
$style='';
foreach(array('B','I','U') as $s)
if($this->$s>0)
$style.=$s;
$this->SetFont('',$style);
}

function PutLink($URL,$txt)
{
//Escribir un hiper-enlace
$this->SetTextColor(0,0,255);
$this->SetStyle('U',true);
$this->Write(5,$txt,$URL);
$this->SetStyle('U',false);
$this->SetTextColor(0);
}
}
$result=@mysql_query('select CODI, NOMB, LIBRE from vells',$conexion);
while($row =@mysql_fetch_array($result))
{
//Si la fila actual es la ultima, creo una nueva página e imprimo el titulo (quitar comentarios para activar)
$CODI = $row['CODI'];
$html='$CODI';
}







$pdf=new PDF();

$pdf->AddPage();
$pdf->SetLink($link);
$pdf->SetLeftMargin(5);
$pdf->SetFontSize(10);
$pdf->WriteHTML($html);
$pdf->Output();
?>
  #4 (permalink)  
Antiguo 23/06/2008, 10:59
 
Fecha de Ingreso: junio-2008
Mensajes: 291
Antigüedad: 15 años, 10 meses
Puntos: 9
Respuesta: Formatear texto fpdf

Hola! sólo me devuelve el segundo valor del campo CODI de mi base de datos

Código PHP:
<?php
require('fpdf.php');
define('FPDF_FONTPATH','font/');
include(
"conexion.php");
class 
PDF extends FPDF
{
var 
$B;
var 
$I;
var 
$U;
var 
$HREF;

function 
PDF($orientation='P',$unit='mm',$format='A4')
{
    
//Llama al constructor de la clase padre
    
$this->FPDF($orientation,$unit,$format);
    
//Iniciación de variables
    
$this->B=0;
    
$this->I=0;
    
$this->U=0;
    
$this->HREF='';
}

function 
WriteHTML($html)
{
    
//Intérprete de HTML
    
$html=str_replace("\n",' ',$html);
    
$a=preg_split('/<(.*)>/U',$html,-1,PREG_SPLIT_DELIM_CAPTURE);
    foreach(
$a as $i=>$e)
    {
        if(
$i%2==0)
        {
            
//Text
            
if($this->HREF)
                
$this->PutLink($this->HREF,$e);
            else
                
$this->Write(5,$e);
        }
        else
        {
            
//Etiqueta
            
if($e{0}=='/')
                
$this->CloseTag(strtoupper(substr($e,1)));
            else
            {
                
//Extraer atributos
                
$a2=explode(' ',$e);
                
$tag=strtoupper(array_shift($a2));
                
$attr=array();
                foreach(
$a2 as $v)
                    if(
ereg('^([^=]*)=["\']?([^"\']*)["\']?$',$v,$a3))
                        
$attr[strtoupper($a3[1])]=$a3[2];
                
$this->OpenTag($tag,$attr);
            }
        }
    }
}

function 
OpenTag($tag,$attr)
{
    
//Etiqueta de apertura
    
if($tag=='B' or $tag=='I' or $tag=='U')
        
$this->SetStyle($tag,true);
    if(
$tag=='A')
        
$this->HREF=$attr['HREF'];
    if(
$tag=='BR')
        
$this->Ln(5);
}

function 
CloseTag($tag)
{
    
//Etiqueta de cierre
    
if($tag=='B' or $tag=='I' or $tag=='U')
        
$this->SetStyle($tag,false);
    if(
$tag=='A')
        
$this->HREF='';
}

function 
SetStyle($tag,$enable)
{
    
//Modificar estilo y escoger la fuente correspondiente
    
$this->$tag+=($enable : -1);
    
$style='';
    foreach(array(
'B','I','U') as $s)
        if(
$this->$s>0)
            
$style.=$s;
    
$this->SetFont('',$style);
}

function 
PutLink($URL,$txt)
{
    
//Escribir un hiper-enlace
    
$this->SetTextColor(0,0,255);
    
$this->SetStyle('U',true);
    
$this->Write(5,$txt,$URL);
    
$this->SetStyle('U',false);
    
$this->SetTextColor(0);
}
}
$result=@mysql_query('select CODI, NOMB, LIBRE from vells',$conexion);
$num_rows mysql_num_rows($result); 

while(
$row =@mysql_fetch_array($result))
{
    
    
    

$CODI $row['CODI'];
$NOMB $row['NOMB'];
$LIBRE $row['LIBRE'];

$html=$row['CODI'];
}

$pdf=new PDF();
//Primera página
$pdf->AddPage();
$pdf->SetFont('Arial','',20);
$pdf->Write(5,'Para saber qué hay de nuevo en este tutorial, pulse ');
$pdf->SetFont('','U');
$link=$pdf->AddLink();
$pdf->Write(5,'aquí',$link);
$pdf->SetFont('');
//Segunda página
$pdf->AddPage();
$pdf->SetLink($link);
$pdf->SetLeftMargin(45);
$pdf->SetFontSize(14);
$pdf->WriteHTML($html);
$pdf->Output();
?>
  #5 (permalink)  
Antiguo 23/06/2008, 11:12
Avatar de cfranco  
Fecha de Ingreso: enero-2008
Ubicación: Barcelona - España
Mensajes: 182
Antigüedad: 16 años, 3 meses
Puntos: 6
Respuesta: Formatear texto fpdf

El problema es que cuando haces la consulta, pides todos los valores, pero sólo lo almacena el último valor.
__________________
Salud y Éxitos
PFI Tube
  #6 (permalink)  
Antiguo 23/06/2008, 11:16
Avatar de cfranco  
Fecha de Ingreso: enero-2008
Ubicación: Barcelona - España
Mensajes: 182
Antigüedad: 16 años, 3 meses
Puntos: 6
Respuesta: Formatear texto fpdf

Código PHP:
while($row =@mysql_fetch_array($result)) 

     
$pdf->WriteHTML($row['CODI']);
}
$pdf->Output(); 
Cambia esto en tu código y verifica si te funciona.
Yo probé con mi BD y me funciona.
__________________
Salud y Éxitos
PFI Tube
  #7 (permalink)  
Antiguo 23/06/2008, 11:29
 
Fecha de Ingreso: junio-2008
Mensajes: 291
Antigüedad: 15 años, 10 meses
Puntos: 9
Respuesta: Formatear texto fpdf

Muchas gracias por tu ayuda pero me tira error:


Código PHP:
<?php
require('fpdf.php');
define('FPDF_FONTPATH','font/');
include(
"conexion.php");
class 
PDF extends FPDF
{
var 
$B;
var 
$I;
var 
$U;
var 
$HREF;

function 
PDF($orientation='P',$unit='mm',$format='A4')
{
    
//Llama al constructor de la clase padre
    
$this->FPDF($orientation,$unit,$format);
    
//Iniciación de variables
    
$this->B=0;
    
$this->I=0;
    
$this->U=0;
    
$this->HREF='';
}

function 
WriteHTML($html)
{
    
//Intérprete de HTML
    
$html=str_replace("\n",' ',$html);
    
$a=preg_split('/<(.*)>/U',$html,-1,PREG_SPLIT_DELIM_CAPTURE);
    foreach(
$a as $i=>$e)
    {
        if(
$i&#37;2==0)
        
{
            
//Text
            
if($this->HREF)
                
$this->PutLink($this->HREF,$e);
            else
                
$this->Write(5,$e);
        }
        else
        {
            
//Etiqueta
            
if($e{0}=='/')
                
$this->CloseTag(strtoupper(substr($e,1)));
            else
            {
                
//Extraer atributos
                
$a2=explode(' ',$e);
                
$tag=strtoupper(array_shift($a2));
                
$attr=array();
                foreach(
$a2 as $v)
                    if(
ereg('^([^=]*)=["\']?([^"\']*)["\']?$',$v,$a3))
                        
$attr[strtoupper($a3[1])]=$a3[2];
                
$this->OpenTag($tag,$attr);
            }
        }
    }
}

function 
OpenTag($tag,$attr)
{
    
//Etiqueta de apertura
    
if($tag=='B' or $tag=='I' or $tag=='U')
        
$this->SetStyle($tag,true);
    if(
$tag=='A')
        
$this->HREF=$attr['HREF'];
    if(
$tag=='BR')
        
$this->Ln(5);
}

function 
CloseTag($tag)
{
    
//Etiqueta de cierre
    
if($tag=='B' or $tag=='I' or $tag=='U')
        
$this->SetStyle($tag,false);
    if(
$tag=='A')
        
$this->HREF='';
}

function 
SetStyle($tag,$enable)
{
    
//Modificar estilo y escoger la fuente correspondiente
    
$this->$tag+=($enable : -1);
    
$style='';
    foreach(array(
'B','I','U') as $s)
        if(
$this->$s>0)
            
$style.=$s;
    
$this->SetFont('',$style);
}

function 
PutLink($URL,$txt)
{
    
//Escribir un hiper-enlace
    
$this->SetTextColor(0,0,255);
    
$this->SetStyle('U',true);
    
$this->Write(5,$txt,$URL);
    
$this->SetStyle('U',false);
    
$this->SetTextColor(0);
}
}



$result=@mysql_query('select CODI, NOMB, LIBRE from vells',$conexion);



    while(
$row =@mysql_fetch_array($result))  
{  
     
$pdf->WriteHTML($row['CODI']); 

$pdf->Output();  







$pdf=new PDF();

//Primera página
$pdf->AddPage();
$pdf->SetFont('Arial','',20);
$pdf->Write(5,'Para saber qué hay de nuevo en este tutorial, pulse ');
$pdf->SetFont('','U');
$link=$pdf->AddLink();
$pdf->Write(5,'aquí',$link);
$pdf->SetFont('');
//Segunda página

$pdf->SetLink($link);
$pdf->SetLeftMargin(45);
$pdf->SetFontSize(14);
$pdf->AddPage();
$pdf->WriteHTML($html);
$pdf->Output();

?>
  #8 (permalink)  
Antiguo 23/06/2008, 11:38
Avatar de cfranco  
Fecha de Ingreso: enero-2008
Ubicación: Barcelona - España
Mensajes: 182
Antigüedad: 16 años, 3 meses
Puntos: 6
Joer

Hombre!!!

¿Cual es el error que te tira?
Cambia toda la última parte de tu código, es decir desde tu consulta e inserta este código:
Código PHP:
$pdf=new PDF();
$pdf->AddPage();
$pdf->SetLeftMargin(5);
$pdf->SetFontSize(10);

$result=@mysql_query('select CODI, NOMB, LIBRE from vells',$conexion); 



while(
$row =@mysql_fetch_array($result))   
{   
     
$pdf->WriteHTML($row['CODI']);  
}  
$pdf->Output();   

$pdf=new PDF(); 
No puedes ejecutar una operacion de una clase sin antes declarar la clase.
__________________
Salud y Éxitos
PFI Tube
  #9 (permalink)  
Antiguo 23/06/2008, 11:45
 
Fecha de Ingreso: junio-2008
Mensajes: 291
Antigüedad: 15 años, 10 meses
Puntos: 9
Respuesta: Formatear texto fpdf

PERFECTO!! muchisimas gracias, mil gracias...pero ahora cómo aplico el formato? así? porque no me deja

$pdf->WriteHTML(<I>$row['CODI']</I>);
  #10 (permalink)  
Antiguo 23/06/2008, 12:23
Avatar de cfranco  
Fecha de Ingreso: enero-2008
Ubicación: Barcelona - España
Mensajes: 182
Antigüedad: 16 años, 3 meses
Puntos: 6
Respuesta: Formatear texto fpdf

mmm, este, yo...
Creo que un pequeño repaso de cadenas no te vendría mal

Cita:
Iniciado por charlyta Ver Mensaje
PERFECTO!! muchisimas gracias, mil gracias...pero ahora cómo aplico el formato? así? porque no me deja

$pdf->WriteHTML(<I>$row['CODI']</I>);
tendrías que hacerlo asi;
Código PHP:
 $pdf->WriteHTML('<I>'.$row['CODI'].'</I>'); 
__________________
Salud y Éxitos
PFI Tube
  #11 (permalink)  
Antiguo 23/06/2008, 12:29
 
Fecha de Ingreso: junio-2008
Mensajes: 291
Antigüedad: 15 años, 10 meses
Puntos: 9
Respuesta: Formatear texto fpdf

Ya lo había hecho pero me da este error.

FPDF error: Undefined font: I
  #12 (permalink)  
Antiguo 23/06/2008, 12:31
Avatar de cfranco  
Fecha de Ingreso: enero-2008
Ubicación: Barcelona - España
Mensajes: 182
Antigüedad: 16 años, 3 meses
Puntos: 6
Respuesta: Formatear texto fpdf

Es que te falta aumentar esto despues de declarar la clase
Código PHP:
 $pdf->SetFont('Arial','',20); 
__________________
Salud y Éxitos
PFI Tube
  #13 (permalink)  
Antiguo 23/06/2008, 12:39
Avatar de cfranco  
Fecha de Ingreso: enero-2008
Ubicación: Barcelona - España
Mensajes: 182
Antigüedad: 16 años, 3 meses
Puntos: 6
Respuesta: Formatear texto fpdf

Código PHP:
$pdf=new PDF();

$pdf->AddPage();
$pdf->SetFont('Arial','',20);
$pdf->SetLeftMargin(45);
$pdf->SetFontSize(14);
$result=@mysql_query('select CODI, NOMB, LIBRE from vells',$conexion);  

while(
$row =@mysql_fetch_array($result)) 
{
 
$pdf->WriteHTML('<I>'.$row['CODI'].'</I>');  
}
$pdf->Output(); 
__________________
Salud y Éxitos
PFI Tube
  #14 (permalink)  
Antiguo 23/06/2008, 12:39
 
Fecha de Ingreso: junio-2008
Mensajes: 291
Antigüedad: 15 años, 10 meses
Puntos: 9
Respuesta: Formatear texto fpdf

Muy bien! pero por que ahora aparecen juntos? UNODOS
  #15 (permalink)  
Antiguo 23/06/2008, 12:42
 
Fecha de Ingreso: junio-2008
Mensajes: 291
Antigüedad: 15 años, 10 meses
Puntos: 9
Respuesta: Formatear texto fpdf

perdon ..me referia a esto

uno

dos
  #16 (permalink)  
Antiguo 23/06/2008, 12:43
 
Fecha de Ingreso: junio-2008
Mensajes: 291
Antigüedad: 15 años, 10 meses
Puntos: 9
Respuesta: Formatear texto fpdf

uno

espacio dos
  #17 (permalink)  
Antiguo 23/06/2008, 12:53
Avatar de cfranco  
Fecha de Ingreso: enero-2008
Ubicación: Barcelona - España
Mensajes: 182
Antigüedad: 16 años, 3 meses
Puntos: 6
Respuesta: Formatear texto fpdf

Ya estamo tres horas tio, ni cuenta me habia dado.

Ahora tienes que jugar un poco con la cadena de caracteres.

Espero que puedas compartir tu logro con esta comunidad.

Ahora tengo que terminar de trabajar .
__________________
Salud y Éxitos
PFI Tube
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 22:52.