[PHP]Line break with FPDF
Tomrage
-
Nivernus Posted messages 1 Status Membre -
Nivernus Posted messages 1 Status Membre -
Hello!
I have a SQL database. In it, there is a field coming from a text area. Therefore, line breaks are accepted. I have no problem displaying the carriage returns on my PHP page.
However, I am currently displaying some fields on a PDF document using the FPDF library, including the text area field with those famous carriage returns. Do you know if it's possible to display those carriage returns on the PDF document using the FPDF library?
Thank you in advance for your responses ^^ :)
I have a SQL database. In it, there is a field coming from a text area. Therefore, line breaks are accepted. I have no problem displaying the carriage returns on my PHP page.
However, I am currently displaying some fields on a PDF document using the FPDF library, including the text area field with those famous carriage returns. Do you know if it's possible to display those carriage returns on the PDF document using the FPDF library?
Thank you in advance for your responses ^^ :)
Configuration: Windows XP Firefox 1.5.0.11
3 réponses
No, exactly. The nl2br() function is aptly named. It transforms the "\n" characters into "
".
Basically, it's used to convert line breaks into "HTML-style" line breaks. The "\n" character is not interpreted in HTML. It needs to be converted into the "
" tag.
So obviously, you shouldn't use this function to create line breaks in a PDF.
However, I admit I have the same issue as you when it comes to creating line breaks in PDFs with FPDF. So if anyone has the solution... ;)
In the meantime, I'm looking on my end.
".
Basically, it's used to convert line breaks into "HTML-style" line breaks. The "\n" character is not interpreted in HTML. It needs to be converted into the "
" tag.
So obviously, you shouldn't use this function to create line breaks in a PDF.
However, I admit I have the same issue as you when it comes to creating line breaks in PDFs with FPDF. So if anyone has the solution... ;)
In the meantime, I'm looking on my end.
I made progress since this morning. However, I still have a small problem to solve.
Indeed, to handle line breaks, I used nl2br (which is quite normal).
However, when creating my PDF, it does not interpret the \r\n tags.
Here is my code:
$reqsql="SELECT marquage from `equipement` where ID='totop'";
if(mysql_query($reqsql,$db))
{
//Reading the results table
$result=mysql_query($reqsql,$db);
if($res=mysql_fetch_assoc($result))
{
$data=nl2br("$res[marquage]");
$pdf->Cell(0,10,"$data",0,1,"l");
}
}
And here is what I get on the PDF:
Marking materials:
toto<br />titi<br />tutu<br />tata
Thank you for your help ;)
Indeed, to handle line breaks, I used nl2br (which is quite normal).
However, when creating my PDF, it does not interpret the \r\n tags.
Here is my code:
$reqsql="SELECT marquage from `equipement` where ID='totop'";
if(mysql_query($reqsql,$db))
{
//Reading the results table
$result=mysql_query($reqsql,$db);
if($res=mysql_fetch_assoc($result))
{
$data=nl2br("$res[marquage]");
$pdf->Cell(0,10,"$data",0,1,"l");
}
}
And here is what I get on the PDF:
Marking materials:
toto<br />titi<br />tutu<br />tata
Thank you for your help ;)
Here is the solution:
<?php
require('fpdf.php');
class PDF extends FPDF {
function MultiCell($w, $h, $txt, $border=0, $align='J', $fill=false, $indent=0)
{
//Output text with automatic or explicit line breaks
$cw=&$this->CurrentFont['cw'];
if($w==0)
$w=$this->w-$this->rMargin-$this->x;
$wFirst = $w-$indent;
$wOther = $w;
$wmaxFirst=($wFirst-2*$this->cMargin)*1000/$this->FontSize;
$wmaxOther=($wOther-2*$this->cMargin)*1000/$this->FontSize;
$s=str_replace("\r",'',$txt);
$nb=strlen($s);
if($nb>0 && $s[$nb-1]=="\n")
$nb--;
$b=0;
if($border)
{
if($border==1)
{
$border='LTRB';
$b='LRT';
$b2='LR';
}
else
{
$b2='';
if(is_int(strpos($border,'L')))
$b2.='L';
if(is_int(strpos($border,'R')))
$b2.='R';
$b=is_int(strpos($border,'T')) ? $b2.'T' : $b2;
}
}
$sep=-1;
$i=0;
$j=0;
$l=0;
$ns=0;
$nl=1;
$first=true;
while($i<$nb)
{
//Get next character
$c=$s[$i];
if($c=="\n")
{
//Explicit line break
if($this->ws>0)
{
$this->ws=0;
$this->_out('0 Tw');
}
$this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
$i++;
$sep=-1;
$j=$i;
$l=0;
$ns=0;
$nl++;
if($border && $nl==2)
$b=$b2;
continue;
}
if($c==' ')
{
$sep=$i;
$ls=$l;
$ns++;
}
$l+=$cw[$c];
if ($first)
{
$wmax = $wmaxFirst;
$w = $wFirst;
}
else
{
$wmax = $wmaxOther;
$w = $wOther;
}
if($l>$wmax)
{
//Automatic line break
if($sep==-1)
{
if($i==$j)
$i++;
if($this->ws>0)
{
$this->ws=0;
$this->_out('0 Tw');
}
$SaveX = $this->x;
if ($first && $indent>0)
{
$this->SetX($this->x + $indent);
$first=false;
}
$this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
$this->SetX($SaveX);
}
else
{
if($align=='J')
{
$this->ws=($ns>1) ? ($wmax-$ls)/1000*$this->FontSize/($ns-1) : 0;
$this->_out(sprintf('%.3f Tw',$this->ws*$this->k));
}
$SaveX = $this->x;
if ($first && $indent>0)
{
$this->SetX($this->x + $indent);
$first=false;
}
$this->Cell($w,$h,substr($s,$j,$sep-$j),$b,2,$align,$fill);
$this->SetX($SaveX);
$i=$sep+1;
}
$sep=-1;
$j=$i;
$l=0;
$ns=0;
$nl++;
if($border && $nl==2)
$b=$b2;
}
else
$i++;
}
//Last chunk
if($this->ws>0)
{
$this->ws=0;
$this->_out('0 Tw');
}
if($border && is_int(strpos($border,'B')))
$b.='B';
$this->Cell($w,$h,substr($s,$j,$i),$b,2,$align,$fill);
$this->x=$this->lMargin;
}
}
?>
name the file, for example: mc_indent.php
<?php
require('mc_indent.php');
$InterLine = 7;
$pdf=new PDF();
$pdf->AddPage();
$pdf->SetMargins(30,10,30);
$pdf->SetFont('Arial','',12);
$txt = "Dear Pierre";
$txtLen = $pdf->GetStringWidth($txt);
$middle = (210-$txtLen)/2;
$pdf->SetX($middle);
$pdf->Write(5,$txt);
$pdf->ln(30);
$txt = "The time has come for you to renew your insurance license, as it will expire on 28/9. You will find attached to this document the certificate of aptitude to be filled out by the doctor.";
$pdf->MultiCell(0,$InterLine,$txt,0,'J',0,15);
$pdf->ln(10);
$txt = "I would like to remind you that this license is mandatory and necessary for practicing our favorite sport, both during our training and at any other events you may participate in such as competitions, federal courses, or friendly visits to another club.";
$pdf->MultiCell(0,$InterLine,$txt,0,'J',0,15);
$pdf->ln(10);
$txt = "Therefore, I would be grateful if you could return the completed certificate of aptitude duly filled by the doctor along with your payment of €31 or proof of payment via bank transfer. All this as soon as possible to avoid interrupting the coverage of said insurance and, at the same time, preventing you from participating in our courses while the regularization is taking place. It concerns your safety.";
$pdf->MultiCell(0,$InterLine,$txt,0,'J',0,15);
$pdf->ln(10);
$txt = "Thank you for the trust you place in our club for your sporting development.";
$pdf->MultiCell(0,$InterLine,$txt,0,'J',0,15);
$pdf->ln(10);
$txt = "The committee";
$pdf->MultiCell(0,$InterLine,$txt,0,'R',0);
$pdf->Output();
?>
<?php
require('fpdf.php');
class PDF extends FPDF {
function MultiCell($w, $h, $txt, $border=0, $align='J', $fill=false, $indent=0)
{
//Output text with automatic or explicit line breaks
$cw=&$this->CurrentFont['cw'];
if($w==0)
$w=$this->w-$this->rMargin-$this->x;
$wFirst = $w-$indent;
$wOther = $w;
$wmaxFirst=($wFirst-2*$this->cMargin)*1000/$this->FontSize;
$wmaxOther=($wOther-2*$this->cMargin)*1000/$this->FontSize;
$s=str_replace("\r",'',$txt);
$nb=strlen($s);
if($nb>0 && $s[$nb-1]=="\n")
$nb--;
$b=0;
if($border)
{
if($border==1)
{
$border='LTRB';
$b='LRT';
$b2='LR';
}
else
{
$b2='';
if(is_int(strpos($border,'L')))
$b2.='L';
if(is_int(strpos($border,'R')))
$b2.='R';
$b=is_int(strpos($border,'T')) ? $b2.'T' : $b2;
}
}
$sep=-1;
$i=0;
$j=0;
$l=0;
$ns=0;
$nl=1;
$first=true;
while($i<$nb)
{
//Get next character
$c=$s[$i];
if($c=="\n")
{
//Explicit line break
if($this->ws>0)
{
$this->ws=0;
$this->_out('0 Tw');
}
$this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
$i++;
$sep=-1;
$j=$i;
$l=0;
$ns=0;
$nl++;
if($border && $nl==2)
$b=$b2;
continue;
}
if($c==' ')
{
$sep=$i;
$ls=$l;
$ns++;
}
$l+=$cw[$c];
if ($first)
{
$wmax = $wmaxFirst;
$w = $wFirst;
}
else
{
$wmax = $wmaxOther;
$w = $wOther;
}
if($l>$wmax)
{
//Automatic line break
if($sep==-1)
{
if($i==$j)
$i++;
if($this->ws>0)
{
$this->ws=0;
$this->_out('0 Tw');
}
$SaveX = $this->x;
if ($first && $indent>0)
{
$this->SetX($this->x + $indent);
$first=false;
}
$this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
$this->SetX($SaveX);
}
else
{
if($align=='J')
{
$this->ws=($ns>1) ? ($wmax-$ls)/1000*$this->FontSize/($ns-1) : 0;
$this->_out(sprintf('%.3f Tw',$this->ws*$this->k));
}
$SaveX = $this->x;
if ($first && $indent>0)
{
$this->SetX($this->x + $indent);
$first=false;
}
$this->Cell($w,$h,substr($s,$j,$sep-$j),$b,2,$align,$fill);
$this->SetX($SaveX);
$i=$sep+1;
}
$sep=-1;
$j=$i;
$l=0;
$ns=0;
$nl++;
if($border && $nl==2)
$b=$b2;
}
else
$i++;
}
//Last chunk
if($this->ws>0)
{
$this->ws=0;
$this->_out('0 Tw');
}
if($border && is_int(strpos($border,'B')))
$b.='B';
$this->Cell($w,$h,substr($s,$j,$i),$b,2,$align,$fill);
$this->x=$this->lMargin;
}
}
?>
name the file, for example: mc_indent.php
<?php
require('mc_indent.php');
$InterLine = 7;
$pdf=new PDF();
$pdf->AddPage();
$pdf->SetMargins(30,10,30);
$pdf->SetFont('Arial','',12);
$txt = "Dear Pierre";
$txtLen = $pdf->GetStringWidth($txt);
$middle = (210-$txtLen)/2;
$pdf->SetX($middle);
$pdf->Write(5,$txt);
$pdf->ln(30);
$txt = "The time has come for you to renew your insurance license, as it will expire on 28/9. You will find attached to this document the certificate of aptitude to be filled out by the doctor.";
$pdf->MultiCell(0,$InterLine,$txt,0,'J',0,15);
$pdf->ln(10);
$txt = "I would like to remind you that this license is mandatory and necessary for practicing our favorite sport, both during our training and at any other events you may participate in such as competitions, federal courses, or friendly visits to another club.";
$pdf->MultiCell(0,$InterLine,$txt,0,'J',0,15);
$pdf->ln(10);
$txt = "Therefore, I would be grateful if you could return the completed certificate of aptitude duly filled by the doctor along with your payment of €31 or proof of payment via bank transfer. All this as soon as possible to avoid interrupting the coverage of said insurance and, at the same time, preventing you from participating in our courses while the regularization is taking place. It concerns your safety.";
$pdf->MultiCell(0,$InterLine,$txt,0,'J',0,15);
$pdf->ln(10);
$txt = "Thank you for the trust you place in our club for your sporting development.";
$pdf->MultiCell(0,$InterLine,$txt,0,'J',0,15);
$pdf->ln(10);
$txt = "The committee";
$pdf->MultiCell(0,$InterLine,$txt,0,'R',0);
$pdf->Output();
?>