2

Hello I am trying to generate MS-Word reports from a PHP application. In order to do so I am converting XML data into Microsoft Office Open XML by using XSLT Transformations.

I have a simple XML file to pull the data:

<?xml version="1.0" encoding="UTF-8"?>
<Movies>
    <Genre name="Action">
        <Movie>
            <Name>Crash</Name>
            <Released>2005</Released>
        </Movie>
    </Genre>
    <Genre name="Drama">
        <Movie>
            <Name>The Departed</Name>
            <Released>2006</Released>
        </Movie>
        <Movie>
            <Name>The Pursuit of Happyness</Name>
            <Released>2006</Released>
        </Movie>
    </Genre>
    <Genre name="Comedy">
        <Movie>
            <Name>The Bucket List</Name>
            <Released>2007</Released>
        </Movie>
    </Genre>
</Movies>

I also have a XSLT file that transforms the XML into MS Office XML:

<xsl:for-each select="Movies/Genre">
    <w:p w:rsidR="00EC137C" w:rsidRPr="00BF ...
        <w:pPr>
            <w:pStyle w:val="Heading2"/>
        </w:pPr>
        <w:r w:rsidRPr="00BF350E">
            <w:t>
                <xsl:value-of select="@name"/>
            </w:t>
        </<xsl:value-of select w:r>
    </w:p>
    <xsl:for-each select="Movie">
        <w:p w:rsidR="00EC137C" w:rsidRPr="00EC1 ...
            <w:pPr>
                <w:pStyle w:val="ListParagraph"/>
                <w:numPr>
                    <w:ilvl w:val="0"/>
                    <w:numId w:val="1"/>
                </w:numPr>
            </w:pPr>
            <w:r w:rsidRPr="00BF350E">
                <w:rPr>
                    <w:b/>
                </w:rPr>
                <w:t>
                    <xsl:value-of select="Name"/>
                </w:t>
            </w:r>
            <w:r w:rsidR="00C46B60">
                <w:t xml:space="preserve"> (<xsl:value-of select="Released"/>)
                </w:t>
            </w:r>
        </w:p>
    </xsl:for-each>
</xsl:for-each>
...

And also the PHP script:

<?php
//Declare variables for file names.
$xmlDataFile = "MyMovies.xml";
$xsltFile = "MyMovies.xslt";
$sourceTemplate = "MyMoviesTemplate.docx";
$outputDocument = "MyMovies.docx";

//Load the xml data and xslt and perform the transformation.
$xmlDocument = new DOMDocument();
$xmlDocument->load($xmlDataFile);

$xsltDocument = new DOMDocument();
$xsltDocument->load($xsltFile);

$xsltProcessor = new XSLTProcessor();
$xsltProcessor->importStylesheet($xsltDocument);

//After the transformation, $newContentNew contains 
//the XML data in the Open XML Wordprocessing format.
$newContent =  $xsltProcessor->transformToXML($xmlDocument);

//Copy the Word 2007 template document to the output file.
if (copy($sourceTemplate, $outputDocument)) {

    //Open XML files are packaged following the Open Packaging 
    //Conventions and can be treated as zip files when 
    //accessing their content.
    $zipArchive = new ZipArchive();
    $zipArchive->open($outputDocument);

    //Replace the content with the new content created above.
    //In the Open XML Wordprocessing format content is stored
    //in the document.xml file located in the word directory.
    $zipArchive->addFromString("word/document.xml", $newContent);
    $zipArchive->close();

    echo "Processing Complete";
}
?>

Now I need to insert images in the reports. What should I have in the XML file and what should I have in the XSLT file? I am a beginner with XML and XSLT I just took the sample from msdn but now that I need to insert the images in the report I don't know how to do it?

Regards

ivantxo
  • 719
  • 5
  • 18
  • 36
  • I know you can insert binary/into Word documents for WordML (to my understanding, images can be converted to a Base64 string, but I'm not sure how you'd go about this). Check out - http://www.codeproject.com/KB/office/Generating_Word_Reports.aspx#InsertingImagesIntoDocument I hope that's helpful to you - sorry I can't tell you more about the implementation. – Aaron Newton Dec 06 '11 at 00:40
  • @AaronNewton thanks for your answer. I tried that one but I don't understand it well. I wonder if I can do something like this http://stackoverflow.com/a/45955, but I cannot get it to work. – ivantxo Dec 06 '11 at 00:44
  • It looks like there may be a couple of options. See http://www.tkachenko.com/blog/archives/000106.html for C# implementation. Also see http://stackoverflow.com/questions/5406625/add-image-using-wordml as it looks like someone has written a Java implementation. – Aaron Newton Dec 06 '11 at 00:55
  • I am really confused about how to insert an image into a word document with xslt. I don't know how to do it. – ivantxo Dec 06 '11 at 06:42
  • What you could try is to start Microsoft Word, add an image to a document manually, save it and then look at the contents of the .docX file (it's just a zip file) to see if you can make sense of how the image is stored. – Tim C Dec 06 '11 at 10:32
  • No I can't figure out what the docx has inside. I checked document.xml and I don't know how based on this I am going to insert my graph. – ivantxo Dec 07 '11 at 06:11
  • Any idea. I really need to insert this image into the word document. The image goes at the end of the document. So, what I am trying to do is using COM and inserting the image, right. Now the problem is how can I go to the end of the Word document if I open it with COM? – ivantxo Dec 07 '11 at 13:09
  • So stupid. This was very easy. I found the solution in [this blog](http://blogs.msdn.com/b/dmahugh/archive/2006/12/10/images-in-open-xml-documents.aspx). – ivantxo Dec 09 '11 at 00:11
  • @ivantxo The link you gave above is broken. Can you share how you did it here? I know its been 9 years since you posted this.Just in case you remember :) – Suhani Mar 18 '20 at 13:53
  • @Suhani sorry I did this for an old project I can see the blog was archived and now I can't find it. Try to do this search om Google: site:learn.microsoft.com images in open xml documents – ivantxo Mar 19 '20 at 00:06

0 Answers0