使用FOP将xsl-fo转pdf

第三步骤:将生成的fo文件转成pdf。【由于.xsl文件不能识别中文,我在root路径下,给root添加了一个属性,font-family,让其可以识别中文。其余需要修改的属性或者样式,需要你自己修改该.xsl文件】
font-family=“???” ,???的值就是你设置的字体名称。
可以放在fop.xml配置文件中
fop.xml的配置文件内容如下:

<?xml version="1.0"?>
<!-- $Id: fop.xml 901793 2012-12-21 bin.yin $ -->
<!-- NOTE: This is the version of the configuration -->
<fop version="1.0">

  <!-- Base URL for resolving relative URLs -->
  <base>.</base>

  <!-- Source resolution in dpi (dots/pixels per inch) for determining the size of pixels in SVG and bitmap images, default: 72dpi -->
  <source-resolution>72</source-resolution>
  <!-- Target resolution in dpi (dots/pixels per inch) for specifying the target resolution for generated bitmaps, default: 72dpi -->
  <target-resolution>72</target-resolution>

  <!-- Default page-height and page-width, in case value is specified as auto -->
  <default-page-settings height="29.7cm" width="40cm"/>

  <!-- Information for specific renderers -->
  <!-- Uses renderer mime type for renderers -->
  <renderers>
    <renderer mime="application/pdf">
      <filterList>
        <!-- provides compression using zlib flate (default is on) -->
        <value>flate</value>
      </filterList>


      <fonts>
        <!-- embedded fonts -->
        <!--
        This information must exactly match the font specified
        in the fo file. Otherwise it will use a default font.

        For example,
        <fo:inline font-family="Arial" font-weight="bold" font-style="normal">
            Arial-normal-normal font
        </fo:inline>
        for the font triplet specified by:
        <font-triplet name="Arial" style="normal" weight="bold"/>

        If you do not want to embed the font in the pdf document
        then do not include the "embed-url" attribute.
        The font will be needed where the document is viewed
        for it to be displayed properly.

        possible styles: normal | italic | oblique | backslant
        possible weights: normal | bold | 100 | 200 | 300 | 400
                          | 500 | 600 | 700 | 800 | 900
        (normal = 400, bold = 700)
        -->

        <font metrics-url="conf/fonts/arial.xml" kerning="yes" embed-url="conf/fonts/arial.ttf">
          <font-triplet name="Arial" style="normal" weight="normal"/>
          <font-triplet name="Arial" style="italic" weight="normal"/>
        </font>
        <font metrics-url="conf/fonts/arialb.xml" kerning="yes" embed-url="conf/fonts/arialb.ttf">
          <font-triplet name="Arial" style="normal" weight="bold"/>
          <font-triplet name="Arial" style="italic" weight="bold"/>
        </font>
        <font metrics-url="conf/fonts/SimHei.xml" kerning="yes" embed-url="conf/fonts/SimHei.ttf">
          <font-triplet name="SimHei" style="normal" weight="normal"/>
          <font-triplet name="SimHei" style="normal" weight="bold"/>
          <font-triplet name="SimHei" style="italic" weight="normal"/>
          <font-triplet name="SimHei" style="italic" weight="bold"/>
        </font>
        <font metrics-url="conf/fonts/SimSun.xml" kerning="yes" embed-url="conf/fonts/SimSun.ttc">
          <font-triplet name="SimSun" style="normal" weight="normal" />
          <font-triplet name="SimSun" style="normal" weight="bold" />
          <font-triplet name="SimSun" style="italic" weight="normal" />
          <font-triplet name="SimSun" style="italic" weight="bold" />
        </font>
        <!--新宋体//-->
        <font metrics-url="conf/fonts/NSimSun.xml" kerning="yes" embed-url="conf/fonts/SimSun.ttc">
          <font-triplet name="NSimSun" style="normal" weight="normal" />
          <font-triplet name="NSimSun" style="normal" weight="bold" />
          <font-triplet name="NSimSun" style="italic" weight="normal" />
          <font-triplet name="NSimSun" style="italic" weight="bold" />
        </font>
        <font metrics-url="conf/fonts/Code39Seven.xml" kerning="yes" embed-url="conf/fonts/Code39Seven.ttf">
          <font-triplet name="Code39Seven" style="normal" weight="normal" />
        </font>
      </fonts>

      <!-- This option lets you specify additional options on an XML handler -->
      <!--xml-handler namespace="http://www.w3.org/2000/svg">
        <stroke-text>false</stroke-text>
      </xml-handler-->

    </renderer>

    <renderer mime="application/postscript">
      <!-- This option forces the PS renderer to rotate landscape pages -->
      <!--auto-rotate-landscape>true</auto-rotate-landscape-->

      <!-- This option lets you specify additional options on an XML handler -->
      <!--xml-handler namespace="http://www.w3.org/2000/svg">
        <stroke-text>false</stroke-text>
      </xml-handler-->
    </renderer>

    <renderer mime="image/png">
      <!--transparent-page-background>true</transparent-page-background-->
    </renderer>

    <renderer mime="image/tiff">
      <!--transparent-page-background>true</transparent-page-background-->
      <!--compression>CCITT T.6</compression-->
    </renderer>

    <renderer mime="text/xml">
    </renderer>

    <!-- RTF does not have a renderer
    <renderer mime="text/rtf">
    </renderer>
    -->

  </renderers>

</fop>

配置文件中的各种字体需要我们自己生成,可参考:
https://www.ibm.com/developerworks/cn/xml/x-fop/
这篇文章有点老,但是字体部分我们可以借鉴。

主体代码很简单:

public void convertFO2PDF(File fo, File pdf) throws Exception {
        // Construct driver
        FopFactory factory = FopFactory.newInstance();
        factory.setUserConfig("conf/fop.xml");
        OutputStream out = null;
        out = new FileOutputStream(pdf);
        out = new BufferedOutputStream(out);
        Fop fop = factory.newFop(MimeConstants.MIME_PDF, out);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        Source source = new StreamSource(fo);
        Result res = new SAXResult(fop.getDefaultHandler());
        transformer.transform(source, res);
        out.close();
    }

调用该方法就行。最终就可以得到你想要的pdf文件了。

这里写图片描述
上图为效果图 还是很不错吧。

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值