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



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

require_once 'bootstrap.php';
use PhpOfficePhpWordSettings;
use PhpOfficePhpWordElementTextRun;
use PhpOfficePhpWordTemplateProcessor;
use PhpOfficePhpWordElementTable;
use PhpOfficePhpWordSimpleTypeTblWidth;
use PhpOfficePhpWordIOFactory;
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 PhpOfficePhpWordPhpWord();
$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:在需要的地方添加文本分隔符作为字体家族名称

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

步骤2:打开文件PHPWordvendorphpofficephpwordsrcPhpWordWriterHTMLElementText.php并找到下一行$this->closingTags = '</span>';在这行下面添加下一个代码:

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

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

最新更新