Codeigniter TCPDF - Load Multiple PDF - php

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

Related

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 returning weird characters

maybe you can help me. When I try to show a pdf I created I got some weird symbols like this
u\>�'���O���r>c!%�#�R�`YPd+��vv����1��E�'^k-�WD�*+��W^��wy��V Z��dUdJ�B���C�ڳtK����j:c���5����50���D3lgH#�}%���D+������ix����,��-�'\�� �_st^&0�Y���������v�*Ӗ,W����u!H��sNN��0cӝ��`xEk��d��^� �8K9�BL����9�̋"6/�E�|�̛�-�7�P��B�#�T�F���4`����
What I do is transform a html file with this code
$html = $this->load->view('ReporteIngresoView', $data, TRUE);
$pdf = new Pdf('P', 'mm', 'A4', TRUE, 'UTF-8', FALSE);
$pdf->SetTitle('Reporte ingreso');
$pdf->SetHeaderMargin(30);
$pdf->SetTopMargin(40);
$pdf->setFooterMargin(20);
$pdf->SetAutoPageBreak(TRUE);
$pdf->SetAuthor('Pontificia Universidad Católica del Ecuador');
$pdf->SetDisplayMode('real', 'default');
$pdf->AddPage();
$pdf->writeHTML($html, TRUE, 0, TRUE, 0);
$pdf->Output('My-File-Name.pdf', 'I');
On the other side I recieve it like this
Ext.Ajax.request
(
{
method: 'post',
url: '../servidor/archivo/ingreso/getreporte',
success: function(response){
Ext.getCmp('winReporteRegistro').update( response.responseText );
}
}
);
I am using php (server), html (create the page), extjs (interface), ajax (request) and TCPDF to create the pdf.
Any ideas?
Thanks in advance,
You need to convert the response to a Blob (link)
After creating the blob, you can create an URL object (link)
This is how I create a server side generated pdf and display it on the front end.
Adding code snippet:
var blob = new Blob(response.responseText, {type: 'application/pdf'});
var objectURL = URL.createObjectURL(blob);
Returning the location of the file and open that in another web page was the answer, the code is:
Server:
$html = $this->load->view('ReporteCajaView', $data, TRUE);
$pdf = new Pdf('P', 'mm', 'A4', TRUE, 'UTF-8', FALSE);
$pdf->SetTitle('Reporte item');
$pdf->SetHeaderMargin(30);
$pdf->SetTopMargin(20);
$pdf->setFooterMargin(20);
$pdf->SetMargins(25, 20, 20, true);
$pdf->SetAutoPageBreak(TRUE);
$pdf->SetAuthor('Universidad Católica');
$pdf->SetDisplayMode('real', 'default');
$pdf->setPrintHeader(FALSE);
$pdf->setPrintFooter(FALSE);
$pdf->AddPage();
$id = uniqid();
$pdf->writeHTML($html, TRUE, 0, TRUE, 0);
$pathservidor = 'c:/wamp64/www/Archivo/pdf/';
$pdf->Output($pathservidor . 'Reporte de caja' . '.pdf', 'F');
echo 'c:/wamp64/www/Archivo/pdf/'.'Reporte de caja'.'.pdf';
Client:
Ext.Ajax.request
(
{
method: 'post',
url: '../servidor/archivo/item/getreporte',
success: function(response){
window.open(response.responseText);
}
}
);
Thanks!
Sorry for the late answer but I broke my teeth on this yesterday and this morning I had an illumination :-)
You need to support the mime-type so simply add this in your .htaccess:
AddType application/pdf .pdf

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

PDF is not generated in codeigniter and display blank

I am working in codeigniter.I have downloaded TCPDF for creating pdf. I have followed all step for generating pdf in codeigniter.
My controller is:
function list_branch_report()
{
if($this->input->post('submit'))
{
$this->load->library('Pdf');
$pdf = new Pdf('P', 'mm', 'A4', true, 'UTF-8', false);
$pdf->SetTitle('Pdf Example');
$pdf->SetHeaderMargin(30);
$pdf->SetTopMargin(20);
$pdf->setFooterMargin(20);
$pdf->SetAutoPageBreak(true);
$pdf->SetAuthor('Author');
$pdf->SetDisplayMode('real', 'default');
$pdf->Write(5, 'CodeIgniter TCPDF Integration');
$pdf->AddPage();
$pdf->Output('pdfexample.pdf', 'I');
$branch_id = $this->input->post('br_name');
$branch_code = $this->input->post('branch_code');
$query1 = $this->db->query("select * from branch_bal where branch_id = '$branch_id'");
$result1 = $query1->result();
$query2 = $this->db->query("select * from cash_depo where bid = '$branch_id'");
$result2 = $query2->result();
$this->load->view("admin/list_branch_report",array('br_result'=>$result1,'cash_result'=>$result2,'b_code'=>$branch_code));
}
And my view page is:
<div style="float:left;width:100%">
<center><h1> Branch Report </h1></center>
</div>
When I try to generate pdf then it will display blank.
where are you writing HTML to PDF I haven't work on tcpdf but as my experience with mpdf as far as i know
there should a function like
$pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
please refer tcpdf documentation for writeHTML() or writeHTMLCell() functions
http://www.tcpdf.org/doc/code/classTCPDF.html
or refer examples
http://www.tcpdf.org/examples.php
you have to assign codeigniter view to variable for that give last attribute as true
$html= $this->load->view("admin/list_branch_report",array('br_result'=>$result1,'cash_result'=>$result2,'b_code'=>$branch_code),true);
and then assign this to
$pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
I have written ob_start(); before the load view page.Then i have written
$html = $this->load->view("admin/list_branch_report",array('br_result'=>$result1,'cash_result'=>$result2,'b_code'=>$branch_code),true);
$pdf->writeHTML($html, true, false, true, false, '');
ob_clean();
$pdf->Output();
this code.
So it works perfect.
Thanks to all.
use mpdf instead of tpdf and send html that you want to convert in pdf.

PDF/A exsiting pdf with TCPDF and FPDI

Hello my task is to certify pdf with digital Signature Certification and release it in pdf/a format. I tried using tcpdf, but i couldn't import existing pages. So I added fpdi, kind of mixing them:
require_once('./tcpdf/tcpdf.php');
require_once('./tcpdf/fpdi.php');
$pdf = new FPDI( );
//$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false, true);
$file = realpath("484.pdf");
$pagecount = $pdf->setSourceFile($file);
for($i = 1 ; $i <= $pagecount ; $i++){
$tpl = $pdf->importPage($i);
$size = $pdf->getTemplateSize($tpl);
$orientation = $size['h'] > $size['w'] ? 'P':'L';
$pdf->AddPage($orientation);
$pdf->useTemplate($tpl, null, null, $size['w'], $size['h'], true);
}
$pdf->SetCreator("Creator");
$pdf->SetTitle('123Titel');
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$certificate = 'file://123.crt';
$info = array(
'Name' => '123test',
'Location' => 'place',
'Reason' => '123',
'ContactInfo' => '123',
);
$pdf->setSignature($certificate, $certificate, '123', '', 2, $info);
$pdf->SetFont('helvetica', '', 12);
$pdf->addEmptySignatureAppearance(0, 0, 0, 0);
$pdf->Output('test.pdf', 'F');
Alright, so I can put signature in alright with this, but i cant make it pdf/a.
What decides pdf/a format is
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false, true);
The last "true". But I cant use this tcpdf function, or i get:
Call to undefined method TCPDF::setSourceFile() in...
So im forced to use $pdf = new FPDI( );, which can't save pdf in pdf/a format
Surely someone knows something that im missing and im running out of ideas on what to do.
-Can I import existing pdf with only tcpdf and if yes HOW?
-Is there any other way to make file format pdf/a (i coudlnt find any)
-Any tips whatsoever
It appears that I can extend tcpdf with fpdi. Somehow it didnt work until i changed fpdi_bridge to always extend tcpdf and changed
new FPDI();
to
new FPDI(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false, true);

Categories