Solution of Chinese garbled code problem when phpword plug in exports word file

  • 2021-07-10 19:18:02
  • OfStack

In the recent development of a project, PHP technology is used to export Word documents. Several schemes are compared. First, ActiveX/COM components of Microsoft Office are used, such as Word. Application. The advantage of this method is that it has high format compatibility and can generate Word2003 format documents of pure doc. The disadvantage is that it takes up resources (calling will start an WINWORD. EXE process), which is not suitable for Web multi-user access. 2 is that PHP, the Web development technology, mostly runs on Linux server, so it is impossible to use the technology under Windows, and the platform is portable and compatible.

The second scheme to generate Word is to generate Word compatible web page format and then open it in Word mode. This scheme generally feels strange. After all, the file format is HTML, and the format compatibility is not good, but the advantage of this method is that it saves server resources and can be generated quickly; The last scheme is today's protagonist, PHPWord is used to generate documents in Word 2007 (docx) format. Now, basically, Microsoft Office Word 2003 and later versions are compatible with this format. For the 2003 version, you only need to download and install a compatible format package (download address), and you can also open this kind of files normally. Of course, if you use the latest version of Office (including but not limited to Office 2007 and Office 2010), you don't need to install this format package.

Ok, let me introduce PHPWord below. You can download and get more information about the project by visiting the project homepage.

In the process of using, I mainly encountered the problem of Chinese garbled codes. Combined with the guidance of online gods, I solved this kind of problem in the following ways, hoping to help everyone.

1. Increase East Asian font support

Open and edit the contents of the path/Writer/Word2007/Base. php file, presumably in line 349 (the number of lines may vary with version) and presumably in function _ writeTextStyle:

$objWriter- > writeAttribute('w:eastAsia', $font)
For example, my modified fragments are basically as follows:


// Font
if($font != 'Arial') {
  $objWriter->startElement('w:rFonts');
    $objWriter->writeAttribute('w:eastAsia', $font); //  Add this line 
    $objWriter->writeAttribute('w:ascii', $font);
    $objWriter->writeAttribute('w:hAnsi', $font);
    $objWriter->writeAttribute('w:cs', $font);
  $objWriter->endElement();
}

2. Solve the problem of Chinese garbled codes

Edit PHPWord/Template. php to find the code $replace = utf8_encode ($replace); Delete or comment out this line of code and add $replace = iconv ('gbk', 'utf-8', $replace); For example, the code should read as follows:


 /**
 * Set a Template value
 * 
 * @param mixed $search
 * @param mixed $replace
 */
public function setValue($search, $replace) {
  if(substr($search, 0, 2) !== '${' && substr($search, -1) !== '}') {
    $search = '${'.$search.'}';
  }
 
  if(!is_array($replace)) {
    //$replace = utf8_encode($replace);
    $replace =iconv('gbk', 'utf-8', $replace); //  Add this line after commenting out the above line 
  }
 
  $this->_documentXML = str_replace($search, $replace, $this->_documentXML);
}


The invocation method is as follows:


$document->setValue('Template', iconv('utf-8', 'GB2312//IGNORE', ' Chinese '));

The above code mainly solves the problem of template. In the same way, solve the problem of adding text to Section and find the code $givenText = utf8_encode ($text); Delete or comment out this line of code and add $givenText = iconv ('gbk', 'utf-8', $text); For example, the code is as follows:


/**
 * Add a Text Element
 * 
 * @param string $text
 * @param mixed $styleFont
 * @param mixed $styleParagraph
 * @return PHPWord_Section_Text
 */
public function addText($text, $styleFont = null, $styleParagraph = null) {
  //$givenText = utf8_encode($text);
  $givenText = iconv('gbk', 'utf-8', $text); //  Add this line after commenting out the above line 
  $text = new PHPWord_Section_Text($givenText, $styleFont, $styleParagraph);
  $this->_elementCollection[] = $text;
  return $text;
}

The invocation method is similar to the template invocation above, so I won't list it here.

After tossing so much, I suddenly found that there is another version of PhpWord on the Internet, and the project class name is slightly different in case, belonging to PHPOffice/PHPWord and GitHub project addresses (documents). This version of PHPWord is richer in content and supports more functions (including line spacing, indentation and first line indentation, etc.). Finally, I also adopted this version of PHPWord. It is worth noting that these two versions of PHPWord are basically identical on API interface and can be used universally. However, some API are not recommended in PHPOffice/PHPWord. For example, createSection needs to be changed to addSection. In addition, applying this version of PHPWord does not need to make any modifications supported by Chinese like above, which is more convenient.

The official of these two PHPWord projects have provided more detailed use examples and documents, which will not be introduced here. Finally, it is suggested that loadTemplate can only use template operation methods such as setValue in template mode, and paragraphs can no longer be added or modified. This one is slightly inconvenient.

For PHPOffice/PHPWord, I provide a simple example for reference (of course, there are more official examples):


require_once 'PhpOffice/PhpWord/PhpWord.php'; //  Include header file 
use PhpOffice \ PhpWord \ Autoloader;
use PhpOffice \ PhpWord \ Settings;
use PhpOffice \ PhpWord \ IOFactory;
 
require_once __DIR__ . '/PhpOffice/PhpWord/Autoloader.php';
Autoloader::register();
Settings::loadConfig();
 
// Create a new PHPWord Object
$PHPWord = new  \ PhpOffice \ PhpWord \ PhpWord();
$PHPWordHelper= new  \ PhpOffice \ PhpWord \ Shared \ Font();
 
$PHPWord->setDefaultFontName(' Imitation of Song Dynasty '); //  Global font 
$PHPWord->setDefaultFontSize(16);   //  The global font size is 3 No. 
 
//  Set the properties of the document, which can be seen by right-clicking the properties on the document, or you can omit these steps 
$properties = $PHPWord->getDocumentProperties();
$properties->setCreator(' Zhang 3');  //  Founder 
$properties->setCompany(' A company '); //  Company 
$properties->setTitle(' So-and-so document '); //  Title 
$properties->setDescription('http://wangye.org'); //  Describe 
$properties->setLastModifiedBy(' Li 4'); //  Final modification 
$properties->setCreated( time() );   //  Creation time 
$properties->setModified( time() );   //  Modification time 
 
//  Add 3 Number imitation song font to 'FangSong16pt' Save it for use below 
$PHPWord->addFontStyle('FangSong16pt', array('name'=>' Imitation of Song Dynasty ', 'size'=>16));
 
//  Add paragraph styles to the 'Normal' For the following use 
$PHPWord->addParagraphStyle(
 'Normal',array(
  'align'=>'both',
  'spaceBefore' => 0,
  'spaceAfter' => 0,
  'spacing'=>$PHPWordHelper->pointSizeToTwips(2.8),
  'lineHeight' => 1.19, //  Line spacing 
  'indentation' => array( //  Indent the first line 
   'firstLine' => $PHPWordHelper->pointSizeToTwips(32)
  )
 )
);
 
// Section Style: Above 3.5 Centimeters, below 3.8 Centimeters, left 3 Centimeters, right 3 Centimeters, footer 3 Centimeters 
//  Pay attention to this centimeter (centimeter) To convert to twips Unit 
$sectionStyle = array(
  'orientation' => null,
  'marginLeft' => $PHPWordHelper->centimeterSizeToTwips(3),
  'marginRight' => $PHPWordHelper->centimeterSizeToTwips(3),
  'marginTop' => $PHPWordHelper->centimeterSizeToTwips(3.5),
  'marginBottom' => $PHPWordHelper->centimeterSizeToTwips(3.8),
  'pageNumberingStart' => 1, //  Page numbers from 1 Begin 
  'footerHeight' => $PHPWordHelper->centimeterSizeToTwips(3),
);
 
$section = $PHPWord->addSection($sectionStyle); //  Add 1 Section 
 
//  The following sentence is the content of the input document. Note that the one we added just now is used here 
//  Font style FangSong16pt And paragraph styles Normal
$section->addText(' Document content ', 'FangSong16pt', 'Normal');
$section->addTextBreak(1); //  New rise 1 Blank paragraphs 
 
$objWriter = IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('/path/to/file'); //  Save to /path/to/file Under the path 

Summarize

1. Using template word to generate word Chinese garbled code solution: open phpword/Template. php file and find $replace = utf8_encode ($replace); Replace it with $replace = iconv ('gbk', 'utf-8', $replace); That's enough.

2. Generate word document directly, and solve the Chinese garbled code when calling addText object: open phpword/Section. php file and find $givenText = utf8_encode ($text); Replace it with $givenText = iconv ('gbk', 'utf-8', $text); That's enough.

3. It seems that other methods are similar to the first solution.

4. Note that php file adopts gbk. Anyway, my display is in Chinese. I searched on the Internet for a long time and studied it for a long time before I got it done.


Related articles: