Inserting a line break in a PDF generated from XSL FO using

后端 未结 11 749
挽巷
挽巷 2020-12-03 17:16

I am using XSL FO to generate a PDF file containing a table with information. One of these columns is a \"Description\" column. An example of a string that I am populating o

相关标签:
11条回答
  • 2020-12-03 17:33

    Generating strings containing escaped XML markup is seldom the right answer, but if that's what you have to work with, then for input like this:

    <Description><![CDATA[This is an example Description.<br/>List item 1<br/>List item 2<br/>List item 3<br/>List item 4]]></Description>
    

    if you're using XSLT 2.0, you can use xsl:analyze-string to get the empty fo:block that you originally wanted:

    <xsl:template match="Description">
      <fo:block>
        <xsl:analyze-string select="." regex="&lt;br/>">
          <xsl:matching-substring>
            <fo:block />
          </xsl:matching-substring>
          <xsl:non-matching-substring>
            <xsl:value-of select="." />
          </xsl:non-matching-substring>
        </xsl:analyze-string>
      </fo:block>
    </xsl:template>
    

    but if you are using XSLT 2.0, you can more concisely use linefeed-treatment="preserve" as per @Daniel Haley and use replace() to insert the linefeeds:

    <xsl:template match="Description">
      <fo:block linefeed-treatment="preserve">
        <xsl:value-of select="replace(., '&lt;br/>', '&#xA;')" />
      </fo:block>
    </xsl:template>
    

    If you are using XSLT 1.0, you can recurse your way through the string:

    <xsl:template match="Description">
      <fo:block linefeed-treatment="preserve">
        <xsl:call-template name="replace-br" />
      </fo:block>
    </xsl:template>
    
    <xsl:template name="replace-br">
      <xsl:param name="text" select="." />
    
      <xsl:choose>
        <xsl:when test="not(contains($text, '&lt;br/>'))">
          <xsl:value-of select="$text" />
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="substring-before($text, '&lt;br/>')"/>
          <xsl:text>&#xA;</xsl:text> <!-- or <fo:block /> -->
          <xsl:call-template name="replace-br">
            <xsl:with-param name="text" select="substring-after($text, '&lt;br/>')"/>
          </xsl:call-template>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:template>
    
    0 讨论(0)
  • 2020-12-03 17:34

    I had a text block that looks like this

    <fo:table-cell display-align="right">
    <fo:block font-size="40pt" text-align="right">
        <xsl:text> Text 1 </xsl:text>
        <fo:block> </fo:block>
        <xsl:text> Text2 </xsl:text>
        <fo:block> </fo:block>
        <xsl:text> Text 3</xsl:text>
    </fo:block>
    

    NB: note the empty

    0 讨论(0)
  • 2020-12-03 17:36

    For XSLT 1.0 I'm using my XSLT Line-Break Template on GitHub.

    For XSL-FO it supports

    • Line breaks
    • Line delimiters (vs Line breaks)
    • Series of pointers in a row
    • Ignore Pointer Repetitions (disable the Series of pointers in a row)
    • Any string as a pointer to insert a break or a delimiter ("\n" is default)
    • Line delimiters' height
    • Default Line delimiter height from a current font size.
    • Auto ignoring of the "\r" char when searching a break place.
    • Added support for XSLT 2.0 for a seamless migration.
    • something else...

    For XSLT 2.0 and later consider to use approaches like

    • XSLT 2.0 xsl:analyze-string (RegEx)
    • XPath 2.0 tokenize + XSLT (RegEx)
    • passing sequences as a template parameter (XSLT 2.0)
    • and so on
    0 讨论(0)
  • 2020-12-03 17:40

    Try using linefeed-treatment="preserve" and \n instead of <br> for a new line.

    <fo:block linefeed-treatment="preserve" >
     <xsl:value-of select="Description" />
    </fo:block>
    
    0 讨论(0)
  • 2020-12-03 17:41

    You could also replace <br/> with &#xA; and add a linefeed-treatment="preserve" attribute to your <fo:block>.

    Something like:

    <fo:block linefeed-treatment="preserve">This is an example Description.&#xA;List item 1&#xA;List item 2&#xA;List item 3&#xA;List item 4</fo:block>
    

    Edit

    Some users may need to use \n instead of &#xA; depending on how they are creating the XML. See Retain the &#xA; during xml marshalling for more details.

    0 讨论(0)
提交回复
热议问题