1
votes

I have try to generate PDF from HTML using FOP. My problem is background color in not working for Table. My html code is

    <table cols="100 100pt" border="1" width="100%" align="center">
                <thead>
                    <tr>
                            <th style="background-color: blue"><span style="color: blue">col 1</span></th>
                            <th style="background-color: #ffffff">col 2</th>
</thead>
<tbody>
<tr>
                            <td style="background-color: #ffffff">value 1</td>
                            <td>Value 2</td>
</tr>
</tbody>
</table>

and my xsl code is

<xsl:template match="table">
    <fo:table table-layout="fixed">
        <xsl:choose>
            <xsl:when test="@cols">
                <xsl:call-template name="build-columns">
                    <xsl:with-param name="cols"
                            select="concat(@cols, ' ')" />
                </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                    <fo:table-column column-width="200pt" />
            </xsl:otherwise>
        </xsl:choose>
        <fo:table-body>
            <xsl:apply-templates select="*" />
        </fo:table-body>
    </fo:table>
</xsl:template>
<xsl:template match="td">
  <fo:table-cell>
    <fo:block text-align="{$align}">
      <xsl:apply-templates select="*|text()"/>
    </fo:block>
  </fo:table-cell>
</xsl:template>

Can any one know how to add back ground color for table in PDF?

1

1 Answers

0
votes

IF the only property in @style is background-color and the values are as consistent in your example, then extract the color from @style if @style exists:

<xsl:template match="td">
  <fo:table-cell>
    <xsl:if test="@style">
      <xsl:attribute name="background-color">
        <xsl:value-of select="substring-after(@style, 'background-color: ')" />
      </xsl:attribute>
    </xsl:if>
  ...

This could be simpler if you were using XSLT 2.0 or XSLT 3.0.