将文档保存为pdf时删除phpoffice换行符

将文档保存为pdf时删除phpoffice换行符,php,dompdf,phpword,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 Ph

当生成的模板文档保存为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"); 
require_once'bootstrap.php';
使用PhpOffice\PhpWord\Settings;
使用PhpOffice\PhpWord\Element\TextRun;
使用PhpOffice\PhpWord\TemplateProcessor;
使用PhpOffice\PhpWord\Element\Table;
使用PhpOffice\PhpWord\SimpleType\TblWidth;
使用PhpOffice\PhpWord\IOFactory;
日期\默认\时区\设置('UTC');
错误报告(E_全部);
定义('CLI',(PHP_SAPI=='CLI')?true:false);
定义('EOL',CLI?PHP_EOL:'
'); 定义('SCRIPT_FILENAME',basename($_SERVER['SCRIPT_FILENAME'],'.php'); 定义('IS_INDEX',SCRIPT_FILENAME=='INDEX'); 设置::loadConfig(); $dompdfPath=$VENDORDRPATH'/dompdf/dompdf'; 如果(文件_存在($dompdfPath)){ 定义('dompd\u ENABLE\u AUTOLOAD',true); 设置::setPdfRendererName(设置::PDF\u RENDERER\u DOMPDF); } //编剧 $writers=array('Word2007'=>'docx','ODText'=>'odt','RTF'=>'RTF','HTML'=>'HTML','PDF'=>'PDF'); 设置::setPdfRendererPath('.'); //打开输出转义 设置::setOutputEscapingEnabled(真); //通过CLI运行时返回调用方脚本 如果(CLI){ 返回; } //正在创建新文档。。。 $phpWord=new\phpooffice\phpWord\phpWord(); $phpWord->getCompatibility()->setOoxmlVersion(15); $templateProcessor=新的templateProcessor('Terms-Of-Service.docx'); $ExplodeDplandDescription=explode(“\n”,$decodedJson->数据->订阅->计划->说明); if(is_数组($ExplodedPlandDescription)){ $inline=newtextrun(); $inline->addText(“计划详细信息”,数组('size'=>13,'bold'=>true)); $inline->addTextBreak(1); $inline->addText(“计划详细信息”,数组('size'=>13,'bold'=>true)); $inline->addTextBreak(1); foreach($exploddepland描述为$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中的显示方式,也是它在pdf中错误显示的方式。pdf中的所有换行符都已删除,但在word文档中工作正常。 我无法在服务器上安装任何软件,但如果需要修复此问题,可以使用任何库,如phpoffice


谢谢

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

对于我自己,我创建了一个带有小phpoffice源代码修改的解决方案

步骤1:在需要的地方添加文本分隔符作为字体系列名称

$inline = new TextRun();
$inline->addText("Plan Details",array('size' => 13,'bold' => true,'name'=>'Times New Roman text-break'));
$inline->addTextBreak(1);
第2步:打开文件
PHPWord\vendor\phpooffice\PHPWord\src\PHPWord\Writer\HTML\Element\Text.php
并找到下一行
$this->closingTags=''
在此行下添加下一个代码:

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

因此,现在docx、pdf和html版本对我来说是正确的。

这方面成功了吗?