Answer the question
In order to leave comments, you need to log in
How to convert HTML with images to DOCX in PHPWord?
Good afternoon!
You need to generate a single Word DOCX document. The library used is PHPOffice/PHPWord . Version 0.18
There is a block in the initial template:
${additional_block}
${block}
${/additional_block}
/* additional_block */
$m = new \Mustache_Engine(array('entity_flags' => ENT_QUOTES));
$templateProcessor->cloneBlock('additional_block', count($db_contract_docs), true, true);
$i = 1;
foreach ($db_contract_docs as $docs){
/* Подставим значения через шаблонизатор */
$template = $m->render($docs->template, $params);
$phpWord = new PhpWord('Word2007');
/* Создадим новую страницу */
$section = $phpWord->addSection();
/* Конвертируем HTML в Word */
\PhpOffice\PhpWord\Shared\Html::addHtml($section, $template, false);
$xmlWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY, './', WordSettings::hasCompatibility());
$containerWriter = new Container($xmlWriter, $section);
$containerWriter->write();
$htmlAsXml = $xmlWriter->getData();
WordSettings::setOutputEscapingEnabled(false);
$templateProcessor->setValue('block#' . ($i), $htmlAsXml);
$templateProcessor->addImageToRelations();
WordSettings::setOutputEscapingEnabled(true);
$i++;
}
header("Content-Disposition: attachment; filename=contract_$contract.docx");
$templateProcessor->saveAs('php://output');
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question