PHPWord icon indicating copy to clipboard operation
PHPWord copied to clipboard

Changing document properties when using Template Processor or loadTemplate()

Open ghost opened this issue 8 years ago • 26 comments

Does anyone have idea how to set new document properties (setCreator, setCompany, setTitle) when template is loaded and processed? I got document properties to work fine when creating new document but can't get it to work when using loadTemplate(). Thanks!


Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

ghost avatar Oct 06 '16 16:10 ghost

bump, is it possible to modify document properties?

christopher-francisco avatar Dec 16 '16 14:12 christopher-francisco

This is currently not possible, the TemplateProcessor only allows you to change the body and the header of the document

troosan avatar Jun 27 '17 21:06 troosan

This is currently not possible, the TemplateProcessor only allows you to change the body and the header of the document

@troosan If it is not possible, with this problem https://github.com/PHPOffice/PHPWord/issues/991, how can I change docx settings?

icy2003 avatar Jan 28 '19 06:01 icy2003

I guess your best option is to use applyXslStyleSheet(...) But it would need some code changes, as currently the settings.xml is not loaded

troosan avatar Jan 30 '19 21:01 troosan

I guess your best option is to use applyXslStyleSheet(...) But it would need some code changes, as currently the settings.xml is not loaded

I have a Class that extends TemplateProcessor with my own lib to change docx settings. :) @troosan

icy2003 avatar Feb 01 '19 09:02 icy2003

@icy2003 thanks. I integrated your changes in the develop branch

troosan avatar Feb 04 '19 23:02 troosan

@troosan thx actually,this is because I need the function in my work so I wrote that function,will you please add more functions about settings or others because I think templateprocesser is.so powerful and it should not be only this. I am not familiar with docx, so I dont know if there has more. I think it will be helpful

icy2003 avatar Feb 05 '19 20:02 icy2003

@icy2003 indeed, I was thinking to change this method to be more generic instead of supporting only this feature. to be followed!

troosan avatar Feb 05 '19 20:02 troosan

@troosan I will follow it. by the way, It is too late(5:00) in China, and I am about to sleep, good night.Thx for your branch change again

icy2003 avatar Feb 05 '19 21:02 icy2003

@icy2003 thanks. I integrated your changes in the develop branch

@troosan can you link to where this was integrated. I am looking into setting custom document properties and templateprocessor does not seem to support it. :(

@icy2003 your link (https://github.com/icy2003/icy2003_php/blob/master/iexts/phpword/TemplateProcessor.php) seems to be dead.

chrisribe avatar Jul 11 '19 12:07 chrisribe

@troosan I will follow it. by the way, It is too late(5:00) in China, and I am about to sleep, good night.Thx for your branch change again

@chrisribe emmmmm……I put it in another folder, now it is https://github.com/icy2003/php/blob/master/src/iexts/PhpOffice/PhpWord/TemplateProcessor.php and will never change again

icy2003 avatar Jul 11 '19 12:07 icy2003

Thanks !

But it seems to have changed a lot or is out of date compared to the current one on the develop branch. https://github.com/PHPOffice/PHPWord/blob/develop/src/PhpWord/TemplateProcessor.php

Where you able to set the document properties via TemplateProcessor ? An how ? Thanks

chrisribe avatar Jul 11 '19 13:07 chrisribe

@chrisribe by using regular expression, setting operate function is only setIsUpdateFields, others are some operations for generating blocks

icy2003 avatar Jul 11 '19 13:07 icy2003

@chrisribe I provide a function for setting xml part,you can match the string for your own function, the way I wrote setIsUpdateFields was by comparing the xml I changed before and the after,maybe it's not a good way, but it runs well

icy2003 avatar Jul 11 '19 14:07 icy2003

If I understand correctly you load the template then replace xml after...

It is just not clear how you can proceed to load your template, set your custom property value then save. Do you have small pseudo code example ?

I am looking to perform this on the template then save, PhpWord setCustomProperty: https://github.com/PHPOffice/PHPWord/blob/4fa945561f02893263e311e4c7c745df3a5dfee0/tests/PhpWord/Writer/Word2007/Part/DocumentTest.php#L52

Sorry I do not have any experience parsing docx xml values and their values relative to the docx format.

chrisribe avatar Jul 11 '19 14:07 chrisribe

@chrisribe sorry for that......I don't know about custom property until you ask me......all I know is to use xdebug and run and test and change word document and unzip and compare xml again and again

icy2003 avatar Jul 11 '19 14:07 icy2003

Ah ok I get it.

That allows you to figure out what is what and how the xml stucture changes based on set values in the docx. So when using your lib you never set things like "setCompany" etc ? The code is more for debug purposes ?

Was assuming you could set properties, since this issue is about "document properties".

chrisribe avatar Jul 11 '19 15:07 chrisribe

@chrisribe setIsUpdateFields

icy2003 avatar Jul 11 '19 15:07 icy2003

@icy2003 https://github.com/icy2003/php/blob/a1d96b23913d34f6e516823c1a5d5b300a4f2ac2/src/iexts/PhpOffice/PhpWord/TemplateProcessor.php#L331

Only has a true/false parameter, that's why I am having a hard time decoding your code.

_tempDocumentSettingPart == target template (init on construct).

tempDocumentMainPart == seems to be init via setMain

That's why I wanted a pseudo example on how you use your lib :)

chrisribe avatar Jul 11 '19 15:07 chrisribe

@icy2003 https://github.com/icy2003/php/blob/a1d96b23913d34f6e516823c1a5d5b300a4f2ac2/src/iexts/PhpOffice/PhpWord/TemplateProcessor.php#L331

Only has a true/false parameter, that's why I am having a hard time decoding your code.

_tempDocumentSettingPart == target template (init on construct).

tempDocumentMainPart == seems to be init via setMain

That's why I wanted a pseudo example on how you use your lib :)

@chrisribe check this: https://github.com/icy2003/php/blob/master/src/iexts/PhpOffice/PhpSpreadsheet/Worksheet/iWorksheet.php for example: word document:

${table111}
${/table111}

php:

$templateProcessor->setTable('table111', $array);

$array is come from iWorksheet::rangeToArray which is from Worksheet::rangeToArray,I wrote templateProcessor because I need convert Excel to Word table, you can find the differences easily beacause I named them the similar namespace between phpoffice and my lib by the way, templateProcessor is not for setting property but for replacing blocks, if you really want to set properties, you need to match them yourself

icy2003 avatar Jul 11 '19 15:07 icy2003

@chrisribe you may use cloneRow to clone a table in templateProcessor from phpoffice, but it cannot help me to convert any, I mean any Excel table to word table, and I need to bring styles from excel to word, and, also the merge cell parts!!

icy2003 avatar Jul 11 '19 16:07 icy2003

@icy2003 thanks for taking the time to explain, I get what you are doing now.

chrisribe avatar Jul 11 '19 17:07 chrisribe

@chrisribe may help you with my lib

icy2003 avatar Jul 11 '19 17:07 icy2003

@icy2003 thanks for taking the time to explain, I get what you are doing now.

@chrisribe if you want to match yourself, see this: https://github.com/icy2003/php/blob/master/samples/php7preg_bug.php it may help you if you use php7.0~7.2 and, if my lib helps you in some way, give a star thanks :)

icy2003 avatar Jul 13 '19 08:07 icy2003

Any update on this? I am trying to change the word document title on a document generated through TemplateProcessor, is there a way of doing this?

$templateProcessor = new TemplateProcessor(__DIR__ . '/Template.docx');
$templateProcessor->setTitle('Title goes here');

olivermbs avatar May 04 '22 09:05 olivermbs

I'm also interested in this

Marre-86 avatar Feb 09 '24 17:02 Marre-86