I am new to using FPDF and cant get my head around it. Here is the simple case:
$pdf = new FPDF();
$pdf->AddPage();
for($i=0; $i< 10; $i++){
$pdf->SetFont('Arial', "B", 12);
$pdf->Cell(40, 10, "asd");
}
$pdf->Output();
The browser says it failed to load PDF document.
whats wrong?
Well the code itself works for me. As stupid as it might sound, did you include the fpdf.php?
require('your/dircetory/fpdf.php');
Related
I am troubling in generating this report..
I am aiming something like this
But I only have this
I want the 5th and 6th cell to have a space between them..
and here is my code so far:
$pdf = new PDF('L', 'mm', array(215.9, 330.2));
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Arial','',11);
$pdf->Cell(132);
$pdf->Cell(45,15,'Teacher'."'s ".'Table',1,'','C');
$pdf->Ln(25);
while($row = mysql_fetch_array($sqlresult)){
if($pdf->GetX() < 290){
$pdf->SetFont('Arial','',10);
$pdf->Cell(30,15,$row['stud_fname'].", ".$row['stud_lname'],1,'','C');
}else{
$pdf->Ln(20);
}
}
$pdf->Output();
any idea would be appreciated, thanks in advance :)
Count the cells and create an 'invisible' cell between the 5th and the 6th.
EDIT: I think your current code is wrong, when $pdf->GetX() > 290 you are creating a new line BUT are also skipping the mysql result (you are not writting it). I have updated my answer with what I think is right.
$thisCell=1;
while($row = mysql_fetch_array($sqlresult)){
if($pdf->GetX() >= 290){
$pdf->Ln(20);
$thisCell=1;
}
$pdf->SetFont('Arial','',10);
$pdf->Cell(30,15,$row['stud_fname'].", ".$row['stud_lname'],1,'','C');
if ($thisCell==5)
$pdf->Cell(10,15,'',0); //cell without borders
$thisCell++;
}
I'm trying to write text to a PDF and there seems to be a weird margin on the top of my page.
This is my following code:
require_once('fpdf.php');
require_once('fpdi/fpdi.php');
//Start the FPDI
$pdf = new FPDI('P', 'pt');
//Set the source PDF file
$source_file = $pdf->setSourceFile("template.pdf");
//Import the first page of the file
$tppl = $pdf->importPage(1);
$pdf->AddPage();
//get size of pdf page
$size = $pdf->getTemplateSize($tppl);
$pdf->useTemplate($tppl, null, null, $size['w'], $size['h'], true);
$pdf->SetMargins(0, 0, 0);
$pdf->SetTextColor(0, 0, 0);
When I use a font-size pt 12, and write text I get this:
$pdf->SetFont('Arial', '', 12);
$pdf->SetXY(0, 0);
$pdf->Write(0, "Hi");
When I do $pdf->SetXY(0, 7.5) I get this
The above looks like I can easily add 7.5 points to the Y and be fine.
However, if I changed the font-size, the distance between the top and the text grows a little greater.
$pdf->SetFont('Arial', '', 8);
Could anyone help me figure out how to neutralize this to at least make it so if I set my XY to a number, it will put it on the some location regardless of the font-size? I've tried different pdf's and it works all the same.
EDIT:
I did $pdf->GetY() and I get 28.35
You simply define a line height of zero. Because of this the text is "centered" vertically around 0.
A common line height is:
$pdf->Write($pdf->FontSize * 1.2, "Hi");
I solved this by instead of doing Write() I used Cell().
I think the main issue was not having a solid width and height. All I know is it works perfectly now so anyone encountering the same problems should try this.
$pdf->Cell(WIDTH,HEIGHT,TEXT);
I also did the following, not sure if this helped or not but I have it in my script.
$pdf->SetMargins(0, 0);
$pdf->cMargin = 0;
I have two features which uses FPDF in the application I am developing. The other one prints things correctly, while the other outputs only a blank sheet and I really dont know where's the error in here. I already tried to var_dump to know if values are fetch correctly from the database and everything is good except for this printing the pdf part. What should I do?
Here is the code snippet for the one that only outputs a blank sheet.
function generate(){
$sample= $this->model_a->get_a();
$j = 10;
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetMargins(0.5, 0.5, 0.5);
$pdf->Cell(40,4,'TITLE','',0,'C',0);
$pdf->setXY(12.5,$j+=4);
$pdf->SetFont('times','B',10);
$pdf->Cell(42,4,'COL1','TLRB',0,'C',0);
$pdf->Cell(100,4,'NAME','TLRB',0,'C',0);
$pdf->Cell(42,4,'NAME2','TLRB',0,'C',0);
foreach($sample as $s){
$x= $s['id'];
$y= "{$s['f_name']}, {$s['l_name']} {$s['mi']}.";
$z= "{$s['f_name1']}, {$s['l_name1']} {$student['mi1']}.";
$this->addRow($pdf,$x,$y,$z, $j);
}
$pdf->Output();
}
function addRow($pdf,$x,$y,$z,&$j){
$pdf->setXY(12.5,$j+=4);
$pdf->SetFont('times','',10);
$pdf->Cell(42,4,$x,'TLRB',0,'C',0);
$pdf->Cell(100,4,$y,'TLRB',0,'C',0);
$pdf->Cell(42,4,$z,'TLRB',0,'C',0);
}
I think that the function addRow have to return the pdf object. Alternatively you can create the new FPDF in a variable class ($this->pdf) and change all the other $pdf.
Is there a way to import pdf files which stored in database into mpdf class instance?
As for now i can use import for certain page from file only like shown below.
$mpdf->SetImportUse();
$pagecount = $mpdf->SetSourceFile('testfile.pdf');
$tplId = $mpdf->ImportPage($pagecount, 50, 50, 100, 100);
$mpdf->UseTemplate($tplId, '', '', 100, 100);
$mpdf->Output();
But what if I want to import from pdf which saved in database?
Thanks for helping.
Reading large files from a database is a bad idea, you'll find storing them somewhere on the file-system to be much faster. But, any file stored in the database should be stored in its' original form (possibly base64 encoded). Pretend the value you get from the database is the return from fread() and it should work fine.
Little bit later, but maybe this help somebody else.
With MPDF 8 and later:
Method SetSourceFile accept string (filename) OR StreamReader. The StreamReader can be created from file OR from string.
So your code will looks like:
// $mpdf->SetImportUse(); is not supported in MPDF 8
$data = ''; // your data, NOT base64
$pagecount = $mpdf->SetSourceFile(\setasign\Fpdi\PdfParser\StreamReader::createByString($data));
for ($i=1; $i<=$pagecount; $i++) {
$import_page = $mpdf->ImportPage($i, 50, 50, 100, 100); // OR just: $import_page = $mpdf->ImportPage($i);
$mpdf->UseTemplate($import_page);
if ($i < $pagecount) {
$mpdf->AddPage();
}
}
$mpdf->Output();
Note:
You can always call WriteHTML before or after or inside for cycle.
The problem is, I need to write data to several copies of the same imported pdf file, and save it as one pdf. I can write data to one page just fine, but when I try to write to more than one, or even continue text (using SetAutoPageBreak()), it simply stops writing once it hits the next page. Although, if I add an arbitrary loop to write more data, the resulting pdf's number of pages is increased to accommodate the added data, but the pages beyond the first are still blank. I have simplified what I'm trying to do into a smaller example to illustrate the issue:
public function actionSample() {
$pdf = new FPDI();
$pdf->AcceptPageBreak();
$pdf->SetAutoPageBreak(true, 30);
$pagecount = $pdf->setSourceFile('images/sample.pdf');
for ($i = 1; $i <= $pagecount; $i++) {
$pdf->AddPage();
$tplidx = $pdf->ImportPage($i);
$pdf->useTemplate($tplidx, 10, 10, 200);
$s = $pdf->getTemplatesize($tplidx);
$pdf->SetTextColor(32,32,32);
$pdf->SetFontSize(10);
$pdf->SetXY($pdf->getX(), $pdf->getY()+10);
$pdf->Write(2, 'This is not!');
}
$pdf->Output('Sample.pdf', 'D');
}
The sample document has 3 blank pages initially. (I did this to make it easier to see what was being written)
$pdf->AddPage();
you have to just put this piece of code inside for loop near closing braces.
All the best..!!