标签: phpword

使用PHPWord下载DOCX文件时获取损坏的文件

我正在尝试使用“ 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)

谁能告诉我这个问题可能是什么?

php phpword

2
推荐指数
1
解决办法
2799
查看次数

PHPWord:使用 Laravel 4 插入分页符并设置边距和页面方向

我正在使用 PHPOffice/PHPWord,并且我有一个 MySQL 数据库,其中包含一个名为trials. 它包含 2 列idname. 两列都是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 页idname)。

如何在行之间插入分页符?

另外,如何设置边距和页面方向?

作为一个例子,我想要的内容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 …

php phpword

1
推荐指数
1
解决办法
1万
查看次数

使用 PHPWord 填写 docx 文档时转义“&”

我正在构建一个必须填写 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 符号,这样文件就不会被损坏?

regex string escaping phpword phpoffice

1
推荐指数
1
解决办法
3394
查看次数

phpWord中的粗体,空格和缩进文本

我有一些要加粗的文字,与前几段和后几段分开并缩进。我不能让所有三个属性一起工作。

这适用于粗体和空格:

$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)

php phpword

1
推荐指数
1
解决办法
3350
查看次数

如何避免 php 包的作曲家?

我正在开发一些带有文件转换的项目docx to pdf,并且我使用没有连接的共享主机服务器ssh,唯一可以使用的选项是Ftp

那么,是否有任何选项可以在不使用作曲家的情况下将包提取为库,例如phpword,它是可移植的,并且基本上可以在服务器文件夹中复制,但转换器包仅适用于composer. 我尝试从包中提取所需的文件,但似乎它们核心需要 autoloader.php,我看到的唯一选项就是修改源文件以供便携式使用。

如果可能的话,有没有更简单的选择?

我是新手composer,这让我很难理解便携式使用的提取包的可能性。

有什么建议么?谢谢!

pdf file-conversion composer-php phpword

1
推荐指数
1
解决办法
2348
查看次数

PHPWord 0.11.0 字体样式下划线

我对 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

我感谢任何帮助,提前致谢。

php codeigniter phpword codeigniter-3 phpoffice

1
推荐指数
1
解决办法
2763
查看次数

PHPWord HTML 到 Word 设计问题

我正在使用 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)

我面临以下问题:

  1. 我正在使用内联样式应用背景颜色和字体颜色,但字体颜色仅起作用,背景颜色不起作用。
 <p style="background-color:#FFFF00;color:#FF0000;">Some text</p>
Run Code Online (Sandbox Code Playgroud)
  1. 我使用下面的 HTML 代码来允许在 HTML 模板的各个部分中分页。但它不适用于导出的 Word 文件。
<div style="page-break-after:always"><span style="display:none">&nbsp;</span></div>
Run Code Online (Sandbox Code Playgroud)
  1. 我正在使用 span 标签以粗体标记某些单词或更改段落中单词的颜色。但是当我导出到Word文件时,span标签后的内容会移动到单词下方的新行。我尝试在span标签中使用display:inline css属性。但它不起作用。
<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!

php phpword

1
推荐指数
1
解决办法
7446
查看次数

PhpWord 输出在 LibreOffice 中不起作用,但在 MS 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)

php ms-word libreoffice phpword

1
推荐指数
1
解决办法
367
查看次数

在Laravel phpword中找不到类“ ZipArchive”

我已经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行

zip 已经安装在我的PHP中。 在此处输入图片说明

php laravel phpword phpoffice

0
推荐指数
1
解决办法
990
查看次数

如何在 PHPWord 中向传入的 HTML 添加样式

在 phpword 中,我发现您曾经能够将样式数组传递给 addHTML 函数,但现在不能了。

如果我的 HTML 没有设置任何内联样式,是否有办法指定 fontStyle 和 paragraphStyle,这样它就不会在 Word 文件中以普通的“Arial 10pt”形式结束?

我通过在调用 addHTML 之前设置默认样式取得了一些成功:

$phpWord->setDefaultFontName('...');
$phpWord->setDefaultFontSize('...');
$phpWord->setDefaultParagraphStyle('...');
Run Code Online (Sandbox Code Playgroud)

但是,这不是完整的字体样式。例如,您将如何更改文本的颜色?

phpword phpoffice

0
推荐指数
1
解决办法
6588
查看次数