Replies: 2   Views: 108
Crash when using a custom docx file as base template

Posted by vpratfr  · 06-07-2020 - 10:46


(I have an advanced licence)

I have issues using our own file as base for the generated Word files (we do not need to replace anything withing the Word file itself, we just convert our HTML to a Word document, hence we just want to use the styling from that template file).

Here is the code used to create the file

$wordTemplate = 'docx';
$customTemplatePath = storage_path("app/system/templates/word-templates/{$context->getStylesheetName()}.docx");
if (file_exists($customTemplatePath))
    $wordTemplate = $customTemplatePath;

$docx = new CreateDocx($wordTemplate);
$docx->embedHTML($htmlContent, [
    'useHTMLExtended'  => true,
    'isFile'           => false,
    'strictWordStyles' => false,
    'wordStyles'       => [
        'h1' => 'Titre 1',
        'h2' => 'Titre 2',
        'h3' => 'Titre 3',


When not using a custom template, no problem, the file is generated.

When using a custom template (empty basic Word file created with Office 2019), we get a warning:

ErrorException {#7987
  #message: "dom_import_simplexml() expects parameter 1 to be object, bool given"
  #code: 0
  #file: "./packages/phpdocx/Classes/Phpdocx/Utilities/DOCXStructure.php"
  #line: 166
  #severity: E_WARNING
  trace: {
    ./packages/phpdocx/Classes/Phpdocx/Utilities/DOCXStructure.php:166 {
      Illuminate\Foundation\Bootstrap\HandleExceptions->handleError($level, $message, $file = '', $line = 0, $context = [])
      › // get the main document XML and rename it to word/document.xml
      › $contentTypesDom = dom_import_simplexml($contentTypesXml);
      › $contentTypesXpath = new \DOMXPath($contentTypesDom->ownerDocument);
    Illuminate\Foundation\Bootstrap\HandleExceptions->handleError() {}
    ./packages/phpdocx/Classes/Phpdocx/Utilities/DOCXStructure.php:166 { …}
    ./packages/phpdocx/Classes/Phpdocx/Create/CreateDocx.php:790 { …}

I have tried to mute that warning (@new), but that did not help.

I tried using CreateDocxFromTemplate but that did not help either.


Posted by admin  · 06-07-2020 - 10:54

Edited by admin · 06-07-2020 - 11:15


To use a DOCX as template, you need to use CreateDocxFromTemplate, not CreateDocx ( Please send the DOCX you are using as template to contact[at]phpdocx so we can test it, phpdocx is tested from MS 2003 to MS Word 2019 (including MS Word 365 and online versions), LibreOffice and other DOCX editors. The line that returns the error you have sent is trying to get the main file of a DOCX ([Content_Types].xml) that should always exist.

Adding @ may be helpful only to hide some unwanted PHP notice, not warnings or errors.

Also note that using an Advanced you can't use HTML Extended. This feature is only available in Premium licenses, so you can't use the following option:

'useHTMLExtended'  => true,


Posted by vpratfr  · 06-07-2020 - 15:03


I was about to send the template and then Gmail just pointed me at the fix:

When creating an empty file and just saving without touching anything else, Word does not save anything into the file (it is a 0 byte file).

I simply opened it, and just typed and erased a character, saved it, and BAM - the file was 13 kb in size. My code is now working nicely.

Thanks, sorry for the confusion, blame Microsoft for a fake Word file :)