将 doc 保存为 pdf 时删除了 phpoffice 换行符

标签 php dompdf phpword phpoffice

当生成的模板文档另存为 pdf 时,我遇到了 phpoffice 最新版本不保留我添加到 textrun 的换行符的问题。下面是我使用的代码。

require_once 'bootstrap.php';
use PhpOffice\PhpWord\Settings;
use PhpOffice\PhpWord\Element\TextRun;

use PhpOffice\PhpWord\TemplateProcessor;
use PhpOffice\PhpWord\Element\Table;
use PhpOffice\PhpWord\SimpleType\TblWidth;
use PhpOffice\PhpWord\IOFactory;

date_default_timezone_set('UTC');
error_reporting(E_ALL);
define('CLI', (PHP_SAPI == 'cli') ? true : false);
define('EOL', CLI ? PHP_EOL : '<br />');
define('SCRIPT_FILENAME', basename($_SERVER['SCRIPT_FILENAME'], '.php'));
define('IS_INDEX', SCRIPT_FILENAME == 'index');

Settings::loadConfig();

$dompdfPath = $vendorDirPath . '/dompdf/dompdf';
if (file_exists($dompdfPath)) {
    define('DOMPDF_ENABLE_AUTOLOAD', true);
    Settings::setPdfRendererName(Settings::PDF_RENDERER_DOMPDF);
}

// Set writers
$writers = array('Word2007' => 'docx', 'ODText' => 'odt', 'RTF' => 'rtf', 'HTML' => 'html', 'PDF' => 'pdf');
Settings::setPdfRendererPath('.');


// Turn output escaping on
Settings::setOutputEscapingEnabled(true);

// Return to the caller script when runs by CLI
if (CLI) {
    return;
}
// Creating the new document...
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$phpWord->getCompatibility()->setOoxmlVersion(15);


$templateProcessor = new TemplateProcessor('Terms-Of-Service.docx');
$explodedPlanDescription = explode("\n",$decodedJson->data->subscription->plan->description);
if(is_array($explodedPlanDescription)){


    $inline = new TextRun();
    $inline->addText("Plan Details",array('size' => 13,'bold' => true));
            $inline->addTextBreak(1);

    $inline->addText("Plan Details",array('size' => 13,'bold' => true));
    $inline->addTextBreak(1);
    foreach($explodedPlanDescription as $exPlanDesc){
        $inline->addText($exPlanDesc);
        $inline->addTextBreak(1);
    }
     
}

$templateProcessor->setComplexValue('firstname', $inline);
//$templateProcessor->setValue('firstname', $decodedJson->data->subscription->plan->description);

$templateProcessor->saveAs('Sample_07_TemplateCloneRow1.docx','Word2007');
$wordPdf = IOFactory::load('Sample_07_TemplateCloneRow1.docx','Word2007');
$wordPdf->save("test.pdf","PDF"); 

这是输出在 docx 中的显示方式 https://share.getcloudapp.com/p9urDdmB这就是它在 pdf 中错误显示的方式 https://share.getcloudapp.com/mXu5LmJ1 .所有换行符都在 pdf 中被删除,但在 word 文档中工作正常。 我无法在服务器上安装任何软件,但如果需要解决此问题,可以使用任何库(如 phpoffice)。

谢谢

最佳答案

我也有类似的问题。我检查了 phpoffice 源代码 (0.18.1),看起来它只是跳过了 TextBreak 元素。

对于我自己,我已经创建了对 phpoffice 源代码进行少量修改的解决方案。

第 1 步:在需要的地方添加 text-break 作为字体系列名称

$inline = new TextRun();
$inline->addText("Plan Details",array('size' => 13,'bold' => true,'name'=>'Times New Roman text-break'));
$inline->addTextBreak(1);

第 2 步: 打开文件 PHPWord\vendor\phpoffice\phpword\src\PhpWord\Writer\HTML\Element\Text.php 并找到下一行 $this->closingTags = '</span>'; 在此行下添加下一个代码:

if(strstr($style,'text-break')) $this->closingTags .='<br>';

所以现在 docx、pdf 和 html 版本对我来说是正确的。

关于将 doc 保存为 pdf 时删除了 phpoffice 换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65724805/

相关文章:

php - 我应该将 PHPDoc 中的 @throws 添加到使用抛出异常的函数的函数中吗?

php - Mysql 通过 Group by 进行限制

php - Dompdf 错误 : "The Row #4 could not be found" when converting PHP file to PDF

php - Dompdf 0.8.3 不在 PHP 7.2 和 CodeIgniter 3 中获取远程 CSS

PHPWORD 如何在不中断的情况下使用函数 addText

php - 如何使用 phpWord 和使用 cloneBlock 将变量复制到模板中

php - 如何使用 ./configure 设置变量

php - dompdf 不呈现超过 10 页的 PDF

php - 如何用phpword设置表格宽度?

php - 在php中将单列数据合并为双列数据