Apache Fop可以把xsl-fo格式、xml、xslt转换pdf文件。其中关键点就是熟悉xsl-fo语法和xslt
http://mirrors.hust.edu.cn/apache/xmlgraphics/fop/binaries/fop-2.1-bin.zip
import org.apache.fop.apps.FopFactory; import org.apache.fop.apps.Fop; import org.apache.fop.apps.MimeConstants;
public static void convertFO2PDF()throws IOException, SAXException, ConfigurationException{
//读取字体配置文件,配置中文
FopFactory fopFactory =Fop Factory.newInstance(new File("C:/Temp/bar.conf.xml"));//输出pdf文件路径 OutputStream out = new BufferedOutputStream(new FileOutputStream(new File(“C:/Temp/myfile.pdf”)));
try { // Step 3: Construct fop with desired output format Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
// Step 4: Setup JAXP using identity transformer TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); // identity transformer // Step 5: Setup input and output for XSLT transformation // Setup input stream Source src = new StreamSource(new File("C:/Temp/myfile.fo")); // Resulting SAX events (the generated FO) must be piped through to FOP Result res = new SAXResult(fop.getDefaultHandler()); // Step 6: Start XSLT transformation and FOP processing transformer.transform(src, res);} finally { //Clean-up out.close(); }
String[] parameters = { “-ttcname”, “NotoSansCJK-Black”, “xxx.ttf”, xxx.xml文件} TTFReader.main(parameters);
方正宋三简体.xml文件是我用工具生成xml 然后可以在fop中调用Barcode字体,
文件我都是用的绝对路径,官网可以用相对路径,可是我没试成功 ,希望看到这篇文章的大神,可以帮我解决下这个问题 ,其中我用的Freemarker+FOP 生成自定义pdf的内容
xsl-fo详细教程:https://www.antennahouse.com/XSLsample/FOsample.htm