1.首先至Apache FOP下載fop-0.20.5-bin.zip。

連至Apache FOP首頁, 由左側選單進入Download頁面後, 點擊Binary Download段落中FOP Distribution mirror連結。
於眾多連結中, 點選自己想要的下載點, 下載fop-0.20.5-bin.zip即可。



2.依個人使用習慣, 建立對應相關設定(以下說明以Jbuilder為主)。
將fop-0.20.5-bin.zip解至對應的實體位置, 並於Jbuilder中將相關lib載入。

Windows下實體位置(自定)


JBuilder設定


3.於自定的目錄建立如下內容的fo檔
(關於fo檔的產生方式由於網路上參考文件頗多, 在此先不敘述), 在範例中將fo檔置於D:\programming\FOP_exercise\fo\。
simple.fo檔內容:

<?xml version="1.0" encoding="utf-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
  <fo:layout-master-set>
    <fo:simple-page-master master-name="simple"
                  page-height="29.7cm" 
                  page-width="21cm"
                  margin-top="1cm" 
                  margin-bottom="2cm" 
                  margin-left="2.5cm" 
                  margin-right="2.5cm">
      <fo:region-body margin-top="3cm"/>
      <fo:region-before extent="3cm"/>
      <fo:region-after extent="1.5cm"/>
    </fo:simple-page-master>
  </fo:layout-master-set>
  <fo:page-sequence master-reference="simple">
    <fo:flow flow-name="xsl-region-body">
      <fo:block font-size="18pt" 
            font-family="sans-serif" 
            line-height="24pt"
            space-after.optimum="15pt"
            background-color="blue"
            color="white"
            text-align="center"
            padding-top="3pt">
         
            FOP 0.20.2
      
      </fo:block>
      <!-- Normal Text -->
      <fo:block font-size="12pt" 
                font-family="sans-serif" 
                line-height="15pt"
                space-after.optimum="3pt"
                text-align="justify">
                      
FOP is the world's first print formatter driven by XSL formatting objects.
It is a Java application that reads a formatting object tree and then turns it into a PDF document.
      
      </fo:block>
    </fo:flow>
  </fo:page-sequence>
</fo:root>

其中<fo:block></fo:block&gt範圍中的內容為欲輸出的主文。


4.於JBuilder下建立一個Class以產生PDF檔, Class檔內容如下:
foptest.class

import java.io.*;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.apache.fop.apps.*;

public class foptest {
  public static void main(String[] args) {
      try {
          Driver driver = new Driver();
          //設定要作為輸出參考的fo檔所在及檔名
          driver.setInputSource(new InputSource ("D:\\programming\\FOP_exercise\\fo\\simple.fo"));
          //設定要作為輸出檔案(在此為PDF檔)至何處及其檔名
          driver.setOutputStream(new FileOutputStream("D:\\programming\\FOP_exercise\\fo\\simple.pdf"));
          //設置轉換類型
          //還可以為RENDER_PCL,RENDER_PS,RENDER_TXT,RENDER_MIF
          driver.setRenderer(Driver.RENDER_PDF);
          //開始轉換
          driver.run();
      }
      catch( Exception e ){
          e.printStackTrace();
      }
  }
}

建立org.apache.fop.apps.Driver物件,以Driver物件中setInputSource()設定要作為輸出參考的fo檔所在及檔名, setOutputStream()設定要作為輸出檔案至何處及其檔名, 並以setRenderer()設定輸出類型, 以run()執行輸出。


5.執行後產生的simple.pdf檔效果如下




資料參考來源:
如何在程式中嵌入FOP
Apache FOP

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 sanji107 的頭像
    sanji107

    點點滴滴的隨意

    sanji107 發表在 痞客邦 留言(0) 人氣()