'How to set the encode in PhpSpreadsheet\Reader\Xlsx?
I want to use PhpSpreadsheet convert xlsx to pdf, it's succeed. But the Chinese word in xlsx is error. And the setInputEncoding method is not found. What can I do? please give me guide
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
$spreadsheet = $reader->load($public['root'].'/' . $fileName);
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Mpdf');
$pdfName = str_replace($extension, 'pdf', $fileName);
$writer->save($public['root'].'/' . $pdfName);
Solution 1:[1]
You can set Font Family in file xlsx.
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
$spreadsheet = $reader->load($public['root'].'/' . $fileName);
$styleArray = array(
'font' => array(
'name' => 'Verdana' // change your font
));
$spreadsheet->getDefaultStyle()->applyFromArray($styleArray);
Solution 2:[2]
The question is fixed. First I download the yahei.ttf and put it to mpdf/ttfonts. Then regist it in mpdf/src/Config/FontVariables.php. Finally find the line 41 at PhpSpreadsheet/Writer/Pdf/Mypdf.php and change the code like this
$pdf = $this->createExternalWriterInstance(array_merge($config, ['default_font' => 'yahei']));
$pdf->setDisplayMode('fullpage');
$pdf -> useAdobeCJK = TRUE;
$pdf ->autoScriptToLang = true;
$pdf -> autoLangToFont = true;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | poppies |
Solution 2 | shawn |