package com.yux;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import javax.xml.parsers.SAXParserFactory;
import org.apache.fop.apps.Driver;
import org.apache.fop.apps.Options;
import org.apache.fop.configuration.ConfigurationParser;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
public class Test {
public Options options = null;
public static void main(String[] args) throws Exception {
new Test().test("test11.pdf");
// new Test().test("test12.pdf");
// new Test().test("test13.pdf");
}
public void test(String fileName) throws Exception {
//java -cp build\fop.jar;lib\avalon-framework.jar;lib\xml-apis.jar;lib\xercesImpl.jar;lib\xalan.jar org.apache.fop.fonts.apps.TTFReader D:\fonts\msjh.ttf msjh.xml
if(options==null) {
InputStream in = new FileInputStream(new File("D:/yux/fop-0.20.5/conf/userconfig.xml"));
//
options=new Options(new InputSource(in));
in.close();
in =null;
System.out.println("init..................................................................");
}
Driver driver = new Driver();
String folder ="D:/yux/fop-0.20.5/test/";
FileOutputStream out=new FileOutputStream(folder+fileName);
driver.setInputSource(new InputSource (folder+"text.xsl"));
driver.setOutputStream(out);
driver.setRenderer(Driver.RENDER_PDF);
//读入配置(在Options的构造函数中完成)
driver.run();
out.close();
driver.reset();
options =null;
// Thread.sleep(300000);
System.out.println("end");
}
}
userconfig.xml
<font metrics-file="D:\workspace\fopdemo\conf\fonts\msjhbd.xml" embed-file="D:\workspace\fopdemo\conf\fonts\msjhbd.ttf" kerning="yes">
<font-triplet name="MicrosoftJhengHei" style="normal" weight="bold"/>
<font-triplet name="MicrosoftJhengHei" style="italic" weight="bold"/>
</font>
<font metrics-file="D:\workspace\fopdemo\conf\fonts\msjh.xml" embed-file="D:\workspace\fopdemo\conf\fonts\msjh.ttf" kerning="yes">
<font-triplet name="MicrosoftJhengHei" style="normal" weight="normal"/>
<font-triplet name="MicrosoftJhengHei" style="italic" weight="normal"/>
</font>
XSL
<?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-family="MicrosoftJhengHei">
测试123
</fo:block>
<fo:block font-family="MicrosoftJhengHei" font-weight="bold">
测试123455
</fo:block>
<fo:block>
ABC
</fo:block>
<fo:block font-family="SimSun1">
teste测试
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>