PHPWORD 生成“镜像”表而不是普通表

发布于 2025-01-18 09:35:50 字数 2453 浏览 4 评论 0原文

我使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文