Ofbiz 用FOP生成PDF文件中文无法显示的解决方案

在开发过程中,发现生成的PDF文件中,所有中文都不能显示,以”#“表示。

在ofbiz中,生成pdf的过程是:

1、访问xxx.pdf的url.

2、controller.xml中对*.pdf配置如下:

<handler name="screenfop" type="view" class="org.ofbiz.widget.screen.ScreenFopViewHandler"/>

...

<view-map name="PicklistReport.pdf" type="screenfop" page="component://warehouse/widget/warehouse/screens/shipping/ShippingScreens.xml#PicklistReportPDF" content-type="application/pdf" encoding="none"/>

3、在ScreenFopViewHandler.java中,先把PicklistReport.fo.ftl渲染成xml

        // render and obtain the XSL-FO
        Writer writer = new StringWriter();
        FopFactory fopFactory = ApacheFopFactory.instance();
            ScreenRenderer screens = new ScreenRenderer(writer, null, htmlScreenRenderer);
            screens.populateContextForRequest(request, response, servletContext);
            // this is the object used to render forms from their definitions
            screens.getContext().put("formStringRenderer", new FoFormRenderer(request, response));
            screens.render(page);

  然后由apache的FOP(Formatting Objects Processor)对xml渲染,生成pdf文件,并放回到response中输出:

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        TransformerFactory transFactory = TransformerFactory.newInstance();
        try {
            Fop fop = fopFactory.newFop(contentType, out);
            Transformer transformer = transFactory.newTransformer();
            // set the input source (XSL-FO) and generate the output stream of contentType
            Reader reader = new StringReader(writer.toString());
            Source src = new StreamSource(reader);
            // Get handler that is used in the generation process
            Result res = new SAXResult(fop.getDefaultHandler());
                // Transform the FOP XML source
                transformer.transform(src, res);
                // We don't want to cache the images that get loaded by the FOP engine
                fopFactory.getImageFactory().clearCaches();
                // set the content type and length
                response.setContentType(contentType);
                response.setContentLength(out.size());
                // write to the browser
                    out.writeTo(response.getOutputStream());
                    response.getOutputStream().flush();

又一次看到中文被鄙视了,Apache FOP中不支持任务中文字体,所以要做下边三步,才能使pdf中的中文正常显示:

1、用FOP生成黑体字体配置文件simhei.xml,并把windows下的黑体文件simhei.ttf放在相同的目录下。在openb2c 中为:

%OPENB2C_HOME%\hot-deploy\opentaps-common\lib\DejaVu\

生成simhei.xml的命令为:

java -classpath framework\webapp\lib\fop-0.93.jar;framework\base\lib\avalon-framework-4.2.0.jar;framework\webapp\lib\batik-all-1.6.jar;framework\base\lib\commons\commons-logging.jar;framework\base\lib\commons\commons-io-1.4.jar;framework\base\lib\serializer.jar;framework\base\lib\xalan.jar;framework\base\lib\xml-apis-2.8.1.jar;framework\webapp\lib\xmlgraphics-commons-1.1.jar org.apache.fop.fonts.apps.TTFReader -ttcname "SimHei" C:\WINDOWS\Fonts\simhei.ttf D:\simhei.xml

2、修改ofbiz中fop的配置文件fop.xconf

添加字体:simhei

       <font metrics-url="simhei.xml" kerning="yes" embed-url="simhei.ttf">
            <font-triplet name="simhei" style="normal" weight="normal"/>
        </font>

3、修改*.fo.ftl模板文件,把font-family改为simhei

<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"
    font-family="simhei">

重启看到pdf能正常显示中文了!

 

为我们公司做下广告易合信息技术yiihee.com

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值