TCPDF - weird issue with footers - php

Check out the source code here:
http://www.savvissl.com/demo1/showcode.php
check out the script here
http://www.savvissl.com/demo1/testPDF.php
Here is the issue... the footer prints fine on every page except for the last page. The last page never has a footer. If there is only one page in the document the footer will not print at all.

OK I couldn't figure it out, but i was able to copy a co-workers example that worked. If anyone wants the source code here it is:
<?php
require_once('tcpdf/config/lang/eng.php');
require_once('tcpdf/tcpdf.php');
define('PDF_FOOTER_TEXT','800 Vinial St. Pittsburgh, PA 15212 | phone: 412.321.7006 | fax: 412.321.7005 | www.savvior.com');
$PDF_LINE_COLOR=array(255,255,0);
define('PDF_FOOTER_TEXT_COLOR',170);
class MYPDF extends TCPDF
{
//Page header
public function Header()
{
global $PDF_LINE_COLOR;
$image_file = K_PATH_IMAGES.'image.jpg';
$this->Image($image_file, 160, 0, 30, '', 'JPG', '', 'T', false, 300, '', false, false, 0, false, false, false);
$this->SetFont('helvetica', 'B', 20);
$this->Cell(0, 15, '', 0, false, 'C', 0, '', 0, false, 'M', 'M');
$this->line(10,27,200,27,array('width' => 0.5, 'cap' => 'butt', 'join' => 'miter', 'solid' => 4, 'color' => $PDF_LINE_COLOR));
}
public function Footer()
{
global $PDF_LINE_COLOR;
$cur_y = $this->GetY();
$ormargins = $this->getOriginalMargins();
$this->SetTextColor(PDF_FOOTER_TEXT_COLOR, PDF_FOOTER_TEXT_COLOR, PDF_FOOTER_TEXT_COLOR);
$this->SetY($cur_y);
$this->line(10,400,200,400,array('width' => 0.5, 'cap' => 'butt', 'join' => 'miter', 'solid' => 4, 'color' => $PDF_LINE_COLOR));
$this->Cell(0,11,"Page ". $this->getAliasNumPage().'/'.$this->getAliasNbPages(),'T',0,'L');
$this->Cell(0,11,PDF_FOOTER_TEXT,'T',0,'R');
}
}
ob_start();
?><h1>Content Is Needed For This Page...</h1>
...
<?
$html=ob_get_clean();
function makePDFFile($fileName,$html)
{
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Savvior Project Manager');
$pdf->SetTitle('Auto Generated PDF');
$pdf->SetSubject('Auto Generated PDF');
$pdf->SetKeywords('TCPDF');
// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
//set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP+5, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
//set some language-dependent strings
$pdf->setLanguageArray($l);
// set font
$pdf->SetFont('helvetica', '', 12);
// add a page
$pdf->AddPage();
$pdf->writeHTML($html, true, false, true, false, '');
$doc=$pdf->Output(dirname(__FILE__)."/cache/{$fileName}", 'F');
return $fileName;
}
$file=makePDFFile('poo-poo-platter.pdf',$html);
header("location: cache/{$file}");
?>
Comparing this new code to my old reveals no insight into why this works... in fact the example in the TCPDF examples folder exhibits the same issue, however if you run it from their website the footer is displayed correctly. Well anyway hope this helps someone

I know nothing about TCPDF save what I just learned going through their docs.
It looks like Footer() is only called for you when you explicitly call AddPage(), at which point it is added to the PREVIOUS PAGE. The rest of the time I believe you have to call it yourself.
There's also this whole StartPage()/EndPage() thing that sounds like an alternative to AddPage().
You might want to: "start page, header, draw text, footer, end page" instead. It looks like Write() calls AddPage() for you, which is why the headers and footers on all-but-the-last-page are present.
Bottom Line: Just call Footer() after you call Write() in this example. Real world examples will almost certainly be a bit more complex.

Related

Codeigniter TCPDF - Load Multiple PDF

Current Issue:
When i click on "Print PDF" it only show "1" PDF.
What I Have Done:
I have set array on the URI , which means that it able to loop 2 times
using foreach , but problem now is that the PDF is currently showing
"1" time only
What I Need :
Allow TCPDF generate 2 pdf
Create this thread is whated to know the idea, i will try to solve it myself
Code
<?php
foreach($invoice as $row){
$body='HTML
XXXXXXX
Table';
$pdf = new Pdf('P', 'mm', 'A4', true, 'UTF-8', false);
$pdf->SetTitle('Invoice');
$pdf->SetHeaderMargin(10);
$pdf->SetTopMargin(10);
$pdf->setFooterMargin(10);
$pdf->SetAutoPageBreak(true);
$pdf->SetAuthor('Invoice');
$pdf->SetDisplayMode('real', 'default');
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->AddPage();
$pdf->writeHTML($body, true, false, true, false, '');
$pdf->lastPage();
//$pdf->Write(5, $tnc);
$pdf->Output('InvoceOutput'.'\.pdf', 'I' );
}
?>
You're creating and outputting your PDF inside your for loop, so 2 separate PDFs that are 1 page long are being generated. The 2nd PDF is likely overwriting the 1st PDF, leaving you with a single PDF 1 page long.
Configuring and outputting your PDF outside of the for loop should fix your issue:
<?php
$pdf = new Pdf('P', 'mm', 'A4', true, 'UTF-8', false);
$pdf->SetTitle('Invoice');
$pdf->SetHeaderMargin(10);
$pdf->SetTopMargin(10);
$pdf->setFooterMargin(10);
$pdf->SetAutoPageBreak(true);
$pdf->SetAuthor('Invoice');
$pdf->SetDisplayMode('real', 'default');
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
foreach($invoice as $row){
$body='HTML
XXXXXXX
Table';
$pdf->AddPage();
$pdf->writeHTML($body, true, false, true, false, '');
$pdf->lastPage();
//$pdf->Write(5, $tnc);
}
$pdf->Output('InvoceOutput'.'\.pdf', 'I' );
?>

TCPDF margins on different page by auto page break

https://ibb.co/qYH0x20
I'm using TCPDF for label PDF on my project.
I set custom footer function. In footer function, there are different margins for second page.
I'm writing text by MultiCell method. But on the project image,
$pdf->MultiCell(0, 0, $txt, 1, 'L', 0, 1, '', '', true, 0, true);
// This code is in the Extended footer method.
// $t2 = is page front or back
if ($t2 == 2) { $this->SetMargins(2, 1, 7, true); } else { $this->SetMargins(7, 1, 2, true); }
Where is the problem??

PDF generation with Japanese fonts in Codeigniter using TCPDF

I am facing problem to generate japanese text based pdf using TCPDF. Previously I was working in raw php, html and css and tcpdf was working just fine with the following code:
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
if (#file_exists(APPPATH . 'libraries/tcpdf/examples/lang/eng.php')) {
require_once(APPPATH . 'libraries/tcpdf/examples/lang/eng.php');
$pdf->setLanguageArray($l);
}
if (#file_exists(APPPATH . 'libraries/tcpdf/examples/lang/jpn.php')) {
require_once(APPPATH . 'libraries/tcpdf/examples/lang/jpn.php');
$pdf->setLanguageArray($l);
}
$pdf->setLanguageArray($l);
$pdf->setPrintHeader(false);
$pdf->setFontSubsetting(true);
$pdf->SetFont('cid0jp', '', 11);
$pdf->AddPage();
$pdf->writeHTML($html, true, false, true, false, '');
$pdf->Output('result.pdf', 'I');
which can generate my desired pdf with japanese text.
氏名
(全角・名字と名前の間に字スペー
ス)
Name
(Last Name/First name)
But when I tried to include this in codeigniter controller, the japanese texts are showing question marks in the generated pdf:
require_once(APPPATH . 'libraries/tcpdf/tcpdf.php');
The pdf output becomes like the following:
??
(?????????????????)
Name
(Last Name/First name)
What I am missing? Can anybody give me a solution? I will greatly appreciate a help here.
You can try any one from listed (because I'm not sure which one works for you):-
1) $pdf->SetFont('kozgopromedium', '', 11);
2) $pdf->SetFont('kozminproregular', '', 11);
3) $pdf->SetFont('cid0jp', '', 11);
4) $pdf->SetFont('arialunicid0', '', 11);
5) $pdf->SetFont('arialuni', '', 12);
If you have font file(.ttf) in your system, then you can give path also like :-
$pdf->addTTFfont('path/myfont.ttf', '', '', 11);

TCPDF Issue With Adding Pages

I am trying to add a landscape page in the middle of a portrait PDF generated from HTML. I have set AutoPageBreak to true, but this results in the pages overlapping when I call AddPage(). For example:
$pageBody = "<h1>Test</h1><p>Long content here so that auto page break comes into effect</p>";
$pageBody .= "<br pagebreak=\"true\" /><h2>Page Two</h2>";
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->AddPage();
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$pdf->writeHTMLCell(170, '', 20, 50, $pageBody, 0, 0, false, true, '', true);
$pdf->endPage();
$pageBody2 = "<h1>Test Page 3 Landscape</h1>";
$pdf->AddPage('L');
$pdf->writeHTMLCell(170, '', 20, 50, $pageBody2, 0, 0, false, true, '', true);
$pdf->endPage();
$pdf->Output('my.pdf', 'I');
That results in the first pages displaying correctly (after being auto page broken), but the second content, pageBody2, being overlapped on top of the first set of page(s).
You need to change the $ln (7th) parameter of the writeHTMLCell() calls in your AutoPageBreak-broken pages from 0 to 1 (or 2):
$pdf->writeHTMLCell(170, '', 20, 50, $pageBody, 0, 1, false, true, '', true);
so that the new landscape page goes to the beginning of the next line (1) or below (2) the last box of $pageBody instead of to the right of it.
for adding pagebreak,use
<br pagebreak="true"/>
or
<tcpdf method="AddPage" />

How to send the file inline to the browser using php with TCPDF

I am new in PHP. i am using TCPDF to generate pdf file with php coding. I have a problem. I am not able to send file inline to the browser. when i click on file link it begin to start. I want to send it to the browser inline.
Here is my Code
<?php
require_once('tcpdf/tcpdf.php');
class MYPDF extends TCPDF {
//Page header
public function Header() {
// Logo
$image_file ='image/pacra.jpg';
$this->Image($image_file, 100, 05, 15);
// Set font
$this->SetFont('helvetica', 'I', 15);
// Title
$this->SetTextColor(0,63,127);
$this->Cell(0,50, 'The Pakistan Credit Rating Agency Limited', 0, false, 'C', 0, '', 0, false);
// $this->setColor(0,63,127);
$this->Line(10,30,200,30);
/*$style = array('width' => 0.5, 'cap' => 'butt', 'join' => 'miter', 'dash' => '10,20,5,10', 'phase' => 10, 'color' => array(255, 0, 0));
$this->Line(5, 10, 80, 30, $style);*/
}
// Page footer
public function Footer() {
// Position at 15 mm from bottom
$this->SetY(-25);
// Set font
$this->SetFont('helvetica', 'I', 8);
// Page number
$this->Cell(0, 05, 'Awami Complex FB-1, Usman Block, New Garden Town, Lahore - 54600, Pakistan'
, 0, false, 'C', 0, '', 0, false, 'T', 'M');
$this->Ln();
$this->Cell(0, 05, 'PABX: 92(42)3586 9504 Fax: 92(42)3583 0425 E-mail: pacra#pacra.com'
, 0, false, 'C', 0, '', 0, false, 'T', 'M');
}
}
// create new PDF document
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$pdf->AddPage();
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="test123"; // Database name
$tbl_name="form"; // Table name
$con = mysqli_connect('localhost','root','');
mysqli_select_db($con,"test123");
$sql="SELECT * FROM form WHERE Id=34";
$result = mysqli_query($con,$sql);
while($rows= (mysqli_fetch_array($result,MYSQLI_ASSOC)))
{
$name = $rows['Name'];
$address = $rows['Address'];
$class = $rows['Designation'];
$phone = $rows['Text'];
$pdf->SetXY(10,32);
$pdf->SetFontSize(12);
$pdf->SetTextColor(0,63,127);
$pdf->writeHTML($name, true, false, true, false, '');
$pdf->SetTextColor(0,0,0);
$pdf->SetXY(180,32);
$pdf->writeHTML($address, true, false, true, false, '');
$pdf->SetXY(10,36);
$pdf->SetTextColor(0,0,0);
$pdf->writeHTML($class, true, false, true, false, '');
$pdf->SetXY(10,45);
$pdf->writeHTML($phone, true, false, true, false, '');
}
$html= '<h6>This is test paragraph</h6>
<br>
<h6>This is another test paragraph</h6>
';
$pdf->writeHTML($html, true, false, true, false, '');
$pdf->Output('test.pdf','I');
?>
Although it works with I, it works for O as well.
Try
$pdf->Output('name.pdf', 'O');
If the above does not work, it is just better to use the php header() function.
header("Content-type: application/pdf");
After using the header() function, just echo the content of the PDF file you created.
This is what I found out in the documentation.
I: send the file inline to the browser (default). The plug-in is used if available. The name given by name is used when one selects
the "Save as" option on the link generating the PDF.
D: send to the browser and force a file download with the name given by name.
F: save to a local server file with the name given by name.
S: return the document as a string (name is ignored).
FI: equivalent to F + I option
FD: equivalent to F + D option
E: return the document as base64 mime multi-part email attachment (RFC 2045)

Categories