FPDF function control - php

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.

Related

FPDF generates blank page if I try to add Header and Footer

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();

FPDF not working in creating a simple pdf

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

tFPDF override Footer and Header methods?

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');

FPDF - Other ways to call Header and Footer with Codeigniter controller

I am new to FPDF and I am trying to make the header and footer change,
when I used
function Header()
{
$this->SetFont('Arial','B',15);
$this->Cell(80);
$this->Cell(30,10,'Title',1,0,'C');
$this->Ln(20);
}
It has error that says
'Cannot redeclare header()',
so what Im asking is, is there any other way of calling the header and footer? like
$pdf->header->SetFont('Arial','B',15)
or something?
Here is my code, I copied it from the tutorial 2 of FPDF, I dont want it views, I wanted it in the controller so it can called all the time
public function tutorial2()
{
$this->load->library('myfpdf');
// function Header()
// {
// $this->SetFont('Arial','B',15);
// $this->Cell(80);
// $this->Cell(30,10,'Title',1,0,'C');
// $this->Ln(20);
// } -- the Header function that is normally used
$pdf = new FPDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Times','',12);
//$pdf->footer->SetY(-15);
//$pdf->footer->SetFont('Arial','I',8);
//$pdf->footer->Cell(0,10,'Page '.$pdf->footer->PageNo().'/{nb}',0,0,'C'); -- not actually a working, or is there another way?
for($i=1;$i<=40;$i++)
$pdf->Cell(0,10,'Printing line number '.$i,0,1);
$pdf->Output();
}
Download the FPDF from the website. After downloading the file, unzip it, you will find a folder named "fpdf181" (where 181 is the version of FPDF).
Now move this folder into "application/third_party" folder of
Codeigniter.
Now create a new Library named "CustomFPDF.php" in "application/libraries" folder of Codeigniter. Copy the code below in this "CustomFPDF.php"
Code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require(APPPATH . 'third_party/fpdf181/fpdf.php');
class CustomFPDF extends FPDF {
public function header($family){
$this->SetFont($family,'B',15);
$this->Cell(80);
$this->Cell(30,10,'Title',1,0,'C');
$this->Ln(20);
}
public function getInstance(){
return new CustomFPDF();
}
}
?>
Now create a controller in "application/controllers" and use the following in your function where you are using FPDF:
$this->load->library('CustomFPDF');
$pdf = $this->customfpdf->getInstance();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->header('Arial');
$pdf->SetFont('Times','',12);
for($i=1;$i<=40;$i++)
$pdf->Cell(0,10,'Printing line number '.$i,0,1);
$pdf->Output();

using fpdf to write html content to pdf

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();
?>

Categories