Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Html(twig) to docx #1181

Open
fabiengermier opened this issue Nov 3, 2017 · 6 comments
Open

Html(twig) to docx #1181

fabiengermier opened this issue Nov 3, 2017 · 6 comments

Comments

@fabiengermier
Copy link

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
Copy link

FBnil commented Nov 3, 2017

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
Copy link
Author

fabiengermier commented Nov 6, 2017

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:https://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
Copy link

AdonisGC commented Nov 10, 2017

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

@rich-97
Copy link

rich-97 commented Mar 2, 2018

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
Copy link

kent069 commented May 2, 2018

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

@DoYouZz
Copy link

DoYouZz commented Sep 27, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

6 participants