2016-11-05 26 views
0

我從下面的這個xsl文檔生成PDF。我想添加一個背景圖片來覆蓋整個生成的pdf,但是當我在第12行包含<fo: block-container>時,它會繼續破壞pdf的生成。是否有另一種方法可以生成背景圖像或者是否存在我在這裏做的不正確的事情?XSL-FO背景圖像腐蝕生成pdf

我也試圖使用<fo:external-graphic>包括背景圖片,它沒有顯示圖像,但在頂部留有邊框(我認爲這是區域之前)。但是,這隻適用於託管的圖像url,而不是本地存儲的圖像,這不是問題,但我發現有點奇怪。

<fo:block text-align="center" display-align="before"> <fo:external-graphic top="-20mm" bottom="-20mm" content-width="210mm" height="150mm" src="url()"> </fo:external-graphic> </fo:block>

`

<xsl:template match="/"> 
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> 
     <fo:layout-master-set> 
      <fo:simple-page-master master-name="page" page-height="150mm" page-width="210mm" margin-top="10mm" margin-bottom="10mm" margin-left="12mm" margin-right="12mm"> 
       <fo:region-body margin-top="20mm" margin-bottom="20mm" /> 
       <!-- <fo:region-before extent="20mm" /> --> 
       <!-- <fo:region-after extent="20mm" /> --> 
      </fo:simple-page-master> 
     </fo:layout-master-set> 
     <fo:page-sequence master-reference="page"> 
      <fo:flow flow-name='xsl-region-body' font-size="20pt"> 
       <fo:block-container absolute-position="absolute" top="-20mm" bottom="-20mm" width="210mm" height="150mm" background-image="/Users/myname/Downloads/background.png"> 
        <fo:block/> 
       </fo:block-container>     
       <xsl:apply-templates select="certificate" /> 
      </fo:flow> 
     </fo:page-sequence> 
    </fo:root> 
</xsl:template> 

<xsl:template match="certificate"> 
    <fo:block text-align="center"> 
     <xsl:variable name="smallcase" select="'abcdefghijklmnopqrstuvwxyz'" /> 
     <xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" /> 
     <xsl:variable name="bgImg" select="/images/background.png"/> 



     <fo:block font-size="30pt" text-align="center" color="#000000" font-family=" Arial, Helvetica, sans-serif"> 
      <xsl:value-of select="first_title" /> 
     </fo:block> 
     <fo:block font-size="14pt" padding-before="3mm" font-weight="lighter" text-align="center" color="#000000" font-family=" Arial, Helvetica, sans-serif"> 
      <xsl:value-of select="student_name" /> 
     </fo:block> 
     <fo:block font-size="14pt" font-weight="lighter" text-align="center" color="#000000" font-family=" Arial, Helvetica, sans-serif"> 
      <xsl:value-of select="school_name" /> 
     </fo:block> 
     <fo:block font-size="20pt" padding-before="3mm" padding-after="5mm" text-align="center" color="#000000" font-family=" Arial, Helvetica, sans-serif"> 
      <xsl:value-of select="second_title" /> 
     </fo:block> 
     <fo:block font-size="14pt" font-weight="lighter" text-align="center" color="#000000" font-family=" Arial, Helvetica, sans-serif"> 
      <xsl:value-of select="level_passed" /> 
     </fo:block> 
     <fo:block font-size="14pt" font-weight="lighter" text-align="center" color="#000000" font-family=" Arial, Helvetica, sans-serif"> 
      <xsl:value-of select="third_title" /> 
     </fo:block> 
     <fo:block font-size="14pt" padding-before="8mm" font-weight="lighter" text-align="center" color="#000000" font-family=" Arial, Helvetica, sans-serif"> 
      <xsl:value-of select="fourth_title" /> 
     </fo:block> 
     <fo:block font-size="14pt" font-weight="lighter" text-align="center" color="#000000" font-family=" Arial, Helvetica, sans-serif"> 
      <xsl:value-of select="teacher_name" /> 
     </fo:block> 
     <fo:block font-size="14pt" font-weight="lighter" text-align="center" color="#000000" font-family=" Arial, Helvetica, sans-serif"> 
      <xsl:value-of select="date" /> 
     </fo:block> 
    </fo:block> 
</xsl:template> 

<xsl:template match="student_name"> 
    <fo:block space-before="10mm" space-before.conditionality="retain"> 
     <fo:inline> 
      <xsl:value-of select="student_name" /> 
      <xsl:text>of</xsl:text> 
     </fo:inline> 
    </fo:block> 
</xsl:template> 

<xsl:template match="school_name"> 
    <fo:block space-after="10mm" space-before="5pt" space-after.conditionality="retain"> 
     <fo:inline> 
      <xsl:value-of select="school_name" /> 
     </fo:inline> 
    </fo:block> 
</xsl:template> 

<xsl:template match="level_passed"> 
    <fo:block space-before="10mm" space-before.conditionality="retain"> 
     <fo:inline> 
      <xsl:value-of select="level_passed" /> 
     </fo:inline> 
    </fo:block> 
</xsl:template> 

<xsl:template match="teacher_name"> 
    <fo:block space-before="5pt"> 
     <fo:inline> 
      <xsl:value-of select="teacher_name" /> 
     </fo:inline> 
    </fo:block> 
</xsl:template> 

<xsl:template match="date"> 
    <fo:block space-before="5pt"> 
     <fo:inline> 
      <xsl:value-of select="date" /> 
     </fo:inline> 
    </fo:block> 
</xsl:template> 

`

+0

FO:區域-體可以具有@背景圖片屬性。你已經嘗試過了嗎? – tmakita

+0

@tmakita我做了,仍然導致PDF在生成時被破壞。 –

回答

0

這是通過添加<fo:block-container>左右,我拉着在學生的名字和學校名稱等內容解決以下完整代碼

`

<xsl:template match="/"> 
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> 
     <fo:layout-master-set> 
      <fo:simple-page-master master-name="page" page-height="211mm" page-width="338mm" margin-top="2cm" margin-bottom="2cm" margin-left="3cm" margin-right="3cm"> 
           <fo:region-body/> 
      </fo:simple-page-master> 
     </fo:layout-master-set> 
     <fo:page-sequence master-reference="page"> 
      <fo:flow flow-name='xsl-region-body' font-size="20pt" letter-spacing="0.05pt"> 
       <xsl:apply-templates select="certificate" /> 
      </fo:flow> 
      </fo:page-sequence> 
    </fo:root> 
</xsl:template> 

<xsl:template match="certificate"> 


    <xsl:variable name="imageHref"><xsl:value-of select="background"/></xsl:variable> 

     <fo:block-container absolute-position="absolute" 
     top="-2cm" left="-3cm" width="338mm" height="211mm" 
     background-image="{$imageHref}"> 

     <xsl:variable name="smallcase" select="'abcdefghijklmnopqrstuvwxyz'" /> 
     <xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" /> 
     <xsl:variable name="bgImg" select="/images/background.png"/> 

     <fo:block font-size="45pt" text-align="center" color="#000000" padding-top="4cm" font-family=" Arial, Helvetica, sans-serif"> 
      This <fo:inline font-family="serif" font-size="50pt" font-weight="bold" font-style="italic"><xsl:value-of select="first_title" /></fo:inline> honors 
     </fo:block> 
     <fo:block font-size="20pt" padding-top="5mm" font-weight="lighter" text-align="center" color="#000000" font-family=" Arial, Helvetica, sans-serif"> 
      <xsl:value-of select="student-name" /> 
     </fo:block> 
     <fo:block font-size="20pt" font-weight="lighter" text-align="center" color="#000000" font-family=" Arial, Helvetica, sans-serif"> 
      <xsl:value-of select="school-name" /> 
     </fo:block> 
     <fo:block padding-top="1cm" font-size="35pt" padding-before="3mm" padding-after="5mm" text-align="center" color="#000000" font-family=" Arial, Helvetica, sans-serif"> 
      for <fo:inline font-family="serif" font-size="40pt" font-weight="bold" font-style="italic"><xsl:value-of select="second_title" /></fo:inline> the 
     </fo:block> 
     <fo:block font-size="20pt" font-weight="lighter" text-align="center" color="#000000" font-family=" Arial, Helvetica, sans-serif"> 
      <xsl:value-of select="dropdown" /> 
     </fo:block> 
     <fo:block font-size="20pt" font-weight="lighter" text-align="center" color="#000000" font-family=" Arial, Helvetica, sans-serif"> 
      <xsl:value-of select="additional-info" /> 
     </fo:block> 
     <fo:block padding-top="1.5cm" font-size="20pt" padding-bottom="5mm" font-weight="lighter" text-align="center" color="#000000" font-family=" Arial, Helvetica, sans-serif"> 
      <xsl:value-of select="fourth_title" /> 
     </fo:block> 
     <fo:block font-size="20pt" font-weight="lighter" text-align="center" color="#000000" font-family=" Arial, Helvetica, sans-serif"> 
      <xsl:value-of select="teacher-name" /> 
     </fo:block> 
     <fo:block padding-bottom="1cm" font-size="20pt" font-weight="lighter" text-align="center" color="#000000" font-family=" Arial, Helvetica, sans-serif"> 
      <xsl:value-of select="date" /> 
     </fo:block> 
    </fo:block-container> 
</xsl:template> 

<xsl:template match="student-name"> 
    <fo:block space-before="10mm" space-before.conditionality="retain"> 
     <fo:inline> 
      <xsl:value-of select="student_name" /> 
      <xsl:text>of</xsl:text> 
     </fo:inline> 
    </fo:block> 
</xsl:template> 

<xsl:template match="school-name"> 
    <fo:block space-before="5pt" space-after.conditionality="retain"> 
     <fo:inline> 
      <xsl:value-of select="school_name" /> 
     </fo:inline> 
    </fo:block> 
</xsl:template> 

<xsl:template match="dropdown"> 
    <fo:block space-before="10mm" space-before.conditionality="retain"> 
     <fo:inline> 
      <xsl:value-of select="level_passed" /> 
     </fo:inline> 
    </fo:block> 
</xsl:template> 

<xsl:template match="teacher-name"> 
    <fo:block space-before="5pt"> 
     <fo:inline> 
      <xsl:value-of select="teacher_name" /> 
     </fo:inline> 
    </fo:block> 
</xsl:template> 

<xsl:template match="date"> 
    <fo:block space-before="5pt"> 
     <fo:inline> 
      <xsl:value-of select="date" /> 
     </fo:inline> 
    </fo:block> 
</xsl:template> 

'