Get basic HTML formattings working in word 2003, formattings inside table working in word documents

This commit is contained in:
Nathan Gray 2011-08-25 20:28:24 +00:00
parent fb62831e86
commit 0571ac47e2
3 changed files with 34 additions and 17 deletions

View File

@ -377,13 +377,14 @@ abstract class bo_merge
); );
// It seems easier to split the parent tags here // It seems easier to split the parent tags here
$replace_tags = array( $replace_tags = array(
// Tables, lists don't go inside <w:p>
'/<(ol|ul|table)( [^>]*)?>/' => '</w:t></w:r></w:p><$1$2>', '/<(ol|ul|table)( [^>]*)?>/' => '</w:t></w:r></w:p><$1$2>',
'/<\/(ol|ul|table)>/' => '</$1><w:p><w:r><w:t>', '/<\/(ol|ul|table)>/' => '</$1><w:p><w:r><w:t>',
// Fix for things other than text (newlines) inside table row
'/<(td)( [^>]*)?>((?!<w:t>))(.*?)<\/td>[\s]*?/' => '<$1$2><w:t>$4</w:t></td>',
'/<(li)(.*?)>(.*?)<\/\1>/' => '<$1 $2>$3</$1>', '/<(li)(.*?)>(.*?)<\/\1>/' => '<$1 $2>$3</$1>',
/* // Remove extra whitespace
'/<(span)(.*?)>/' => "\n".'</w:t></w:r><$1$2>', '/<w:t>[\s]+(.*?)<\/w:t>/' => '<w:t>$1</w:t>'
'/<\/(span)>/' => '</$1><w:r><w:t>'."\n",
*/
); );
$content = preg_replace(array_keys($replace_tags),array_values($replace_tags),$content); $content = preg_replace(array_keys($replace_tags),array_values($replace_tags),$content);
//echo $content;die(); //echo $content;die();
@ -400,17 +401,18 @@ abstract class bo_merge
try try
{ {
$element = new SimpleXMLelement($content); $element = new SimpleXMLelement($content);
$content = $xslt->transformToXml($element); $content = @$xslt->transformToXml($element);
// Word 2003 needs invalid XML, add back in // Word 2003 needs two declarations, add extra declaration back in
if($mimetype == 'application/xml' && $mso_application_progid == 'Word.Document') { if($mimetype == 'application/xml' && $mso_application_progid == 'Word.Document' && strpos($content, '<?xml') !== 0) {
$content = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'.$content; $content = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'.$content;
} }
// Validate // Validate
/*
$doc = new DOMDocument(); $doc = new DOMDocument();
// $doc->loadXML($content); $doc->loadXML($content);
// $doc->schemaValidate('/home/nathan/Downloads/WordprocessingML Schemas/wordnet.xsd'); $doc->schemaValidate(*Schema (xsd) file*);
//echo $content;die(); */
} }
catch (Exception $e) catch (Exception $e)
{ {

View File

@ -86,7 +86,7 @@ Breakers
</xsl:template> </xsl:template>
--> -->
<xsl:template match="w:r[descendant::strong|descendant::em|descendant::u|descendant::span]"> <xsl:template name="apply-styles" match="w:r[descendant::strong|descendant::em|descendant::u|descendant::span]">
<xsl:for-each select="node()|@*[not(w:rPr)]"> <xsl:for-each select="node()|@*[not(w:rPr)]">
<xsl:choose> <xsl:choose>
<xsl:when test="descendant::strong|descendant::em|descendant::u|descendant::span" > <xsl:when test="descendant::strong|descendant::em|descendant::u|descendant::span" >
@ -221,7 +221,7 @@ Breakers
<xsl:template match="table"> <xsl:template match="table">
<w:tbl> <w:tbl>
<w:tblPr> <w:tblPr>
<w:tblW w:type="dxa" w:w="9972"/> <w:tblW w:type="pct" w:w="4500"/>
<w:jc w:value="left"/> <w:jc w:value="left"/>
<xsl:if test="@border &gt; 0"> <xsl:if test="@border &gt; 0">
<w:tblBorders> <w:tblBorders>
@ -251,7 +251,13 @@ Breakers
<w:right w:color="000000" w:space="0" w:sz="{$width}" w:val="single"/> <w:right w:color="000000" w:space="0" w:sz="{$width}" w:val="single"/>
</w:tcBorders></w:tcPr> </w:tcBorders></w:tcPr>
</xsl:if> </xsl:if>
<w:p><w:r><w:t><xsl:apply-templates select="child::node()" /></w:t></w:r></w:p> <w:p>
<xsl:call-template name="apply-styles">
<w:r>
<xsl:copy-of select="node()|@*"/>
</w:r>
</xsl:call-template>
</w:p>
</w:tc> </w:tc>
</xsl:for-each> </xsl:for-each>
</w:tr> </w:tr>

View File

@ -87,7 +87,7 @@ Breakers
</xsl:template> </xsl:template>
--> -->
<xsl:template match="w:r[descendant::strong|descendant::em|descendant::u|descendant::span]"> <xsl:template match="w:r[descendant::strong|descendant::em|descendant::u|descendant::span]" name="apply-styles">
<xsl:for-each select="node()|@*[not(w:rPr)]"> <xsl:for-each select="node()|@*[not(w:rPr)]">
<xsl:choose> <xsl:choose>
<xsl:when test="descendant::strong|descendant::em|descendant::u|descendant::span" > <xsl:when test="descendant::strong|descendant::em|descendant::u|descendant::span" >
@ -123,6 +123,7 @@ Breakers
</w:t> </w:t>
<w:br/> <w:br/>
</xsl:template> </xsl:template>
<xsl:template match="i|em"> <xsl:template match="i|em">
<w:i /> <w:i />
</xsl:template> </xsl:template>
@ -165,7 +166,7 @@ Breakers
</xsl:otherwise> </xsl:otherwise>
</xsl:choose> </xsl:choose>
</xsl:variable> </xsl:variable>
<w:shd w:fill="{$hex}" w:val="solid"/> <w:shd w:fill="{$hex}" w:val="clear"/>
</xsl:if> </xsl:if>
<xsl:if test="starts-with(.,'font-size')"> <xsl:if test="starts-with(.,'font-size')">
<xsl:variable name="font-size" select="substring-after(text(),'font-size:')" /> <xsl:variable name="font-size" select="substring-after(text(),'font-size:')" />
@ -218,8 +219,8 @@ Breakers
<xsl:template match="table"> <xsl:template match="table">
<w:tbl> <w:tbl>
<w:tblPr> <w:tblPr>
<w:tblW w:type="dxa" w:w="9972"/>
<w:jc w:val="left"/> <w:jc w:val="left"/>
<w:tblW w:w="5000" w:type="pct"/>
</w:tblPr> </w:tblPr>
<w:tblGrid> <w:tblGrid>
<xsl:for-each select="./tr[1]/td"> <xsl:for-each select="./tr[1]/td">
@ -229,7 +230,15 @@ Breakers
<xsl:for-each select="./tr"> <xsl:for-each select="./tr">
<w:tr> <w:tr>
<xsl:for-each select="./td"> <xsl:for-each select="./td">
<w:tc><w:p><w:r><w:t><xsl:apply-templates select="child::node()" /></w:t></w:r></w:p></w:tc> <w:tc>
<w:p>
<xsl:call-template name="apply-styles">
<w:r>
<xsl:copy-of select="node()|@*"/>
</w:r>
</xsl:call-template>
</w:p>
</w:tc>
</xsl:for-each> </xsl:for-each>
</w:tr> </w:tr>
</xsl:for-each> </xsl:for-each>