Category Archives: apache-fop

Issue in xsl stylesheet when creating pdf using apache fop

I want to convert html document to pdf using Apache FOP. I tried https://blogs.oracle.com/chrisf/entry/converting_html_to_pdf_using worked fine for a basic html file. but when the page has a right align paragraph this wont work

<p style= "font-size: 10px;margin: 5px 0px 20px; padding: 0px 20px 0px 0px; line-height: 130%;max-width: 258px;">
                <b> Accounts details</b><br>
                $Description <br>
                $address_line1 $address_line2<br>
                $address_city, $address_state $address_zip <br>
                $address_country <br>
</p>

I tried to create a simple xsl file but getting the bellow error

(Location of error unknown)org.apache.fop.fo.ValidationException: First element must be the fo:root formatting object. Found (Namespace URI: "http://www.w3.org/1999/xhtml", Local Name: "html") instead. Please make sure you're producing a valid XSL-FO document.

Please find the xsl file I created.

  <?xml version="1.0" encoding="utf-8" standalone="no" ?>
<xsl:stylesheet xmlns="http://www.w3.org/1999/xhtml" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" encoding="utf-8" indent="yes" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" />
    <xsl:template match="/">
        <xsl:element name="html">
            <xsl:attribute name="xmlns">http://www.w3.org/1999/xhtml</xsl:attribute>
            <xsl:element name="head">
                <xsl:element name="meta">
                    <xsl:attribute name="equiv">content-type</xsl:attribute>
                    <xsl:attribute name="content">text/html;charset=utf-8</xsl:attribute>
                </xsl:element>
                <xsl:element name="title">
                    Happy
                </xsl:element>
            </xsl:element>
            <xsl:element name="body">
                <p style= "font-size: 10px;margin: 5px 0px 20px; padding: 0px 20px 0px 0px; line-height: 130%;
                                  max-width: 258px;">
                    <b>Accounts Payable</b><br/>
                    $Description <br/>
                    $address_line1 $address_line2<br/>
                    $address_city, $address_state $address_zip <br/>
                    $address_country <br/>
                </p>
            </xsl:element>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

Apache fop generated PDF renders hyperlinks by replacing accented characters with ‘?’

I have used Apache FOP 1.1 to programmatically generate PDF. The PDF is supposed to contain name of a document as hyperlink. When I click on the name, it should open the corresponding file. Here is the code:

<fo:block>
    <fo:basic-link color="blue" show-destination="new">
        <xsl:attribute name="external-destination">
            <xsl:choose>
                <xsl:when test="@parentFolderPath">
                   <xsl:value-of select="@parentFolderPath" />/<xsl:value-of select="@FileName" />
                </xsl:when>
                <xsl:otherwise>
                   <xsl:value-of select="@FileName" />
                </xsl:otherwise>
            </xsl:choose>
        </xsl:attribute><xsl:call-template name="writeWithoutOverlap"><xsl:with-param name="data" select="@FileName"/></xsl:call-template>                   
    </fo:basic-link>
</fo:block>

This works perfectly fine when I have a file having English characters in the name. However, when I have a file name like this: "étudiant où forêt naïve garçon.docx" the hyperlink formed in the PDF replaces the accented characters with '?'.

This is a screen shot of the PDF where you can see the malformed hyperlink.

I am using "Arial" font and encoding="UTF-8".

When the name of the file is getting printed correctly, why is the hyperlink giving a problem?