美文网首页
JAVA实现BI报表中RTF模版转PDF

JAVA实现BI报表中RTF模版转PDF

作者: flyhigh2kk | 来源:发表于2020-01-09 10:03 被阅读0次

    JAVA实现BI报表中RTF模版转PDF

    功能背景,某监测实验室要求在信息系统中输出固定格式的PDF,让我想到BI Publisher中自定义rtf模版,通过BI的引擎生成PDF的功能。然后,我想到了java是不是可以实现这个功能。

    rtftemplate【官网】

    • rtftemplate是rtf2rtf的引擎,若使用该引擎还需要将rtf转为pdf
    • RTF模版的定义规则与BI Publisher不一致。

    BI Publisher API

    • 该API由oracle随BI Pulisher for desktop的安装提供。
    • 有完整的rtf2pdf功能,且有pdf数字签名等其他一些功能,功能丰富。
    • RTF模版的创建是高度自定义的
    • 参与过BI rtf报表模版设计的人可以设计RTF模版(与 BI Publisher rtf模版设计一致)

    列出我自己做的一个小demo
    EmpDataTemplate.rar
    BI Publisher开发文档

    package com.bip;
    /*
    * java version: 1.8_231
    * lib route: BI_PUSBLISHER_ROOT\BI Publisher Desktop\Template Builder for Word\jlib
    */
    import oracle.xdo.XDOException;
    import oracle.xdo.template.FOProcessor;
    import oracle.xdo.template.RTFProcessor;
    import org.omg.CosNaming.BindingIterator;
    
    import java.io.IOException;
    
    public class Main  {
        public static void main(String[] args) {
            try {
                //rtf模版路径
                String rtfPath = "C:\\EmpDataTemplate\\CS_accntpro.rtf";
                //xsl样式表路径
                String xslPath = "C:\\EmpDataTemplate\\CS_accntpro.xsl";
                //xliff文件路径
                String xliffPath = "C:\\EmpDataTemplate\\CS_accntpro.xliff";
                //pdf文件路径
                String pdfPath = "C:\\EmpDataTemplate\\CS_accntpro.pdf";
                //数据路径
                String xmlPath = "C:\\EmpDataTemplate\\test.xml";
    
                //根据rtf模版生成xsl和xliff
                initXSL(rtfPath,xslPath,xliffPath);
    
                //根据xsl、xliff、xml数据生成pdf
                processPdf(xslPath,xliffPath,xmlPath,pdfPath);
    
                System.exit(0);
            }catch (Exception e){
                e.printStackTrace();
                System.exit(1);
            }
        }
    
        private static void processPdf(String xslPath, String xliffPath, String xmlPath, String pdfPath) throws XDOException {
            //生成pdf
            FOProcessor p1 = new FOProcessor();
            p1.setXLIFF(xliffPath);// set xliff file, which is generated from RTFProcessor
            p1.setData(xmlPath); // set data file
            p1.setTemplate(xslPath); // set xsl file
            p1.setOutput(pdfPath);
            p1.generate();
        }
    
        private static void initXSL(String rtfPath,String xslPath,String xliffPath) throws IOException, XDOException {
            //根据rtf生成xsl、xliff
            RTFProcessor rp = new RTFProcessor(rtfPath);
            rp.setOutput(xslPath);
            rp.setXLIFFOutput(xliffPath);
            rp.process();
        }
    }
    

    遇到的问题

    Q: 在编辑RTF模版时,使用了非英文字符时,根据RTF模版生成的PDF文件中非英文字符(中文、韩文等)显示成“?“
    A: 原因为jre运行时缺少了RTF模版中所能使用的各种字体。将目录BI_PUSBLISHER_ROOT\BI Publisher Desktop\Template Builder for Word\fonts下的字体,拷贝到JRE_ROOT\lib\fonts中。 运行打包好的JAR包,问题解决。注意: 在开发工具中运行,仍不能正确转换

    相关文章

      网友评论

          本文标题:JAVA实现BI报表中RTF模版转PDF

          本文链接:https://www.haomeiwen.com/subject/bbxyactx.html