So I have run into a bit of a problem with fpdf. I am generating a report successfully, except when I try to add a header/footer, it just displays a blank page. Not even the Pdf viewer. I have just copied the example from the tutorial on the fpdf website, and I have used $pdf-> new PDF instead of $pdf-> new FPDF, as suggested under every similar question here. But the problem persists. There seems to be another issue, that is the I also get a blank page unless I comment out the require 'fpdf/fpdf.php';. Only if it is commented out, does the pdf show.
Here's my code :
//require '/usr/share/php/fpdf/fpdf.php';
/////////ADD HEADER, FOOTER AND NEW PAGE/ MARGIN SETTING////////////
class PDF extends FPDF
{
// Page header
function Header()
{
// Logo
$this->Image("../tmp/headers/header2.",0,0,210);
// Arial bold 15
$this->SetFont('Arial','B',15);
// Move to the right
$this->Cell(80);
// Title
$this->Cell(30,10,'Title',1,0,'C');
// Line break
$this->Ln(20);
}
// Page footer
function Footer()
{
// Position at 1.5 cm from bottom
$this->SetY(-15);
// Arial italic 8
$this->SetFont('Arial','I',8);
// Page number
$this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
}
// Instanciation of inherited class
//$pdf = new PDF();
//$pdf->AliasNbPages();
//$pdf->AddPage();
//$pdf->Header();
//$pdf->SetFont('Times','',12);
//for($i=1;$i<=40;$i++)
// $pdf->Cell(0,10,'Printing line number '.$i,0,1);
//$pdf->Output();
//-------------------- Start of Title Page --------------------//
//start pdf file, add page, set font
$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->AddGBFont();
$pdf->AddBig5Font();
$pdf->AddPage();
/////rest of the report /////
$pdf->Output();
Related
I'm playing with FPDF library and i try this simple code:
require_once($_SERVER['DOCUMENT_ROOT'].'/fpdi/FPDF/fpdf.php');
class PDF extends FPDF{
function Header(){
$this->Write(6,'Dokumen ini adalah sah');
}
}
$pdf = new PDF();
$pdf->SetFont('Arial');
$pdf->AddPage();
$pdf->SetXY(5, 5);
$pdf->Write(8, 'A complete document imported with FPDI');
// Output the new PDF
$pdf->Output();
But it didn't do anything. No document or exception is popped out. If i correct, if everything is fine a document should appear. I have no idea why it's not working. Any help would be very appreciated :)
Your problem comes from Header function on your PDF Class. Based on the documentation at least you have to set these variables on Header function
function Header()
{
// Select Arial bold 15
$this->SetFont('Arial','B',15);
// Move to the right
$this->Cell(80);
// Framed title
$this->Cell(30,10,'Title',1,0,'C');
// Line break
$this->Ln(20);
}
And this is my code that looks like your code, and its works
<?php
require __DIR__ . '/vendor/autoload.php';
class PDF extends FPDF{
function Header()
{
// Select Arial bold 15
$this->SetFont('Arial','B',15);
// Move to the right
$this->Cell(80);
// Framed title
$this->Cell(50,10,'Dokumen ini sah',1,0,'C');
// Line break
$this->Ln(20);
}
}
$pdf = new PDF();
// print_r($pdf);
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
Result :
More detail you can visit official documentation of FPDF
I am using tFPDF, how can I override Footer and Header methods? I am initiating my pdf with this code:
$pdf = new tFPDF('P', 'mm', 'A4');
So this code it is not working for me
class PDF extends tFPDF
{
// Page header
function Header()
{
// Logo
$this->Image('logo.png',10,6,30);
// Arial bold 15
$this->SetFont('Arial','B',15);
// Move to the right
$this->Cell(80);
// Title
$this->Cell(30,10,'Title',1,0,'C');
// Line break
$this->Ln(20);
}
// Page footer
function Footer()
{
// Position at 1.5 cm from bottom
$this->SetY(-15);
// Arial italic 8
$this->SetFont('Arial','I',8);
// Page number
$this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
}
The example code is here: https://tcpdf.org/examples/example_003
From this you can easily see what goes wrong. You create a new PDF with:
$pdf = new tFPDF('P', 'mm', 'A4');
calling the original tFPDF class, not the class you created with your custom header and footer. You should use:
$pdf = new PDF('P', 'mm', 'A4');
Since PDF is the class with your header and footer in it.
Use the following code:
class MYPDF extends tFPDF
...
$pdf = new MYPDF('P', 'mm', 'A4');
I am trying to use fpdf and html2pdf to write contents of a div to a pdf file. But the WriteHTML function doesn't seem to work. What's wrong with the code?
It says
call to undefined function WriteHTML() in demo.php on line 43
Below is my code.
<?php
$contents = $_POST['divContent'];
require_once('fpdf/fpdf.php') ;
require('html2pdf/html2pdf.php');
class PDF extends PDF_HTML
{
// Page header
function Header()
{
// Logo
$this->Image('fpdf/logo.png',10,6,30);
// Arial bold 15
$this->SetFont('Arial','B',15);
// Move to the right
$this->Cell(80);
// Title
$this->Cell(60,10,'Report',1,0,'C');
// Line break
$this->Ln(20);
}
// Page footer
function Footer()
{
// Position at 1.5 cm from bottom
$this->SetY(-15);
// Arial italic 8
$this->SetFont('Arial','I',8);
// Page number
$this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
}
// Instanciation of inherited class
$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Times','',12);
$pdf->MultiCell(190,10,WriteHTML($contents));
$pdf->Output();
?>
Remember your PDF extends the main class(PDF_HTML), so upon calling the class PDF,
It will inherit all functions contained in PDF_HTML, hence you must also access it properly
$pdf->MultiCell(190,10,$pdf->WriteHTML($contents));//you were not accessing the function properly
Try this code:
$pdf->WriteHTML($contents);
This code is used to generate PDF BY HTML Code.
<?php
require('fpdf/fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$row=file('toys.txt');
$pdf->SetFont('Arial','B',12);
foreach($row as $rowValue) {
$data=explode(';',$rowValue);
foreach($data as $columnValue)
$pdf->Cell(90,12,$columnValue,1);
$pdf->SetFont('Arial','',12);
$pdf->Ln();
}
$pdf->Output();
?>
I'm new to programming and I'm creating a pdf with FPDF(A PHP Class). At the bottom of my code you will notice that I create a new page and call the function Header(). For some reason, the header is also applied to the first page. How do I only apply the header to the second page?
Here is a link to a live version of the pdf: http://fosterinnovationculture.com/experiments/fpdf/index-two.php
<?php
require('fpdf.php');
// classes
class PDF extends FPDF
{
function Logo(){
$this->Image('images/logo.png',1,5.5,3);
}
//change name of function
function HeaderOne($xCor, $yCor, $text)
{
$this->SetX($xCor);
$this->SetY($yCor);
$this->SetFont('Arial','B',18);
$this->Cell(5,1,$text);
}
//Add bottom border
function BorderLine()
{
$this->SetDrawColor(0,0,0);
$this->SetFillColor(0,0,0);
$this->Rect(1, 10, 6.5, .015, 'F');
}
function Footer()
{
// Go to 1.5 cm from bottom
$this->SetY(-5.7);
// Select Arial italic 8
$this->SetFont('Arial','I',8);
// Print centered page number
$this->Cell(0,10,'Page '.$this->PageNo(),0,0,'R');
}
function Header(){
// Subtitle
$this->SetY(.25);
$this->SetX(1);
$this->SetFont('Arial','',12);
$this->Cell(6,1,'INNOVATION READINESS ASSESSMENT');
// Line
$this->SetDrawColor(0,0,0);
$this->SetFillColor(0,0,0);
$this->Rect(1, .5, 6.5, .015, 'F');
}
}
//pdf document preferences
$pdf = new PDF('p', 'in', 'Letter');
$pdf->AddPage();
$pdf->SetMargins(1,1,1,1);
//Page 1
$pdf->SetDrawColor(238,170,40);
$pdf->SetFillColor(238,170,40);
$pdf->Rect(1, 1, 6.5, .25, 'F');
$pdf->Image('images/header.jpg',1,1.5,6.5,3.5,'JPG','');
$pdf->Logo();
$pdf->HeaderOne(1,9,'Innovation Readiness Assessment');
$pdf->BorderLine();
//Page 2
$pdf->AddPage();
$pdf->Header();
// Close and output file to the browser
$pdf->Output();
?>
See the official documentation for Header() and
AddPage().
The AddPage() function already internally calls Header().
There is no need to call Header() explicitly here.
I've created a PDF generator thanks to FPDF, it uses data from a mysql database and works very well. Number of pages is variable.
Then I wanted to add to every of this PDF some pages imported from other PDF files. Number of added page and adress of imported file are variable too.
It works very well, excepted that my Footer doesn't appear anymore. I want to keep this Footer on every page, the ones created by the generator and the ones imported. Can someone tell me where is the problem?..
This i my code :
require_once('gpdf/fpdf.php');
require_once('gpdf/fpdi.php');
class PDF extends FPDI
{
function Header()
{
}
function Footer()
{
// Positionnement à 1,5 cm du bas
$this->SetY(-15);
// Police Arial italique 8
$this->SetFont('Arial','I',8);
// Numéro de page
$this->Cell(0,10,'Devis from MyCompany - Page '.$this->PageNo().'/{nb}'.' Paraphes :',0,0,'C');
}
}
// Instanciation de la classe dérivée
$pdf = new FPDI();
$pdf->AliasNbPages();
$pdf->AddPage();
// Here is page 1, you don't need the details
$pdf->AddPage();
// Here is page 2, some other pages can come too
// Then begins the importation
// get the page count
$pageCount = $pdf->setSourceFile('cgua/cgu_'.$customer['num'].'.pdf');
// iterate through all pages
for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
// import a page
$templateId = $pdf->importPage($pageNo);
// get the size of the imported page
$size = $pdf->getTemplateSize($templateId);
// create a page (landscape or portrait depending on the imported page size)
if ($size['w'] > $size['h']) {
$pdf->AddPage('L', array($size['w'], $size['h']));
} else {
$pdf->AddPage('P', array($size['w'], $size['h']));
}
// use the imported page
$pdf->useTemplate($templateId);
}
$pdf->Output('devis.pdf','I');
I've found no explanation about how to keep my Footer in FPDI's manual... I'm sure it's easy to rule the problem, I just didn't find the way!
Thanks!
You created a new class that inherits the FPDI class. This new class, PDF, defines the Footer method correctly. But then you instantiated the FPDI class, instead of the PDF class.
Just change
$pdf = new FPDI();
to
$pdf = new PDF();
so that you can instantiate your new class and see the results of the new Footer method.