Open fabiengermier opened 7 years ago
Could you explain better what you want to accomplish? Twig as a format is not supported by PHPWord. But you can render a twig to HTML, and then read it into PHPWord.
$phpWord = \PhpOffice\PhpWord\IOFactory::load($wellformed_html_filename, 'HTML');
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save();
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor($new_docx_filename);
$templateProcessor->*();
You need to twig->html->docx->templateprocessor->docx->rtf/html/docx
So produce an html out of your twig ($twig->render()), read it in with the IOFactory, and create a writer, then write it to docx, then use the TemplateProcessor to read in the docx as a template, do some operations, write it back to docx and reopen it with the IOFactory to convert it back to an other format.
You can use the mustache variables in the TemplateProcessor if you want (but you need to disable the automatic addition of ${}
) however, a twig for-loop and other constructs are complex and need to be coded inside PHP (so you need to transform all the twig operations those manually to the available TemplateProcessor commands).
See: https://github.com/PHPOffice/PHPWord/blob/develop/samples/Sample_26_Html.php
Hey @FBnil ,
Thanks for your quick answer. This is my code actually :
`public function convertToWordAction(Request $request) {
$CPRequests = $this->getDoctrine()
->getRepository(CPRequest::class)
->findAll();
$myHtml = $this->render('EupBundle::CPRequest/liste.html.twig', array(
'CPRequests' => $CPRequests,
));
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
\PhpOffice\PhpWord\Shared\Html::addHtml($section, $myHtml, true);
// Saving the document as OOXML file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
header("Content-Disposition: attachment; filename='File.doc'");
$objWriter->save("php://output");
}`
Actually, i got a problem on the addHtml, who return this error : Warning: DOMDocument::loadXML(): Start tag expected, '<' not found in Entity, line: 1
What did i missed ?
Thanks for your help !
Use renderView instead of render function, because render also returns response headers.
Hi, when be convert html to docx dont does a page break when the html content is long. ¿There are a solution for this?
Hello, It works for me with renderView but don't consider the CSS. How can I do to put CSS ?
@AdonisGC I use renderView,display “DOMDocument::loadXML(): Specification mandates value for attribute hidden in Entity, line: 1”。help me。
Hi everyone,
I'm using PHPWord in my SF2.8 application, and i would like to use a twig as template for my docx. Does anybody did that ?
Thanks a lot !