PHPWORD 生成“镜像”表而不是普通表
我使用phpword库来生成带有简单表的Docx文档。
这是代码:
require "vendor/autoload.php";
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$phpWord->setDefaultFontName('Arial');
$phpWord->setDefaultFontSize(12);
$properties = $phpWord->getDocInfo();
$properties->setCreator('My doc');
$properties->setCompany('My doc');
$properties->setTitle('My doc');
$properties->setDescription('My doc');
$properties->setCategory('My doc');
$properties->setLastModifiedBy('My doc');
$properties->setCreated(time());
$properties->setModified(time());
$properties->setSubject('My doc');
$properties->setKeywords('My, Doc, Yeah');
$sectionStyle = array(
'orientation' => 'portrait',
'marginTop' => \PhpOffice\PhpWord\Shared\Converter::pixelToTwip(40),
'marginLeft' => \PhpOffice\PhpWord\Shared\Converter::pixelToTwip(40),
'marginRight' => \PhpOffice\PhpWord\Shared\Converter::pixelToTwip(40),
'colsNum' => 1,
'pageNumberingStart' => 1,
'borderBottomSize'=>100,
'borderBottomColor'=>'ffffff'
);
$section = $phpWord->addSection($sectionStyle);
$html2 = '<table>';
$html2 .= '<tr>';
$html2 .= '<td>Something 1</td>';
$html2 .= '<td>Something 2</td>';
$html2 .= '</tr>';
$html2 .= '<tr>';
$html2 .= '<td>Another 1</td>';
$html2 .= '<td>Another 2</td>';
$html2 .= '</tr>';
$html2 .= '</table>';
\PhpOffice\PhpWord\Shared\Html::addHtml($section, $html2);
header("Content-Description: File Transfer");
header('Content-Disposition: attachment; filename="first.docx"');
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Expires: 0');
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$xmlWriter->save("php://output");
因此生成了文档,但是其中显示的表格并不像这样:
某物1 |其他2
另一个1 |另外2,
但这样:
2 |其他1
另一个2 |另一个1
因此,由于某种原因,它在生成的.docx中被“镜像”。这是成像.docx- https://ibb.co/1mcsss0y 的附件。什么原因?
I use PHPWORD library to generate .docx document with a simple table.
This is the code:
require "vendor/autoload.php";
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$phpWord->setDefaultFontName('Arial');
$phpWord->setDefaultFontSize(12);
$properties = $phpWord->getDocInfo();
$properties->setCreator('My doc');
$properties->setCompany('My doc');
$properties->setTitle('My doc');
$properties->setDescription('My doc');
$properties->setCategory('My doc');
$properties->setLastModifiedBy('My doc');
$properties->setCreated(time());
$properties->setModified(time());
$properties->setSubject('My doc');
$properties->setKeywords('My, Doc, Yeah');
$sectionStyle = array(
'orientation' => 'portrait',
'marginTop' => \PhpOffice\PhpWord\Shared\Converter::pixelToTwip(40),
'marginLeft' => \PhpOffice\PhpWord\Shared\Converter::pixelToTwip(40),
'marginRight' => \PhpOffice\PhpWord\Shared\Converter::pixelToTwip(40),
'colsNum' => 1,
'pageNumberingStart' => 1,
'borderBottomSize'=>100,
'borderBottomColor'=>'ffffff'
);
$section = $phpWord->addSection($sectionStyle);
$html2 = '<table>';
$html2 .= '<tr>';
$html2 .= '<td>Something 1</td>';
$html2 .= '<td>Something 2</td>';
$html2 .= '</tr>';
$html2 .= '<tr>';
$html2 .= '<td>Another 1</td>';
$html2 .= '<td>Another 2</td>';
$html2 .= '</tr>';
$html2 .= '</table>';
\PhpOffice\PhpWord\Shared\Html::addHtml($section, $html2);
header("Content-Description: File Transfer");
header('Content-Disposition: attachment; filename="first.docx"');
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Expires: 0');
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$xmlWriter->save("php://output");
So the document is generated, but the table in it is shown not like this:
Something 1 | Something 2
Another 1 | Another 2
But like this:
Something 2 | Something 1
Another 2 | Another 1
So for some reason it is "mirrored" in generated .docx. Here is an attached imaged of the .docx - https://ibb.co/1McSS0y. What is the reason?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论