A
A
Anya Ivanova2019-10-29 12:01:15
PHP
Anya Ivanova, 2019-10-29 12:01:15

How to change encoding in phpword?

I generate at first word a file, then in pdf. Questions instead of Russian letters. I so understood it is necessary to make something with the coding. On the Internet, it was advised to comment out the encoding in the section.php setting.php file, insert the header with the encoding. But nothing changes. Tell me comrades?

<?
require 'vendor/autoload.php';

 $phpWord->setDefaultFontName('Arial');
$phpWord->setDefaultFontSize(14);

$phpWord = new  \PhpOffice\PhpWord\PhpWord();
  
$document = $phpWord->loadTemplate('template1.docx');

$document->setValue('name', $name);

require __DIR__.'/vendor/dompdf/vendor/autoload.php';

use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\Settings;

// Make sure you have `dompdf/dompdf` in your composer dependencies.
Settings::setPdfRendererName(Settings::PDF_RENDERER_DOMPDF);
// Any writable directory here. It will be ignored.
Settings::setPdfRendererPath('dompdf/');

$phpWord = IOFactory::load('template1.docx', 'Word2007');
$phpWord->save('document.pdf', 'PDF');

     
 ?>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
inFureal, 2019-10-31
@inFureal

iconv(mb_detect_encoding($text, mb_detect_order(), true), "UTF-8", $text);

D
demach, 2021-02-08
@demach

The problem may be not so much in phpword, but in rendering (dompdf, TCPDF). Using dompdf as an example, a possible solution is: use a DejaVu family font
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$phpWord->setDefaultFontName('dejavu sans');
\PhpOffice\PhpWord\Settings::setPdfRendererPath('./dompdf');
\PhpOffice\PhpWord\Settings::setPdfRendererName('DomPDF');
Help the seeker

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question