Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing document properties when using Template Processor or loadTemplate() #908

Open
ghost opened this issue Oct 6, 2016 · 26 comments
Open

Comments

@ghost
Copy link

ghost commented Oct 6, 2016

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.

@christopher-francisco
Copy link

bump, is it possible to modify document properties?

@troosan
Copy link
Contributor

troosan commented Jun 27, 2017

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

@icy2003
Copy link

icy2003 commented Jan 28, 2019

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?

@troosan
Copy link
Contributor

troosan commented Jan 30, 2019

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

@icy2003
Copy link

icy2003 commented Feb 1, 2019

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

@troosan
Copy link
Contributor

troosan commented Feb 4, 2019

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

@icy2003
Copy link

icy2003 commented Feb 5, 2019

@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

@troosan
Copy link
Contributor

troosan commented Feb 5, 2019

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

@icy2003
Copy link

icy2003 commented Feb 5, 2019

@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
Copy link

chrisribe commented Jul 11, 2019

@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.

@icy2003
Copy link

icy2003 commented Jul 11, 2019

@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

@chrisribe
Copy link

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

@icy2003
Copy link

icy2003 commented Jul 11, 2019

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

@icy2003
Copy link

icy2003 commented Jul 11, 2019

@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

@chrisribe
Copy link

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:

$docInfo->setCustomProperty('key1', null);

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

@icy2003
Copy link

icy2003 commented Jul 11, 2019

@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

@chrisribe
Copy link

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".

@icy2003
Copy link

icy2003 commented Jul 11, 2019

@chrisribe setIsUpdateFields

@chrisribe
Copy link

@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 :)

@icy2003
Copy link

icy2003 commented Jul 11, 2019

@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
Copy link

icy2003 commented Jul 11, 2019

@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!!

@chrisribe
Copy link

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

@icy2003
Copy link

icy2003 commented Jul 11, 2019

@chrisribe may help you with my lib

@icy2003
Copy link

icy2003 commented Jul 13, 2019

@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 :)

@olivermbs
Copy link

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');

@Marre-86
Copy link

Marre-86 commented Feb 9, 2024

I'm also interested in this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

6 participants