How can I use a .dot Template on .docx generation in PHP - php

I'm currently writing a docx generator in PHP. It creates tables, images, paragraphs e.g. and saves everything in the correct structure to a .zip (.docx). Now i need to include some macros into that .docx.
I have the macros in a .dot Template on a network drive, which is accessible for my .docx documents. How can I link that Template to my .docx-File?
Important:
I need an approach to link the .dot file in the source of the .docx file. The macros should be automatically added in every .docx my Web-Tool creates for users.
Thanks for every advice

When adding a macro to a docm file, it results in three new files:
/word/_rels/vbaProject.bin.rels
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.microsoft.com/office/2006/relationships/wordVbaData" Target="vbaData.xml"/></Relationships>
and vbAData.bin
and vbaProject.bin
I suggest you to create a file with a macro to see what's in there.

I would like to add that there's an easy way to create docx templates: Using a library I have created and I actively maintain:
https://github.com/edi9999/docxtemplater
Hope that helps

Related

PHP include svg assets in cake php

Does cake have a way to php include svg assets? I know how to use helpers to create an <img> tag pointing to the SVG for the img's src attribute, but I'd like to actually include the file rather than reference it within an <img> tag.
No, CakePHP doesn't ship with such functionality, you'll have to come up with something on your own, or use one of the many PHP based SVG inliners out there, it should be easy enough to wrap that in a custom helper.
If you just need to embed the file, then you could even stick to simply reading and outputting the file contents with the XML declaration and doctype removed, something like:
$svg = file_get_contents($path);
$svg = preg_replace('/^<\?xml.*?\?>\s*(<!DOCTYPE.*?>\s*)?/is', '', $svg);
In the end, this was a silly question. You can of course just use <?php include 'img/thefile.svg' ?> assuming your svg is in webroot/img folder. If the svg or file is not in a publicly accessible folder I would look to creating a custom helper as another post suggested.

wkhtmltopdf style table of contents

I'm wondering how i can go about reducing the font size on the generated table of contents.
I've read and reread their documentation here: https://wkhtmltopdf.org/usage/wkhtmltopdf.txt
The bit i'm confused on is:
The generated XML document can be viewed by dumping it to a file using the
--dump-outline switch. For example:
wkhtmltopdf --dump-outline toc.xml http://qt-project.org/doc/qt-4.8/qstring.html qstring.pdf
I'm having difficulty understanding how to translate to my own project, and what do they mean by this part of the command:
http://qt-project.org/doc/qt-4.8/qstring.html qstring.pdf
If you want to change the default table of content (HTML and / or CSS styles), you can use the xsl-style-sheet parameter to specify a custom XLS stylesheet.
In your custom XLS you can add any HTML tag you need.
You can find an example of the default XLS file here. For your information, le XLS is generated from the tocstylesheet.cc file (see here).

PHP in XML file (or PHP file as XML one)

I have this code (part of bigger script):
flashvars.xmlSource = "datasource.xml";
datasource.xml looks like:
<?xml version="1.0" encoding="utf-8"?>
<Object>
<Contents>
<Source="address" Title="title"></Source>
<Description><h1>New hot Features</h1><p>The all new Piecemaker comes with lots of new features, making it even more slick.</p><p>Just to mention a few - you can now specify unlimited transition styles, include your own SWF and Video files, add hyperlinks to images and info texts with all special characters.</p><p>We also impoved the navigation and the animation with animated shadows and pixel-perfect transitions.</p></Description>
(...)
</Contents>
</Object>
I want to generate datasource.xml dynamically using foreach loop.
I've just changed the file extension to .php but this is not that easy ;)
Any ideas?
Funny or not, but try this one:
leave your file extension to be "xml"
where you wrote (...) write <? PHP CODE HERE ?>
So handle it as if it would be some html file. What I mean is:
<?xml version="1.0" encoding="utf-8"?>
<Object>
<Contents>
<Source="address" Title="title"></Source>
<Description><h1>New hot Features</h1><p>The all new Piecemaker comes with lots of new features, making it even more slick.</p><p>Just to mention a few - you can now specify unlimited transition styles, include your own SWF and Video files, add hyperlinks to images and info texts with all special characters.</p><p>We also impoved the navigation and the animation with animated shadows and pixel-perfect transitions.</p></Description>
<? create php loop here ?>
</Contents>
</Object>
Also note
this line
<Source="address" Title="title"></Source>
might be wrong (you assigned some value to the tagname), try
<Source name="address" Title="title"></Source>
or something like that.
As I see generating xml file with php could be done in this way - for example you'll create file datasource.xml which will be not a static xml file but xml with php code included with contents like
<?php //php code to generate any xml code as Text
// it can be whatever you need to generate
// for example
$content="<h1>New hot Features</h1><p>The all new Piecemaker comes with lots of new features, making it even more slick.</p><p>Just to mention a few - you can now specify unlimited transition styles, include your own SWF and Video files, add hyperlinks to images and info texts with all special characters.</p><p>We also impoved the navigation and the animation with animated shadows and pixel-perfect transitions.</p>";
$output="<Description>".$content."</Description>";
header('Content-type: application/xml');// this is most important php command which says that all output text is XML it must be called before any line of xml will be printed.
// So you need at first generate XML as text then call this command and echo contents of your xml file.
?>
<?xml version="1.0" encoding="utf-8"?>
<Object>
<Contents>
<Source name="address" Title="title"></Source>
<? echo $output; ?>
</Contents>
</Object>
In order to allow php to execute php code inside XML file we need to add some directives to apache host configuration file. In my case I added
<IfModule mod_php5.c>
<FilesMatch "\.xml$">
SetHandler application/x-httpd-php
</FilesMatch>
inside my virtual host configuration file, or you can place this command inside .htaccess file in your directory if Override of this param is allowed in your host configuration.
And about xml- to make sure it's ok you can use http://validator.w3.org/ or http://www.w3schools.com/xml/xml_validator.asp to validate xml generated by your script.
Does this XML need to be a stored file somewhere on the server, or can you just pass it a string formatted like the XML you mentioned. You could write a function that generates the XML you're looking for and returns it, based on input and then call that like
function generateXML($input){
$xml = '<?xml version="1.0" encoding="utf-8"?><Object><Contents>
<Source="address" Title="title"></Source><Whateverelse>' . $input;
$xml .= '</Whateverelse></Contents></Object>';
return $xml;}
flashvars.xmlSource = generateXML("This is whatever else");
If you need to actually generate and store a well formed XML document, or if your XML is fairly complex and you need to generate an object rather than just using a string, you can utilize one of the PHP libraries to do this like http://php.net/manual/en/book.simplexml.php

How can LiveDocx in PHP be used to read .doc & .docx files and read text inside it and save to HTML?

Let's say we have a .doc & .docx files. I want to use LiveDocx in PHP to load the files, read it's content and strip the text from inside it. Then save it to an HTML string.
Can this be done?
I've searched the documentation, and it seams that LiveDocx only loads .doc & .docx template files only!
You can save using external libraries and simply grab the text from the XML within the files:
http://www.webcheatsheet.com/PHP/reading_the_clean_text_from_docx_odt.php
I think you can find what you need in this example.
I might be wrong, but I think they call them "template" files because they act like a template but are still normal .doc/.docx documents. I suggest you simply try to run that example.
I think you can use TextControl that improves phpLiveDocx TextControl link
Using this you can also import pdf doc and docx
When you do document conversion on LiveDocX, you need to do a mailmerge and then retrieve the document. Even though you aren't inserting any new content, you need to do a mailmerge that replaces a dummy placeholder with dummy content.
So, the process I'd suggest is:
1) Set your source document as local template
2) Merge a dummy field with dummy content
3) Retrieve your document as HTML
4) Use a script server side to remove the html and leave only the content (Something like, remove everything between the HEAD tags, then strip_tags on the rest)
5) You should be left with your content as a simple string - I'm not sure it'll be too meaningful, but might be useful for building something like search indices.

How to append XML file to another XML file

I'm trying to create an xml-based template system. The idea is to return my views as xml files (xhtml) and then append them to specific nodes in a template xml file. The template xml is then transformed with xslt.
My question is: how do I insert these xml files into the template xml file?
See DOMDocument::importNode and DOMNode::appendChild.

Categories