Forum


Replies: 8   Views: 4807
Using wordstyles to format replacevariablebyhtml output possible?
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 nicks  · 28-12-2015 - 21:37

Hi,

Having issues with replaceVariableByHTML to replace a tag in a Word file, and trying to format this replaced HTML code from CKEditor to a customised Word Style (font2) that is already in the Word Template. It works fine with addText, though just doesn't work with replaceVariableByHTML.

Any idea what I am doing wrong?

Test code below:

<?php
require_once '../phpdocx_corporate-new/classes/CreateDocx.inc';
$docx = new CreateDocxFromTemplate('template4.docx');
$styles = array('pStyle' => 'font2');
$docx->replaceVariableByHTML('PIC', 'block', '<p>This lack of <span style="color:#FF0000">detail</span> could mean a system does not <span style="background-color:#00FFFF">function correctly.</span></p>
', array('isFile' => false,'wordStyles' => $styles));
$docx->addText('This is some sample paragraph test', array('pStyle' => 'font2'));
$docx->createDocx('test4');
header("Content-type:application/vnd.openxmlformats-officedocument.wordprocessingml.document");
header("Content-disposition:attachment;filename=test4");
readfile('test4.docx');
?>

Posted by admin  · 29-12-2015 - 07:22

Hello,

You need to associate a class, id or tag to the style and use the strictWordStyles option is some to overwrite Word styles is needed.

We recommend you to check the examples available on http://www.phpdocx.com/documentation/introduction/html-to-word-PHP ( Using native Word formatting with HTML). They use embedHtml but the same applies to replaceVariableByHTML.

Regards.

Posted by nicks  · 30-12-2015 - 12:10

Thanks for the quick reply. I'll give that a go!

Posted by nicks  · 30-12-2015 - 19:39

Hi,

I've experimented with this. I've found that it doesn't follow the applied formatting unless I add 

'strictWordStyles' => true

If I do this, then it stripes out the formatting such as text colouring though does change the font size. Is it meant to act like this?

Thanks

Posted by admin  · 04-01-2016 - 09:29

Hello,

You can't mix both Word and CSS styles using the same replace method. You need to do it using CSS styles or Word styles.

To mix both styles, you need to change the library, please check this post:

http://www.phpdocx.com/en/forum/default/topic/1115

That explains how to do that.

Regards.

Posted by nicks  · 04-01-2016 - 16:39

Thanks for the pointers. I had tried using standard CSS in the past with no luck, then read about the requirement of Tidy. Once installed that I could set a CSS for my output from CKEditor to ensure the font for <p> tags was set as I required with the following:

<style>p { font-size: 10pt; } </style>

Posted by nicks  · 12-01-2016 - 10:24

When using the replaceVariableByHTML on <p> tags I've found that the text within the generated Word document has 10pt spacing before and after on the formatted text.

Is there a way to remove this?

Posted by nicks  · 12-01-2016 - 17:51

Managed to solve this by specifying a margin and padding:

<style>p{font-size:10pt;margin:0;padding:0;}</style>

Posted by admin  · 13-01-2016 - 09:24

Hello,

That's a good solution. Phpdocx adds default spacing for paragraphs, so you need to set it manually when you add the paragraph or the placeholder in the template.

Regards.