ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

FOP支持显示中文的详细配置

FOP支持显示中文的详细配置 问题描述FOP在显示中文内容时会将字符显示成#################,本文将通过一个案例详细介绍如何配置并显示中文。package test; import java.io.*; import javax.xml.transform.*; import javax.xml.transform.sax.SAXResult; import javax.xml.transform.stream.StreamSource; import org.apache.fop.apps.*; import org.apache.xmlgraphics.util.MimeConstants; public class FOPPdfCNGenerator3 { public static void main(String[] args) { try { File xmlfile = new File("input/data.xml"); File xsltfile = new File("input/template.xsl"); File pdfFile = new File("input/output.pdf"); File configFile = new File("input/fop.xconf"); InputStream configStream = new FileInputStream(configFile); FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI(), configStream); FOUserAgent foUserAgent = fopFactory.newFOUserAgent(); OutputStream out = new FileOutputStream(pdfFile); out = new BufferedOutputStream(out); Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(new StreamSource(xsltfile)); Source src = new StreamSource(xmlfile); Result res = new SAXResult(fop.getDefaultHandler()); transformer.transform(src, res); System.out.println("PDF生成成功: " + pdfFile.getAbsolutePath()); out.close(); } catch (Exception e) {
返回列表