PDF generation with Japanese fonts in Codeigniter using TCPDF - php

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

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

Hindi is not displaying properly in TCPDF

I am trying to generate PDF using TCPDF with Hindi Unicode characters. I tried Mangal, Noto Sans, Arial Unicode fonts but all give the same output. If there is a "Matra" or "Half character" then it does not display properly.
See this output:
<?php
error_reporting(E_ALL);
ini_set("display_errors", "On");
require_once 'vendor/autoload.php';
$hindi_str = "राष्ट्रपति प्रणव मुखर्जी। ";
$hindi_str2 = "देश हमें देता है सब कुछ";
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
//$fontname_kannada = TCPDF_FONTS::addTTFfont('NotoSansKannada-Regular.ttf', 'TrueTypeUnicode', '', 96);
$fontname_hindi1 = TCPDF_FONTS::addTTFfont('NotoSansDevanagari-Regular.ttf', 'TrueTypeUnicode', '', 96);
$fontname_hindi2 = TCPDF_FONTS::addTTFfont('mangal.ttf', 'TrueTypeUnicode', '', 96);
$fontname_hindi3 = TCPDF_FONTS::addTTFfont("ARIALUNI.TTF", 'TrueTypeUnicode', '', 96);
// use the font
$pdf->AddPage();
$pdf->SetFont($fontname_hindi3, '', 14, '', false);
$pdf->writeHTML("<h1>Hindi $hindi_str $hindi_str2</h1>",true, false, true, false, '');
$pdf->Ln();
$pdf->writeHTML("<h1>Kannada ಖುತುಬ್ ಮಿನಾರ್ ಸಮೀಪವಿರುವ ಪ್ರಸಿದ್ಧ ಕಬ್ಬಿಣದ ಕಂಬ ಯಾರಿಗೆ ಸೇರಿದೆ</h1>",true, false, true, false, '');
// output the HTML content
// ---------------------------------------------------------
//Close and output PDF document
$pdf->Output('example.pdf', 'I');
Please help.
I know it's a little late, but I had the same problem for nepali (which also uses devanagari). I solved it like this:
// SOME FONTS DOES NOT SUPPORT DEVANAGARI, BUT FREESANS DOES
$pdf->SetFont('freesans', '', 10);
// MAKE ARRAY WITH UTF LANGUAGE IDENTIFIER
$lg = Array();
$lg['a_meta_charset'] = 'UTF-8';
$lg['a_meta_dir'] = 'ltr';
$lg['a_meta_language'] = 'np'; // I think you can change this to HI or IN for hindi
$lg['w_page'] = 'page';
// CHANGE SETTINGS IN TCPDF
$pdf->setLanguageArray($lg);
// MAKE SURE TO USE WriteHTML() function
$pdf->WriteHTML($yourhtml, true, 0, true, 0);
You can use this if Hindi is not showing on TCPDF
$pdf->SetFont('freesans', '', 10);
$lang = Array();
$lang['a_meta_charset'] = 'UTF-8';
$lang['a_meta_dir'] = 'ltr';
$lang['a_meta_language'] = 'IN';
$lang['w_page'] = 'page';
$pdf->setLanguageArray($lang);
$pdf->WriteHTML($html, true, 0, true, 0);

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" />

TCPDF - weird issue with footers

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.

TCPDF UTF8 - weird spacing between characters

Pls have a look at the following picture http://dev.mosaicware.pl/tmp/PDF-utf-problem.png
As you can se PL and RO characters are looking weird, they look fine on the webpage. Any clue?
I've used arialunicid0 font and following code (characters in DB are UTF-8):
$pdf = new models_PdfUtf(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', true);
$pdf->SetFont('arialunicid0'); $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
$pdf->SetAuthor($problem . ' ' . $problem['user_surname']); $pdf->SetTitle($problem['problem_title']);
$pdf->SetSubject($problem['problem_title']);
$pdf->Output('export.pdf','I');
$pdf = new models_PdfUtf(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', true);
$pdf->SetFont('arialunicid0');
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
$pdf->setPrintFooter(false);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$pdf->AddPage();
$pdf->writeHTML($c, true, 0, true, 0);
$pdf->SetAuthor($problem . ' ' . $problem['user_surname']);
$pdf->SetTitle($problem['problem_title']);
$pdf->SetSubject($problem['problem_title']);
$pdf->Output('export.pdf','I');
Unfortunately, the picture is now password protected, but from your description I'd guess that there is something in the HTML. I encountered a behavior as described by cou with links. Could you check if $c contains any links at all?

Categories