2
votes

I am generating PDF using XSL/FO which converts HTML to PDF.

I am using XSL/FO stylesheet, that generates a .fo file. Apache FOP generates a pdf from the .fo file.

In my html file there are so many tag which contains "display" inline css property due to this below error occurs :

javax.xml.transform.TransformerException: org.xml.sax.SAXParseException; systemId: file:/E:/Projects/PDF/xhtml2fo.xsl; lineNumber: 484; columnNumber: 44; Invalid property encountered on "fo:block": display (No context info available)

Caused by: org.apache.fop.fo.ValidationException: Invalid property encountered on "fo:block": display (No context info available) at org.apache.fop.events.ValidationExceptionFactory.createException(ValidationExceptionFactory.java:38) at org.apache.fop.events.EventExceptionManager.throwException(EventExceptionManager.java:58) at org.apache.fop.events.DefaultEventBroadcaster$1.invoke(DefaultEventBroadcaster.java:173) at com.sun.proxy.$Proxy65.invalidProperty(Unknown Source) at org.apache.fop.fo.PropertyList.handleInvalidProperty(PropertyList.java:557) at org.apache.fop.fo.PropertyList.convertAttributeToProperty(PropertyList.java:476) at org.apache.fop.fo.PropertyList.addAttributesToList(PropertyList.java:386) at org.apache.fop.fo.FObj.processNode(FObj.java:124) at org.apache.fop.fo.FOTreeBuilder$MainFOHandler.startElement(FOTreeBuilder.java:291) at org.apache.fop.fo.FOTreeBuilder.startElement(FOTreeBuilder.java:179) at org.apache.xalan.transformer.ResultTreeHandler.flushElem(ResultTreeHandler.java:860) ... 72 more

html code with display property:

<div id="something" style="display:block;">Some value</div>

How can we include "display" style part processing in xsl:

<xsl:template name="process-style">
1
In order for anyone to help you you would need to include the xslt you are using to generate the xsl-fo. I am supposing (based on reading your question) that you are doing something like turning every div into an fo block, and copying over attributes, unfortunately the style attribute is not allowed on an fo block therefore you cannot do that. You will instead have to style all fo elements in different ways.user254694
Thanks for reply. I can include xsl but can't xslt. As in this process of PDF conversion from HTML, xslt is generated in middle of process (and cant be stored). Can you please elaborate on "style all fo elements in different ways" that would be indeed helpful.Vikramsinh Gaikwad
ok then I don't think you are in the right group. whatever tool you are using that generates xslt from structures is the one you should be tagging your question as being about and ask the question relevant to that tool. I am going to flag this question as not being salvageable in its current form. Try to ask again about your tool that generates the xslt that then generates xsl-fo.user254694
Perhaps the XSL you use plucks apart and uses the attributes as they are. The attribute "display" is not valid XSL FO. No more than "-moz-border-radius" would be valid. You need to find an XSL that converts CSS styles into "valid" XSL FO.Kevin Brown

1 Answers

2
votes

Perhaps this can assist you in putting together a package that works for you. There is an application on the web at http://www.cloudformatter.com/CSS2Pdf.

This application works to extract a <div> or <div>'s in the existing browser with all CSS resolved from all sources, builds that into an XML and sends to format. As part of that formatting is an XSL that converts that content to XSL FO. That XSL is here.

http://xep.cloudformatter.com/doc/XSL/xeponline-fo-translate-2.xsl

If you take a look through this XSL (yes, it is 1.0 and could be much more efficient in 2.0), you would find the portions where all style attributes are recursively parsed to bust apart and then XSL FO attributes created from them.

It starts here around line 1593:

<xsl:template name="processCSSStyle">
    <xsl:param name="cssString"/>
    <xsl:param name="type"/>
    <xsl:param name="float"/>
    <xsl:param name="ignoreHeight"/>
    <xsl:call-template name="processCSSEntry">
      <xsl:with-param name="attr" select="normalize-space(substring-before($cssString, ': '))"/>
      <xsl:with-param name="value"
        select="normalize-space(substring-after(substring-before($cssString, '; '), ': '))"/>
      <xsl:with-param name="cssRemaining" select="substring-after($cssString, '; ')"/>
      <xsl:with-param name="type" select="$type"/>
      <xsl:with-param name="float" select="$float"/>
      <xsl:with-param name="ignoreHeight" select="$ignoreHeight"/>
    </xsl:call-template>
  </xsl:template>

Working through the list of CSS styles in the style.

<xsl:template name="processCSSEntry">
    <xsl:param name="attr"/>
    <xsl:param name="value"/>
    <xsl:param name="cssRemaining"/>
    <xsl:param name="type"/>
    <xsl:param name="float"/>
    <xsl:param name="ignoreHeight"/>
    <xsl:if test="$attr">
      <xsl:call-template name="handleAttr">
        <xsl:with-param name="attr" select="$attr"/>
        <xsl:with-param name="value" select="$value"/>
        <xsl:with-param name="type" select="$type"/>
        <xsl:with-param name="float" select="$float"/>
        <xsl:with-param name="ignoreHeight" select="$ignoreHeight"/>
      </xsl:call-template>
      <xsl:call-template name="processCSSEntry">
        <xsl:with-param name="attr" select="normalize-space(substring-before($cssRemaining, ': '))"/>
        <xsl:with-param name="value"
          select="normalize-space(substring-after(substring-before($cssRemaining, '; '), ': '))"/>
        <xsl:with-param name="cssRemaining" select="substring-after($cssRemaining, '; ')"/>
        <xsl:with-param name="type" select="$type"/>
        <xsl:with-param name="float" select="$float"/>
        <xsl:with-param name="ignoreHeight" select="$ignoreHeight"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>

Eventually these individual things are processed in a choose/when structure, with things like this:

  <xsl:when test="$attr = 'margin-left'">
    <xsl:attribute name="margin-left">
      <xsl:value-of select="$value"/>
    </xsl:attribute>
  </xsl:when>

So it is breaking apart the style attribute into it's components and making XSL FO attributes.

I am not going to post more as you can download and examine the XSL. Because it is live from browser content there are many rules applied here that may not be relevant to strip out attributes that have no meaning in XSL FO as well as very specific handling for areas where XSL FO and HTML differ.