我正在尝试使用“ PHPWord”下载docx文件。
如果我尝试将文件保存到服务器上,则可以正常工作。但是,如果添加标题以下载文件,则文件将以损坏的形式显示。
注意:我正在使用openOffice打开它。
这是我的代码:
$document->save($doc);
header('Content-Description: File Transfer');
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header("Content-Disposition: attachment; filename=CV.docx");
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($doc));
readfile($doc);
Run Code Online (Sandbox Code Playgroud)
谁能告诉我这个问题可能是什么?
我正在使用 PHPOffice/PHPWord,并且我有一个 MySQL 数据库,其中包含一个名为trials. 它包含 2 列id和name. 两列都是VARCHAR(255). 有几行,如下所示:
id | name
---+-----------
A1 | Avatar
B1 | Browser
C1 | Calculator
A2 | Align
Run Code Online (Sandbox Code Playgroud)
我使用的查询是 SELECT * FROM trials ORDER BY id ASC
我想要做的是制作一个 DOC 文件并为每一行添加一个新页面(1 页为 1,1 页id为name)。
如何在行之间插入分页符?
另外,如何设置边距和页面方向?
作为一个例子,我想要的内容mytrial.doc如下:
Page 01
A1 - Avatar
Page 02
A2 - Align
Page 03
B1 - Browser
Page 04
C1 - Calculator
Run Code Online (Sandbox Code Playgroud)
我使用 Laravel 4 …
我正在构建一个必须填写 docx 文档的 Web 应用程序。我正在使用 PHPWord 提供的 TemplateProcessor,它采用文档上显示的特定钩子并将其替换为给定的字符串。我遇到的问题是,当字符串中出现“&”时,文档会因缺少“;”而损坏。
我用来清除字符串的函数是
$string = trim(html_entity_decode($string));
$string = strip_tags($string);
$string = trim(preg_replace('/\s+/', ' ', $string));
Run Code Online (Sandbox Code Playgroud)
有谁知道如何转义 ampersant 符号,这样文件就不会被损坏?
我有一些要加粗的文字,与前几段和后几段分开并缩进。我不能让所有三个属性一起工作。
这适用于粗体和空格:
$section = $phpWord->addSection();
$section->addText(
'Re: Your Application for Post of Security Guard',
array('bold' => true),
array('space' => array('before' => 360, 'after' => 280))
);
Run Code Online (Sandbox Code Playgroud)
这适用于缩进:
$section = $phpWord->addSection();
$section->addText(
'Re: Your Application for Post of Security Guard',
array('indentation' => array('left' => 540, 'right' => 120))
);
Run Code Online (Sandbox Code Playgroud)
但这不起作用:
$section = $phpWord->addSection();
$section->addText(
'Re: Your Application for Post of Security Guard',
array('bold' => true),
array('space' => array('before' => 360, 'after' => 280)),
array('indentation' => array('left' => 540, 'right' => 120)) …Run Code Online (Sandbox Code Playgroud) 我正在开发一些带有文件转换的项目docx to pdf,并且我使用没有连接的共享主机服务器ssh,唯一可以使用的选项是Ftp。
那么,是否有任何选项可以在不使用作曲家的情况下将包提取为库,例如phpword,它是可移植的,并且基本上可以在服务器文件夹中复制,但转换器包仅适用于composer. 我尝试从包中提取所需的文件,但似乎它们核心需要 autoloader.php,我看到的唯一选项就是修改源文件以供便携式使用。
如果可能的话,有没有更简单的选择?
我是新手composer,这让我很难理解便携式使用的提取包的可能性。
有什么建议么?谢谢!
我对 PHPWord 有一个问题,我相信版本是 0.11.0,我使用 PHPWord for CI。
问题是我找不到合适的 php 脚本来制作下划线,这是我得到的最新脚本,
$section->addText(
$text,
[ 'underline' => \PhpOffice\PhpWord\Style\AbstractStyle::UNDERLINE_SINGLE ], // Problem with this line
[ 'align' => \PhpOffice\PhpWord\Style\Alignment::ALIGN_CENTER ] // This one fine
);
Run Code Online (Sandbox Code Playgroud)
但是每当我得到脚本时,它总是显示错误或没有给我想要的东西,(带下划线的文本)。
这是我得到的错误
遇到未捕获的异常
类型:错误
消息:未定义的类常量“UNDERLINE_SINGLE”
文件名:C:\xampp\htdocs\test\application\controllers\Print.php
行号:47
回溯:
文件:C:\xampp\htdocs\test\index.php 行:315 功能:require_once
我感谢任何帮助,提前致谢。
我正在使用 PHPWord 库将 HTML 模板转换为 Word。
我将 HTML 转换为 Word 文件的代码如下:
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
\PhpOffice\PhpWord\Shared\Html::addHtml($section, $htmlTemplate);
$targetFile = __DIR__ . "/1.docx";
$phpWord->save($targetFile, 'HTML');
Run Code Online (Sandbox Code Playgroud)
我面临以下问题:
<p style="background-color:#FFFF00;color:#FF0000;">Some text</p>
Run Code Online (Sandbox Code Playgroud)
<div style="page-break-after:always"><span style="display:none"> </span></div>
Run Code Online (Sandbox Code Playgroud)
<p>some text before span tag <span style="color:#FF0000;display:inline">XXXXX</span> Text after span tag.</p>
Run Code Online (Sandbox Code Playgroud)
编辑:
如果上述问题无法用 PHPWord 解决,请推荐任何其他库,我可以使用它来将带有内联 css 的 HTML 转换为 Word!
这与 GitHub 页面 [链接]上列出的一个半已知问题有关,其中 PhpWord 生成表格的方式导致它将单元格宽度设置为“”,这在 LibreOffice 中无效,但在 Word 中很好。
GitHub 上的问题专门列出了 html-to-word 转换,但我在使用常规的面向对象界面时也遇到了这个问题。
我试过手动设置单元格宽度,但它似乎没有做任何事情。
我在这里询问是否有人对此问题有任何解决方法,因为它几乎不可能在不访问 MS Word 的情况下在 Linux 机器上调试我的输出。我需要为客户导出 .docx。
为什么不直接导出到 .odt 来测试它呢?如果我这样做,它也不会呈现我在页面上的列表......
这是我正在处理的文档的粗略概述(由于保密协议,我不能随意分享确切的代码):
$document = new PhpWord\PhpWord();
$section = $document->addSection();
// This list doesn't get rendered in the ODT file
$section->addListItem('Employee ID: 98765');
$section->addListItem('First Name: John');
$section->addListItem('Last Name: Doe');
$section->addListItem('SSN: 12345');
// This table has errors in LibreOffice when exported to .docx
if ($show_adv_details) {
$table = $section->addTable();
$table->addRow();
$table->addCell()->addText('SSN');
$table->addCell()->addText('Email');
$table->addCell()->addText('Phone No');
$table->addRow();
$table->addCell()->addText('12345'); …Run Code Online (Sandbox Code Playgroud) 我已经phpword在laravel项目中安装了软件包。现在,当我尝试docx使用此代码读取文件时
public function upload(Request $request){
$file = $request->file('file');
$phpWord = \PhpOffice\PhpWord\IOFactory::load($file);
foreach($phpWord->getSections() as $section) {
foreach($section->getElements() as $element) {
if(method_exists($element,'getText')) {
echo $element->getText() . "<br>";
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我收到一个错误
找不到类“ ZipArchive”
/vendor/phpoffice/common/src/Common/XMLReader.php第54行
在 phpword 中,我发现您曾经能够将样式数组传递给 addHTML 函数,但现在不能了。
如果我的 HTML 没有设置任何内联样式,是否有办法指定 fontStyle 和 paragraphStyle,这样它就不会在 Word 文件中以普通的“Arial 10pt”形式结束?
我通过在调用 addHTML 之前设置默认样式取得了一些成功:
$phpWord->setDefaultFontName('...');
$phpWord->setDefaultFontSize('...');
$phpWord->setDefaultParagraphStyle('...');
Run Code Online (Sandbox Code Playgroud)
但是,这不是完整的字体样式。例如,您将如何更改文本的颜色?
phpword ×10
php ×7
phpoffice ×4
codeigniter ×1
composer-php ×1
escaping ×1
laravel ×1
libreoffice ×1
ms-word ×1
pdf ×1
regex ×1
string ×1