使用FOP将xsl-fo转pdf
来源:互联网 发布:vscode vue 开发环境 编辑:程序博客网 时间:2024/05/21 06:17
第三步骤:将生成的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文件了。
上图为效果图 还是很不错吧。
0 0
- 使用FOP将xsl-fo转pdf
- 使用Apache FOP将XML导出成PDF
- 使用Apache FOP将XML导出成PDF
- xhtml转xsl-fo
- xml与xsl-fo生成PDF
- XSL- FO
- XSL-FO
- XSL-FO
- FOP(PDF与JAVA)
- 使用Apache XML项目FOP生成PDF文件(上)
- 使用Apache XML项目FOP生成PDF文件(下)
- XSL-FO 简介
- XSL-FO 基础知识(三)
- XSL-FO学习笔记
- Definitive XSL-FO
- Xsl Fo [ILLUSTRATED]
- XSL-FO表格(table)
- 利用FOP将xml转换成PDF,有时PDF会破损
- 要把自己遇到过的异常写出来,方便以后查看
- 九个Console命令,让 JS 调试更简单
- Zookeeper安装
- java变量命名规则
- Oracle 生成带 "-" GUID
- 使用FOP将xsl-fo转pdf
- JS子窗口调用父窗口的方法
- cocos2dx在xcode8 GM版下的错误`libpng error:CgBI:unhandled critical chunk`
- List<T>中去除重复的元素
- c++string类详解收集
- 关于推广个人博客的经验_博客推广
- C#几个经常用到的字符串截取
- AIDL实现跨APP通信(双向通信)
- 2016.09.08回顾