How to Implement Malayalam font Meera for pdf generation using mpdf - php

In my function have added the following codes in mpdf->confing_cp.php
CASE "meera": $unifonts = "meera,meeraI"; break;
also Added the following code in mpdf->config_font.php
"meera" => array(
'R' => "Meera.ttf",
),
And in my controller i need the lanscape view also ,so following are the codes in my controller to generte pdf
`
function export_pdf() {
$this->data['admin_page_title'] = 'Script';
$this->data['portrite'] = $this->input->post('formate');
this->data['script_pdf']=$this->Script_model->get_selected();
ini_set('memory_limit','250M');
$html = $this->load->view('admin/script/pdf_view', $this->data,true);
//include_once APPPATH.'/third_party/mpdf/mpdf.php';
$this->load->library('pdf');
$pdf = $this->pdf->load();
$pdf = new mPDF('ml', 'A4');
$pdf->AddFont('meera','','meera.ttf',true);
$pdf->SetFont('meera','',14);
if($this->data['portrite']=="landscape") {
$pdf=new mPDF('utf-8', 'A4-L');
}
// $pdf->SetAutoFont(AUTOFONT_ALL);
// $pdf->setHTMLFooter($footer);
$pdf->WriteHTML($html);
$pdf->Output();
exit;`
}`

Related

DefHTMLHeaderByName doesn't seem to work mpdf

I am trying to get a document generated on a php site. It is all working well but the last thing is to add a header to the top of every page after the title page. I have tried so many things and the closest I can get to what I want is the following (stripped down) code.
ob_start();
$mpdf = new Mpdf();
$mpdf->SetDisplayMode('fullpage');
$mpdf->bleedMargin = 0;
$mdpf->crossMarkMargin = 0;
$mpdf->cropMarkMargin = 0;
$mpdf->nonPrintMargin = 0;
// Home page and headers...
$html_header = 'Some Code here';
$mpdf->DefHTMLHeaderByName('PgHeader', $html_header);
$mpdf->AddPage();
$mpdf->Image('cover.jpg', 0, 0, 210, 297, 'jpg', '', true, false);
// Get the content of any pages we have
if($pdf_pages) {
foreach($pdf_pages as $key=>$pg) {
$mpdf->AddPageByArray(array(
'mgt' => '40',
'odd-header-name' => 'PgHeader'
));
$html .= 'the content from the loop';
// Write some HTML code:
$custom_sheet = file_get_contents('/custom.css');
$mpdf->WriteHTML($custom_sheet, 1);
$mpdf->WriteHTML($html, 2);
}
}
// Output a PDF file directly to the browser
$mpdf->Output('Name.pdf', 'I');
ob_end_flush();
I have tried changing the odd-header-name to html_PgHeader based on the documentation but in either case, I get the margin, all the content but the header never shows up. Why? Why is it not being assigned so I can call it in the AddPageByArray?
According to the documentation you need to prefix header/footer names with 'html_'
Try changing;
'odd-header-name' => 'PgHeader',
to;
'odd-header-name' => 'html_PgHeader',
reference here
To show header, you need to add odd-header-value option to AddPageByArray method with value 1.
ob_start();
$mpdf = new Mpdf();
$mpdf->SetDisplayMode('fullpage');
$mpdf->bleedMargin = 0;
$mdpf->crossMarkMargin = 0;
$mpdf->cropMarkMargin = 0;
$mpdf->nonPrintMargin = 0;
// Home page and headers...
$html_header = 'Some Code here';
$mpdf->DefHTMLHeaderByName('PgHeader', $html_header);
$mpdf->AddPage();
$mpdf->Image('cover.jpg', 0, 0, 210, 297, 'jpg', '', true, false);
$pdf_pages = array('key' => 'val');
// Get the content of any pages we have
if($pdf_pages) {
foreach($pdf_pages as $key=>$pg) {
$mpdf->AddPageByArray(array(
'mgt' => '40',
'odd-header-name' => 'PgHeader',
'odd-header-value' => 1,
));
$html .= 'the content from the loop';
// Write some HTML code:
$custom_sheet = file_get_contents('/custom.css');
$mpdf->WriteHTML($custom_sheet, 1);
$mpdf->WriteHTML($html, 2);
}
}
// Output a PDF file directly to the browser
$mpdf->Output('Name.pdf', 'I');
ob_end_flush();
Pt.II
If this, don't help, then try this scenario:
Open console and go to site's root path.
Create index.php file
Run composer require mpdf/mpdf (install composer, if you don't have it).
Paste this code to the index.php
<?php
require_once __DIR__ . '/vendor/autoload.php';
ob_start();
$mpdf = new \Mpdf\Mpdf();
/** same code from previous example here, except Mpdf init **/
Try something like this
#page {
header: header_name;
footer: footer_name;
}
It might help you.
Url: https://mpdf.github.io/headers-footers/headers-footers.html

How to use mPDF in YII, when I want to generate PDF file from HTML?

$mPDF1 = Yii::app()->ePdf->mpdf();
$mPDF1 = Yii::app()->ePdf->mpdf('', 'A4');
$html= $this->renderPartial('poPdfTemplate', array('pdfValues'=>$ponumberData,'po_number'=>$po_number), true);
$mPDF1->WriteHTML($html,0);
//$mPDF1->WriteHTML($html);
$mPDF1->Output();
The above code is using for generating PDF with YII framework.
First two lines will create css $pdf_style and html file $pdf_part and PDF will be write this file.
Include these 2 variables in the WriteHTML function.
$pdf_style = \Yii::$app->view->renderFile(PROTECTEDPATH . 'common/views/contract/style.css', $data);
$pdf_part = \Yii::$app->view->renderFile(PROTECTEDPATH . 'common/views/contract/file.tpl', $data);
$mpdf = new \mPDF('utf-8','A4');
$mpdf->autoScriptToLang = true;
$mpdf->baseScript = 1; // Use values in classes/ucdn.php 1 = LATIN
$mpdf->autoVietnamese = true;
$mpdf->autoArabic = true;
$mpdf->autoLangToFont = true;
$mpdf->SetMargins(null,null,40);
$mpdf->SetDisplayMode('fullpage');
$mpdf->SetHTMLHeader('<div style="text-align:right"><img src="'. DOCROOT .'images/logo_contract.png" height="68" /></div>');
$mpdf->setFooter(' | www.facebook.com | {PAGENO}');
$mpdf->WriteHTML($pdf_style.'<div class="wrapper">'.$pdf_part.'</div>');
$mpdf->Output('Contract-'.date("iYsmd"), $action, $dest);

Getting TCPDF:Some data has already been output, can't send PDF file in Laravel 4

I am using HTML2pDf convertor to generate pdf my code looks like this
require_once(app_path().'/libs/html2pdf/html2pdf.class.php') ;
$html2pdf = new HTML2PDF('P','A4','en',true,'UTF-8',array(0, 0, 0, 0));
$html2pdf->pdf->SetDisplayMode('fullpage');
$html2pdf->WriteHTML($html22);
$html2pdf->Output($username.'_questionnaire.pdf');
I am getting following error
TCPDF ERROR: Some data has already been output, can't send PDF file
The function where I have this script looks like this
public function postExporttopdf(){
$validator = Validator::make(Input::all(),array('delid'=>'required'));
$event = Session::get('tempevenname');
if($validator->passes()){
$uid1 = Input::get('delid');
$username = User::find(Input::get('delid'))->name;
$page1data = Question::where('event','=',$event)->where('page','=',1)->orderBy('order')->with('choices')
->with(array('answers'=>function($query){
$query->where('user_id','=',Input::get('delid'));
}))->get();
$page2data = Question::where('event','=',$event)->where('page','=',2)->orderBy('order')->with('choices')
->with(array('answers'=>function($query){
$query->where('user_id','=',Input::get('delid'));
}))->get();
$page3data = Question::where('event','=',$event)->where('page','=',3)->orderBy('order')->with('choices')
->with(array('answers'=>function($query){
$query->where('user_id','=',Input::get('delid'));
}))->get();
$page4data = Question::where('event','=',$event)->where('page','=',4)->orderBy('order')->with('choices')
->with(array('answers'=>function($query){
$query->where('user_id','=',Input::get('delid'));
}))->get();
$html22 = View::make('reviewsendpdf')->with(array(
'page1data'=>$page1data,
'page2data'=>$page2data,
'page3data'=>$page3data,
'page4data'=>$page4data
));
require_once(app_path().'/libs/html2pdf/html2pdf.class.php') ;
$html2pdf = new HTML2PDF('P','A4','en',true,'UTF-8',array(0, 0, 0, 0));
$html2pdf->pdf->SetDisplayMode('fullpage');
$html2pdf->WriteHTML($html22);
$html2pdf->Output($username.'_questionnaire.pdf');
} else {
return Redirect::to('admin/exporttopdf')->with('message','Select a user and event');
}
}
Probably something has already been written to the output buffer prior to streaming the pdf content.
Try to use ob_end_clean() to clean the output buffer just before the method invocation:
require_once(app_path().'/libs/html2pdf/html2pdf.class.php') ;
$html2pdf = new HTML2PDF('P','A4','en',true,'UTF-8',array(0, 0, 0, 0));
$html2pdf->pdf->SetDisplayMode('fullpage');
$html2pdf->WriteHTML($html22);
ob_end_clean();
$html2pdf->Output($username.'_questionnaire.pdf');

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.

Cyrillic text with dompdf, print pdf scale error

I need a help with this one. I use PHP and dompdf to create this invoice in Cyrillic:
On pdf everything is looking fine, but when I try to print it, I get it like:
and I get also one extra blank page added. Do you know how this can be fixed?
The example can be seen at: http://projects.stanislavstankov.com/php-pdf/pdfview.php
and sourse downloaded at: http://projects.stanislavstankov.com/php-pdf/php-pdf.rar
PHP code:
<?php
$data = file_get_contents("HTML Invoice Template.html");
$data = iconv('UTF-8//IGNORE','UTF-8//IGNORE', $data);
$data = preg_replace('/<body>/', "", $data);
$data = preg_replace('/<\/body>/', "", $data);
$data = preg_replace('/<html>/', "", $data);
$data = preg_replace('/<\/html>/', "", $data);
// inhibit DOMPDF's auto-loader
define('DOMPDF_ENABLE_AUTOLOAD', false);
//include the DOMPDF config file (required)
require 'extensions/dompdf/dompdf_config.inc.php';
//if you get errors about missing classes please also add:
require_once('extensions/dompdf/include/autoload.inc.php');
//$data = mb_convert_encoding($data, 'HTML-ENTITIES', 'UTF-8');
//$data = utf8_decode($data);
//$data = iconv('Windows-1251','UTF-8', $data);
//
//generate some PDFs!
$dompdf = new DOMPDF(); //if you use namespaces you may use new \DOMPDF()
$dompdf->set_paper('a4', 'portrait');
$dompdf->load_html($data);
//$dompdf->set_paper(array(0,0,595,842));
$dompdf->render();
$dompdf->stream($log_id.".pdf", array("Attachment"=>0));
?>

Categories