3

I am having issue with phpoffice latest version not keeping line breaks I added to textrun when the generated template doc is saved as pdf. Below is the code that I using.

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"); 

This is how the output shows in docx https://share.getcloudapp.com/p9urDdmB and this is how it shows incorrectly in pdf https://share.getcloudapp.com/mXu5LmJ1 . All line breaks are getting removed in pdf but are working fine in word doc. I cannot install any software on server but can use any library like phpoffice if that is needed to fix this issue.

Thanks

deepsingh
  • 149
  • 10

1 Answers1

0

I have similar issue too. I have checked phpoffice source (0.18.1) and looks like it just skip TextBreak element.

For myself I have created solution with small phpoffice source modification.

Step 1: add text-break as font family name where it needs

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

Step 2: open file PHPWord\vendor\phpoffice\phpword\src\PhpWord\Writer\HTML\Element\Text.php and find next line $this->closingTags = '</span>'; under this line add next code:

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

So now docx, pdf and html versions looks correctly for me.