将外部TTF字体文件添加到XSL-FO样式表

时间:2017-04-28 10:53:53

标签: java xslt xsl-fo

我是XSL-Fo的新手。我正在制作一个场景,我必须在生成pdf时包含一些中文字体。我已经下载了一个带有中文字体的TTF文件。但我无法将其应用于text.I不知道如何将ttf文件字体应用到XSL中。我的代码如下:

XSL_Code

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.1"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:fo="http://www.w3.org/1999/XSL/Format"
    xmlns:fox="http://xmlgraphics.apache.org/fop/extensions"
    exclude-result-prefixes="fo">
<xsl:attribute-set name="fontStyles">
    <xsl:attribute name="embed-url">URL('FourthTaskInput/GoJuOn.TTF') format('ttf')</xsl:attribute>
    <xsl:attribute name="background-color">#BB5588</xsl:attribute>
    <xsl:attribute name="border-bottom">solid 2pt #409C94</xsl:attribute>
    <xsl:attribute name="border-top">solid 2pt #409C94</xsl:attribute>
    <xsl:attribute name="font-size">9pt</xsl:attribute>
</xsl:attribute-set>
<xsl:template match="student">
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
  <fo:layout-master-set>
    <fo:simple-page-master master-name="student-details">
      <fo:region-body margin="1in"/>
      <fo:region-before region-name="xsl-region-before" extent="5in"/>
      <fo:region-after region-name="xsl-region-after" extent=".5in"/>
    </fo:simple-page-master>
  </fo:layout-master-set>
  <fox:external-document content-type="pdf" src="FourthTaskInput//CoverPage.pdf"/> 
  <fo:page-sequence master-reference="student-details">
       <fo:static-content flow-name="xsl-region-before">
            <fo:block text-align="center"  margin-top="10mm"  margin-bottom="10mm">Header</fo:block>
        </fo:static-content>
        <fo:static-content flow-name="xsl-region-after">
            <fo:block text-align="center">Page No <fo:page-number/></fo:block>
        </fo:static-content>
    <fo:flow flow-name="xsl-region-body">
      <fo:block>
       <xsl:for-each select=".">
            <fo:table break-after="page" xsl:use-attribute-sets="fontStyles">
                <fo:table-body>
                <fo:table-row>
                    <fo:table-cell border="solid black"text-align="center" font-weight="bold" width="1cm">
                        <fo:block >
                            Id.
                        </fo:block>
                    </fo:table-cell>
                    <fo:table-cell border="solid black"text-align="center" font-weight="bold" width="3cm">
                        <fo:block font-family="Tahoma">
                            Name
                        </fo:block>
                    </fo:table-cell>
                    <fo:table-cell border="solid black" text-align="center" font-weight="bold" width="1.5cm" >
                        <fo:block>
                            Grade
                        </fo:block>
                    </fo:table-cell>
                        <fo:table-cell border="solid black" text-align="center" font-weight="bold"  width="2.5cm">
                        <fo:block>
                            DOB
                        </fo:block>
                    </fo:table-cell>
                </fo:table-row>
             </fo:table-body>
        </fo:table>
        </xsl:for-each>
      </fo:block>
    </fo:flow>
  </fo:page-sequence>
</fo:root>
</xsl:template>
</xsl:stylesheet>

我不确定我提到 URL('FourthTaskInput / GoJuOn.TTF')格式('ttf')的部分是否是将ttf文件导入XLS的正确方法-FO.Even如果是这样我不知道怎么用它来打印字体作为pdf中的中文字体。任何人都可以帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

FWIW,我之前从未见过@embed-url属性。

由于您似乎正在使用FOP,请参阅https://xmlgraphics.apache.org/fop/2.2/fonts.html#custom以获取有关使用自定义字体的FOP文档。