PHPWord

phpword

使用了以上这个库将docx转成html,发现部分样式丢失,并有有单词莫名换行。再将htmldocx就变不回原来那样了,有人遇过这种情况么。。

代码如下

$objReader = \PhpOffice\PhpWord\IOFactory::createReader('Word2007');
$objPHPFile = $objReader->load('123.docx');
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($objPHPFile, 'HTML');
echo $objWriter->save('test.html');

$objReader = \PhpOffice\PhpWord\IOFactory::createReader('HTML');
//var_dump($objReader);exit();
$objPHPFile = $objReader->load('test.html');
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($objPHPFile, 'Word2007');
echo $objWriter->save('test2.docx');
阅读 2.9k
1 个回答

用libreoffice,把Word文档转成HTML,可以保持大部分样式.

在Ubuntu服务器上安装libreoffice:
apt install libreoffice-writer

<?php
$cmd = "libreoffice --headless --convert-to 'html:XHTML Writer File:UTF8' --outdir './' './word.docx'";
shell_exec($cmd);
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏