Forum


Replies: 2   Views: 1742
Crash when using a custom docx file as base template
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

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

Hi,

(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',
    ],
]);

$docx->createDocx($wordFile);

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.