i trying use tcpdf library have pdf file supports arabic letters. have created table , data mysql database.
<?php // include main tcpdf library (search installation path). require_once('tcpdf/config/tcpdf_config.php'); require_once('tcpdf/tcpdf.php'); class pdf extends tcpdf{ var $tablewidths; var $headerset; var $footerset; function morepagestable($lineheight=8) { // things set , 'remember' $l = $this->lmargin*2; $startheight = $h = $this->gety(); $startpage = $currpage = $this->page; // calculate whole width foreach($this->tablewidths $width) { $fullwidth += $width; } // let's start write table $row = 0; while($data=mysql_fetch_row($this->results)) { $this->page = $currpage; // write horizontal borders $this->line($l,$h,$fullwidth+$l,$h); // write content , remember height of highest col foreach($data $col => $txt) { $this->page = $currpage; $this->setxy($l,$h); $this->multicell($this->tablewidths[$col],$lineheight,$txt,0,$this->colalign[$col]); $l += $this->tablewidths[$col]; if($tmpheight[$row.'-'.$this->page] < $this->gety()) { $tmpheight[$row.'-'.$this->page] = $this->gety(); } if($this->page > $maxpage) $maxpage = $this->page; unset($data[$col]); } // height in last used page $h = $tmpheight[$row.'-'.$maxpage]; // set "pointer" left margin $l = $this->lmargin*2; // set $currpage last page $currpage = $maxpage; unset($datas[$row]); $row++ ; } // draw borders // start adding horizontal line on last page $this->page = $maxpage; $this->line($l,$h,$fullwidth+$l,$h); // start @ top of document , walk down for($i = $startpage; $i <= $maxpage; $i++) { $this->page = $i; $l = $this->lmargin*2; $t = ($i == $startpage) ? $startheight : $this->tmargin; $lh = ($i == $maxpage) ? $h : $this->h-$this->bmargin; $this->line($l,$t,$l,$lh); foreach($this->tablewidths $width) { $l += $width; $this->line($l,$t,$l,$lh); } } // set last page, if not it'll cause problems $this->page = $maxpage; } connect($host='xxxx',$username='xxxx',$passwd='xxxx',$db='xxxxx') { $this->conn = mysql_connect($host,$username,$passwd) or die( mysql_error() ); mysql_select_db($db,$this->conn) or die( mysql_error() ); return true; } function query($query){ $this->results = mysql_query($query,$this->conn); $this->numfields = mysql_num_fields($this->results); } function mysql_report($query,$dump=false,$attr=array()){ foreach($attr $key=>$val){ $this->$key = $val ; } $this->query($query); // if column widths not set if(!isset($this->tablewidths)){ // starting col width $this->scolwidth = (($this->w-$this->lmargin*2-$this->rmargin))/$this->numfields; // loop through results header , set initial col widths/ titles/ alignment // if col title less starting col width / reduce column size for($i=0;$i<$this->numfields;$i++){ $stringwidth = $this->getstringwidth(mysql_field_name($this->results,$i)) + 8 ; if( ($stringwidth) < $this->scolwidth){ $colfits[$i] = $stringwidth ; // set column titles less start width column title width } $this->coltitles[$i] = mysql_field_name($this->results,$i) ; switch (mysql_field_type($this->results,$i)){ case 'int': $this->colalign[$i] = 'l'; break; default: $this->colalign[$i] = 'l'; } } // loop through data, column contents bigger col size // resized while($row=mysql_fetch_row($this->results)){ foreach($colfits $key=>$val){ $stringwidth = $this->getstringwidth($row[$key]) + 6 ; if( ($stringwidth) > $this->scolwidth ){ // col row bigger start width discarded unset($colfits[$key]); }else{ // if text not bigger current column width setting enlarge column if( ($stringwidth) > $val ){ $colfits[$key] = ($stringwidth) ; } } } } foreach($colfits $key=>$val){ // set fitted columns smallest size $this->tablewidths[$key] = $val; // work out how (if any) space has been freed $totalreadyfitted += $val; } $surplus = (sizeof($colfits)*$this->scolwidth) - ($totalreadyfitted); for($i=0;$i<$this->numfields;$i++){ if(!in_array($i,array_keys($colfits))){ $this->tablewidths[$i] = $this->scolwidth + ($surplus/(($this->numfields)-sizeof($colfits))); } } ksort($this->tablewidths); if($dump){ header('content-type: text/plain'); for($i=0;$i<$this->numfields;$i++){ if(strlen(mysql_field_name($this->results,$i))>$flength){ $flength = strlen(mysql_field_name($this->results,$i)); } } switch($this->k){ case 72/25.4: $unit = 'millimeters'; break; case 72/2.54: $unit = 'centimeters'; break; case 72: $unit = 'inches'; break; default: $unit = 'points'; } print "all measurements in $unit\n\n"; for($i=0;$i<$this->numfields;$i++){ printf("%-{$flength}s : %-10s : %10f\n", mysql_field_name($this->results,$i), mysql_field_type($this->results,$i), $this->tablewidths[$i] ); } print "\n\n"; print "\$pdf->tablewidths=\n\tarray(\n\t\t"; for($i=0;$i<$this->numfields;$i++){ ($i<($this->numfields-1)) ? print $this->tablewidths[$i].", /* ".mysql_field_name($this->results,$i)." */ \n\t\t": print $this->tablewidths[$i]." /* ".mysql_field_name($this->results,$i)." */\n\t\t"; } print "\n\t);\n"; exit; } } else { // end of if tablewidths not defined for($i=0;$i<$this->numfields;$i++){ $this->coltitles[$i] = mysql_field_name($this->results,$i) ; switch (mysql_field_type($this->results,$i)){ case 'int': $this->colalign[$i] = 'r'; break; default: $this->colalign[$i] = 'l'; } } } mysql_data_seek($this->results,0); $this->open(); $this->sety($this->tmargin); $this->addpage(); $this->morepagestable($this->fontsizept); $this->output(); } } $pdf = new pdf(pdf_page_orientation,pdf_unit,pdf_page_format,true, 'utf- 8', false); $pdf->setfont('aealarabiya', '', 14, '', false); $pdf->connect('xxxxxxxxxx','xxxxxxxxxxxxxx','xxxxxxxxxxxxx','xxxxxxxxxxxxx'); $attr=array('titlefontsize'=>24,'titletext'=>'this pdf file'); $pdf->mysql_report("select * student_table",false,$attr); ?> i ok table created code. arabic data retrieved following?how can solve ?
your problem simple bro, need choose correct database encoding , query , before going , think duty tell you don't have use "mysql" have use "mysqli" ,now let solve problem :
- set database encoding (utf8_general_ci). take backup don't forget old stuff destroyed if change charset of database if it's new or empty
after mysqli connect $db->set_charset("utf8"); or if still want work mysql use mysql_set_charset("utf8");
here edited code:
connect($host='xxxx',$username='xxxx',$passwd='xxxx',$db='xxxxx') { $this->conn = mysql_connect($host,$username,$passwd) or die( mysql_error() ); mysql_select_db($db,$this->conn) or die( mysql_error() ); mysql_set_charset("utf8"); return true; }
but, told , did in wrong way , use mysqli , full oop not time , in other not !
Comments
Post a Comment