Forum


Replies: 8   Views: 4003
Importstyles issues
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 Alain DIAS  · 18-11-2015 - 16:55

I tried to import my own template style thanks to importStyles with merge parameter and array of my customs styles.

I apply those styles to my document. But when I opened it (the generated document), styles witch I imported are not applied correctly or not applied any more.The last styles called section, subSection are never applied and title, subTitle, subSubTitle are applied but with wrong color.

Please Help me (please see below my code). I'm in v5.0

 

$path = $this->container->getParameter('kernel.root_dir') . '/../web/bundles/rffconcertationsrest/templates/report.docx';

$docx = new \Phpdocx();

$docx->importStyles($path, 'merge', array(
            'title',
            'subTitle',
            'subSubTitle',
            'section',
            'subSection'
        ));

//variables are of course initialized
$docx->addText($title, array('pStyle' => 'title'));
        $docx->addText($subTitle, array('pStyle' => 'subTitle'));
        $docx->addText($subSubTitle, array('pStyle' => 'subSubTitle'));
        $docx->addBreak(array('type' => 'page'));
        $docx->addText($section1, array('pStyle' => 'section'));
        $docx->addText($subSection11, array('pStyle' => 'subSection'));

$path = $this->container->getParameter('kernel.root_dir') . '/../web/uploads/';
        $fullPath = $path . 'test';
        $docx->createDocx($fullPath);

 

Posted by admin  · 18-11-2015 - 18:13

Hello,

The importStyles method works fine. The most common issue when working with this method is to set a wrong style name.
We recommend you to use the parseStyles method:

http://www.phpdocx.com/api-documentation/layout-and-general/parse-available-styles-Word-document-with-PHP

to get all the styles available in a document, and check that the styles are correctly created when you generate the DOCX. Also please run the included example to check if the method is running fine on your OS.

If you still have issues after following these steps, please send to contact[at]phpdocx.com the DOCX you're importing and we'll check it.

Regards.

Posted by Alain DIAS  · 19-11-2015 - 08:04

I've already tried imporStyles method too. But when I try to apply one of my custom style, I felt of that weird error (see below):

ContextErrorException: Warning: DOMDocument::loadXML(): xmlParseEntityRef: no name in Entity, line: 6694 in PHPDOCX\classes\CreateDocx.inc line 983

I've googlised the error, and I found on stackoverflow that explanation : http://stackoverflow.com/questions/7604436/xmlparseentityref-no-name-warnings-while-loading-xml-into-a-php-file . I tried to search a & symbol in my template and in my text that I passed in the addText method . But no & symbol found.

Posted by admin  · 19-11-2015 - 08:13

Hello,

The parseStyles method must run standalone, this is using a different object instance than any other method.

Please use this method to generate a DOCX with the style names. And then set in your script the right style name/id.

Regards.

Posted by Alain DIAS  · 19-11-2015 - 10:18

Ok I finally succeed to generate the template. Now how can I apply my custom styles that are generate in my template (if I understood) ? Have I to load it the template (if so how ? With CreateDocFromTemplate or other?) ?

 

public function generate() {
      $this->generateTemplate();
    // Create a new document
        $docx = new \Phpdocx();
}
private function generateTemplate()
    {
        // Create a new document
        $docx = new \Phpdocx();
        // Imports style from the template
        $path = $this->container->getParameter('kernel.root_dir') . '/../web/bundles/rffconcertationsrest/templates/report.docx';
        $docx->parseStyles($path);
        $uploadPath = $this->container->getParameter('kernel.root_dir') . '/../web/uploads/';
        $docx->createDocx($uploadPath.'templateParseStyles');
    }

 

Posted by admin  · 19-11-2015 - 10:32

Hello,

You need to use the importStyles method to import styles and use them. The method parseStyles is only to generate a DOCX with a preview of the existing styles and know which name and id they have and be able to use them.

Please run and check the included examples in the package to know how to use them, and also read the API methods (http://www.phpdocx.com/documentation/api-documentation) They explain how to use the methods and the available options.

Regards.

Posted by Alain DIAS  · 19-11-2015 - 16:23

 

I've of course already read the API documentation in detail. Import a template seems to be a tricky part.

I tried the code below. None of my custom styles are applied. I tried to open the template file to see what is generated. But Word said to me that problems have been detected (and doesn't want to open). I suppose that is why my custom styles aren't applied (maybe).

private function generateTemplate()
    {
        // Create a new document
        $docx = new \Phpdocx();
        // Imports style from the template
        $path = $this->container->getParameter('kernel.root_dir') . '/../web/bundles/rffconcertationsrest/templates/report.docx';
        $docx->parseStyles($path);
        $uploadPath = $this->container->getParameter('kernel.root_dir') . '/../web/uploads/';
        $this->templatePath  = $uploadPath.'templateParseStyles.docx';
        $docx->createDocx($uploadPath.'templateParseStyles');
    }

public function generate(){
$this->generateTemplate();
// Create a new document
        $docx = new \Phpdocx();
$docx->importStyles($this->templatePath, 'merge', array(
            'title',
            'subTitle',
            'subSubTitle',
            'section',
            'subSection')
        );
$docx->addText($title, array('pStyle' => 'title'));
}

 

Posted by admin  · 19-11-2015 - 16:39

Hello,

You must run the parseStyles method standalone. This method generates a document in which you can read the available style names.

When you run standalone the method parseStyles for your DOCX that has the styles you want to import and you open the DOCX output, do you see the 'title' style name?

And why are you parsing and importing different DOCX?

$path = $this->container->getParameter('kernel.root_dir') . '/../web/bundles/rffconcertationsrest/templates/report.docx';
        $docx->parseStyles($path);

...

$docx->importStyles($this->templatePath,...

The steps you need to do are:

1. Run a standalone script in which you use the parseStyles method to generate a DOCX in which you can read the available styles. You don't use this method anymore.

2. In your script you import the styles using the importStyles method and being sure that the style name you set is exactly the same as the one you can read in the DOCX generated in the previous step.

From your script:

 array(
            'title',
...);

$docx->addText($title, array('pStyle' => 'title'));

If the style 'title' doesn't exist exactly with that name in the DOCX you're importing using the importStyles method it won't be added.

Please send to contact[at]phpdocx.com the DOCX that include the styles you want to import and we'll send you a simple script to show you how it works.

Regards.

Posted by Alain DIAS  · 20-11-2015 - 16:33

Now I found why the parseStyle method didn't work well. In fact you must check in your file that you want to parse that there aren't images, footers, textarea zone ... (or something very specific). I must put only text (with of course styles). So the method generate to me the samples "indicating not only the available styles but also how they can be used with the addText, addTable and addList methods." (like API doc said).

If you want to create custom style, name them in order to not be confused with other default Word style (the numbering in a custom style isn't considered well).

Then when parseStyle method works well. I just import my file (with importStyles method with merge parameter) and applied them by following examples in generated templated.

Really thanks to you admin for your help, it was very helpful.