PHPOffice / PHPWord

A pure PHP library for reading and writing word processing documents
https://phpoffice.github.io/PHPWord/
Other
7.27k stars 2.7k forks source link

Html(twig) to docx #1181

Open fabiengermier opened 7 years ago

fabiengermier commented 7 years ago

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 !

FBnil commented 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

fabiengermier commented 7 years ago

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 !

AdonisGC commented 7 years ago

Use renderView instead of render function, because render also returns response headers.

rich-97 commented 6 years ago

Hi, when be convert html to docx dont does a page break when the html content is long. ¿There are a solution for this?

kent069 commented 6 years ago

Hello, It works for me with renderView but don't consider the CSS. How can I do to put CSS ?

DoYouZz commented 6 years ago

@AdonisGC I use renderView,display “DOMDocument::loadXML(): Specification mandates value for attribute hidden in Entity, line: 1”。help me。