vernes / PHPReport

Library for generating reports from PHP
71 stars 50 forks source link

In rendering to 'pdf', the contents with chinese words will all trun to '??'. Why? #12

Closed Amamatthew closed 8 years ago

Amamatthew commented 8 years ago

I found when I render my array datas to 'pdf', the content with chinese words will all trun to '??'. But the datas above will render correctly, whether in 'html' or 'xls' rendering. How can I fix it? Report 2016-07-15.pdf

vernes commented 8 years ago

I have no idea. If it is only pdf, maybe it is a bug in phpexcel? Did you try using phpexcel directly to export files?

Amamatthew commented 8 years ago

I used the domPDF(one of the famous PDF Lib) as the PHPExcel's PDF plugin. And I enclosed with a domPDF's DEMO PHP file of Rendering the HTML as PDF BELOW for you.

//Render the HTML as PDF domPDF_Render_the_HTML_Contents_as_PDF.txt

//ps. Pay attention to the line below in the code above please. //" style="font-family: simsun;"

//It told the domPDF to render the paragraph with font-family 'simsum'. //And the 'simsum' is just one of famous Chinese basic local font-family. //We should install 'simsum' first before we can rendering PDF with it. //See detail instruction in url below: //https://github.com/dompdf/dompdf/wiki/UnicodeHowTo#configure-dompdf-for-unicode-support

//But I just dont know how to specify a font-family in the PHPReport PDF outputing procedure.It seems output the array's data directly and no where to specify the font. //Any other state's local font-family PDF file outputing user had the same problem? I don't know. //Eager the people who meets the same problem can get a perfect solution( to specify the local font-family) too.

//Any hints please tell us, and appreciated with you good job later soon, thank you!

domPDF_Render_the_HTML_Contents_as_PDF.txt

vernes commented 8 years ago

PHPReport is just using PHPExcel pdf rendering:

private function renderPdf($filename)
{
    header('Content-Type: application/vnd.pdf');
    header('Content-Disposition: attachment;filename="'.$filename.'.pdf"');
    header('Cache-Control: max-age=0');
    $this->objWriter = PHPExcel_IOFactory::createWriter($this->objPHPExcel, 'PDF');
    $this->objWriter->save('php://output');
    exit();
 }

I have checked PHPExcel code and I haven't seen an option to change generated HTML.

That's why I asked did you try using PHPExcel only. documentation

Amamatthew commented 8 years ago

Well thank you for your in time explaining. In file line 204~219 of PHPReport.php, I find the function below. It seems a good news for us to be able to set the font-family by ourself through add one line: $this->objPHPExcel->getDefaultStyle()->getFont()->setName('simsun'); You can have a try too please. (Bad news is: It seems still have some bug, when I add this line, the first line with column's name of the table is goned. So I leave one issue for PHPExcel) /* * Creates PHPExcel object and template for report / private function createTemplate() { $this->objPHPExcel = new PHPExcel(); $this->objPHPExcel->setActiveSheetIndex(0); $this->objWorksheet = $this->objPHPExcel->getActiveSheet(); //TODO: other parameters $this->objWorksheet->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_PORTRAIT); $this->objWorksheet->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4); $this->objWorksheet->getPageSetup()->setHorizontalCentered(true); $this->objWorksheet->getPageSetup()->setVerticalCentered(false);

    $this->_usingTemplate=false;
}