I'm trying to add a section title to a PDF page which is underlined so I'm adding a page-width sized Cell and wanting to underline it so I'm doing this:
$pdf->SetFont("Arial", "B", 12);
$pdf->SetXY(5, $y);
$pdf->Cell(195, 0, $qrow['Title'], "B", 1, "L");
However what comes out is more like a "strikethrough" effect where the bottom line of the cell passes through all the text.
Am I doing something obviously wrong?
Since the line height parameter is set to zero, the line is over the text.
Set the height as below, which will fix the problem:
$pdf->Cell(195, 10, $qrow['Title'], "B", 1, "L");
Related
I have a text which contains ±50 words. So I want to add it in the same cell
here is my code:
//header
$this->SetX(30);
$this->Cell(270, 20, "Suggested Improvement ",0, 0, 'C', true);
$this->SetX(300);
$this->Cell(270, 20, "Current Situation ",0, 0, 'C', true);
//data retrieved from DB
$this->Ln(20);
$this->SetTextColor(0);
$this->SetFillColor(19, 68, 160);
$this->SetLineWidth(0.25);
$this->SetX(30.5);
$this->Cell(269, 100,$row1['Suggested Improvement'], 1, 0, 'C', false);
$this->SetX(300);
$this->Cell(269.5, 100,$row1['Current Situation'], 1, 0, 'L', false);
But the problem is that the text is not filled in the cell, it is printed as one line, as it is shown here:
How can I make the text to start from the top right and to be filled in this block only?
Think you are using fpdf for php..
Have a look at MultiCell. There you can youse \n for line-break.
$this->MultiCell(100,8,"Line 1\n Line2");
You have to do the line-break on your own. Its not automaticly added.
Here you can check the witdh with GetStringWidth and add some line-breaks.
I'm developing an e-Certificate web page.
I've managed loading the certificate template and writing on top of it (Attendee Name, Event Title and Event Date).
But in positioning those three pieces of information, I couldn't position them at the center no matter how long they are. they always start from x point.
Check my work result:
Code:
<?php
require_once('fpdf.php');
require_once('fpdi.php');
$pdf =& new FPDI();
$pdf->addPage('L');
$pagecount = $pdf->setSourceFile('Certificate.pdf');
$tplIdx = $pdf->importPage(1);
$pdf->useTemplate($tplIdx);
$pdf->SetFont('Times','I',18);
$pdf->SetTextColor(0,0,0);
$pdf->SetXY(120, 62);
$pdf->Write(0, "Attendee Name");
$pdf->SetXY(120, 102);
$pdf->Write(0, "Event Name");
$pdf->SetXY(120, 140);
$pdf->Write(0, "Event Date");
$pdf->Output();
?>
Is there away to center the text no matter how long is it?
meaning without fixing the x point. however, the y point result is very good.
So 2 ways I've done this before, the most likely easiest way:
Since you only have 1 entry on the row (Attendee Name or Event Name, etc)
Start a cell at the left most point, and have the cell run the full width of the page, then specify the center option:
$pdf->SetXY(0,62);
//0 extends full width, $height needs to be set to cell height.
$pdf->Cell(0, $height, "Attendee Name", 0, 0, 'C');
The other option is to calculate the center, and set X accordingly:
//how wide is the page?
$midPtX = $pdf->GetPageWidth() / 2;
//now we need to know how long the write string is
$attendeeNameWidth = $pdf->GetStringWidth($attendeeName);
//now we need to divide that by two to calculate the shift
$shiftLeft = $attendeeNameWidth / 2;
//now calculate our new X value
$x = $midPtX - $shiftLeft;
//now apply your shift for the answer
$pdf->setXY($x, 62);
$pdf->Write(0, "Attendee Name");
You can then repeat the above for each of the other elements.
You need a way to calculate the string length in pixels of the string (say, 120 pixels) and you need to know the page width in pixels.
At that point you position the string at x = (pagewidth/2 - stringwidth/2) and Bob's your uncle.
In FPDI you do this with GetStringWidth and GetPageWidth.
You can do the same adjustment with the height. For example you can split the string in words, calculate the width of each one, and determine when it is too much. This way you can split the string in two, and center each section.
I have an TCPDF file where i want to set 1 cell with fill-background color, and to change the text in that cell to Upper-Case and color to white
$pdf->SetFont($pdfFont, 'B', 10);
$pdf->SetFillColor(59,78,135);
$pdf->Cell(50, 6, Lang::trans('supportticketsclient'), 0, 1, 'L', true);
I know i have to add "strtoupper" for uppercase letters but don't know where, and SetTextColor or something similar to it, but when i set that
$pdf->SetTextColor(0,0,0);
My whole pdf color is changed.
you just need to use the same function to change the colour back to the original(or a new colour)
$pdf->SetFont($pdfFont, 'B', 10);
$pdf->SetFillColor(59,78,135);
$pdf->SetTextColor(0,0,0);
$pdf->Cell(50, 6, strtoupper(Lang::trans('supportticketsclient')), 0, 1, 'L', true);
$pdf->SetTextColor(255,255,255);//change back to black
I have the same problem on changing the text in a cell to Upper-Case..
What I did was convert it on my query
$appName = $row['appFname']." ".$row['appMname']. " ".$row['appLname'];
$appNameUPPER = strtoupper($appName);
then I used that variable on my cell
$pdf->Cell(179,26,''.$appNameUPPER.'', 'B','', 'C', '','','','','','B');
then it works! Try converting it on your query~
It's my first time to answer here hope it works on you (^_^)/
I've have three cells and i'am trying to align the text to Left, Center and Right.
function Footer()
{
$this->SetY( -15 );
$this->SetFont( 'Arial', '', 10 );
$this->Cell(0,10,'Left text',0,0,'L');
$this->Cell(0,10,'Center text:',0,0,'C');
$this->Cell( 0, 10, 'Right text', 0, 0, 'R' );
}
When I ouput my PDF file, center text get automatically aligned right.
This is how it looks:
Can somebody tell me what I'm doing wrong here and how I can fix this issue?
The new position after a Cell call will be set to the right of each cell if you set the ln-parameter of the Cell method to 0. You have to reset the x-coordinate before the last 2 Cell calls:
class Pdf extends FPDF {
...
function Footer()
{
$this->SetY( -15 );
$this->SetFont( 'Arial', '', 10 );
$this->Cell(0,10,'Left text',0,0,'L');
$this->SetX($this->lMargin);
$this->Cell(0,10,'Center text:',0,0,'C');
$this->SetX($this->lMargin);
$this->Cell( 0, 10, 'Right text', 0, 0, 'R' );
}
}
Though Jan Slabon's answer was really good I still had issues with the center not being exactly centered on my page, maybe we have different versions of the library and that's what accounts for the slight differences, for instance he uses lMargin and on some versions that's not available. Anyway, the way it worked for me is this:
$pdf = new tFPDF\PDF();
//it helps out to add margin to the document first
$pdf->setMargins(23, 44, 11.7);
$pdf->AddPage();
//this was a special font I used
$pdf->AddFont('FuturaMed','','AIGFutura-Medium.ttf',true);
$pdf->SetFont('FuturaMed','',16);
$nombre = "NAME OF PERSON";
$apellido = "LASTNAME OF PERSON";
$pos = 10;
//adding XY as well helped me, for some reaons without it again it wasn't entirely centered
$pdf->SetXY(0, 10);
//with SetX I use numbers instead of lMargin, and I also use half of the size I added as margin for the page when I did SetMargins
$pdf->SetX(11.5);
$pdf->Cell(0,$pos,$nombre,0,0,'C');
$pdf->SetX(11.5);
//$pdf->SetFont('FuturaMed','',12);
$pos = $pos + 10;
$pdf->Cell(0,$pos,$apellido,0,0,'C');
$pdf->Output('example.pdf', 'F');
Seems that there is a parameter for this, so ('R' for right-aligning, 'C' for centering):
$pdf->Cell(0, 10, "Some text", 0, true, 'R');
will right align text.
Also, notice that the first parameter ('width') is zero, so cell has 100% width.
I trying to create a custom table using FPDF Cell/MultiCell.
My 1st cell is a MultiCell that has two lines of text. The next cell should then just be placed right next to it.
Problem : no matter what I do to the next cell, it is always on the next line of the page instead of being placed right next to the 1st cell - and it's driving me crazy.
Here is my code:
require_once 'config.php';
require 'fpdf.php';
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','',10);
$pdf->MultiCell(150,10,'Certificate of foreign Currency usage in respect of materials and components in terms of the notes to rebate item ',1);
$pdf->SetFont('Arial','',10);
$pdf->MultiCell(40,10,'DA190',1);
$pdf->Output();
The cell containing the text "DA190" should be placed next to the previous cell, but is being positioned underneath the previous cell.
Before printing your first multicell, record the cursor position:
$x=$this->GetX();
$y=$this->GetY();
add your multicell using $this->Multicell($w,5,'Content');
Reset the cursor position to the start height (y) and the start horizontal + the width of the 1st multicell:
$this->SetXY($x+$w,$y);
Add your next multicell and repeat as necessary.
this worked for me
$pdf->multicell(120, 5, ' ' . $actividad, 0, 'l', true);
$x = $pdf->GetX();
$y = $pdf->GetY();
$pdf->SetXY($x + 120, $y);
$pdf->Cell(70, -5, ' ' . $claseActividad, '', 0, 'l', true);
result
I found the solution - fpdf has an extension (#3) focussed on using multicells.