zoukankan      html  css  js  c++  java
  • 执法文书打印的实现(三)(word→png的实现)

    执法文书打印的实现(word^png的实现)

        写这篇博客,心里很忐忑和沉重。由于知识结构等多方面的原因,redhat上启动openoffice环境浪费了许多时间和精力。一些验证通过的命令、方法等稍微改变就慌手慌脚卡了进度,如果有得选择,我应该不会用openoffice实现。毕竟我的兴趣和预职业方向是jee和前端,做这些工作有点偏了,实在提不起来劲头。

        这部分的代码都是网上抄的,大部分都一样,算是固定格式,没有多少个人发挥的余地。Openoffice实现word to pdf,icepdf实现pdf to png。Icepdf是收费软件,使用有风险,转换出的图片效果确实比较惊艳,也可以自由调整精度。

        Openoffice的安装与服务启动

        下载安装包:http://www.openoffice.org/download/index.html

        在redhat下解压安装文件:tar -zxvf 压缩文件名.tar.gz 解压后的文件只能放在当前目录

        安装依赖项:cd zh-CN/RPMS/ rpm –ivh *.rpm

        安装桌面:cd desktop-integration/     rpm -ivh openoffice4.1.1-redhat-menus-4.1.1-9775.noarch.rpm

        进入安装目录:cd /opt/openoffice4/program/

        启动服务:soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" –nofirststartwizard & (注意:这里有个坑,命令最后的"&"符最好添加,上星期kill 掉soffice.bin进程后一直启动不起来,原因就是没有添加"&",不知道什么原因,希望知道的园友能解惑)

        查看监听是否创建成功:netstat -apn|grep 8100

        卸载:rpm -e `rpm -qa |grep openoffice` `rpm -qa |grep ooobasis`

        具体word→png:Java代码

      

    /**
         * 
         * @param sourceName  doc文件名
         * @param zoom 图片清晰度  
         * @return 
         * @throws Exception
         */
        public  String[] opt2png(String sourceName,float zoom) throws Exception {
            //图片磁盘绝对路径:可以作为方法的返回值
            String imgFilePaths []; 
            
            //每次生成一个随机的pdf,每天凌晨启动一个自动任务清空 pdfFile目录
            File inputFile = new File(wordFilePath+File.separator+sourceName);  
            if (!inputFile.exists()) {  
                //return -1;// 找不到源文件, 则返回-1
                System.out.println("找不到word文件"+wordFilePath+File.separator+sourceName);
                return null;
            }
            //生成随机的pdf文件
            String pdfname=UUID.randomUUID().toString();
            pdfFilePath=pdfFilePath+File.separator+pdfname+".pdf";
            File outputFile = new File(pdfFilePath);  
            if (!outputFile.getParentFile().exists()) {  
                outputFile.getParentFile().mkdirs();  
            }
            
            //启动服务:不是很好用,需要手动启动
            String command =officeHome+ "program"+File.separator+"soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;"";
            Process pro=null;
            try {
                pro = Runtime.getRuntime().exec(command);
            } catch (IOException e) {
                e.printStackTrace();
            }
            //建立连接
            OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
            try {
                connection.connect();
            } catch (Exception e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            } 
            
            DocumentConverter converter = new OpenOfficeDocumentConverter(connection);  
            try {
                converter.convert(inputFile, outputFile);  //这个方法的执行不可调试
            } catch (Exception e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }finally{
                // close the connection  
                connection.disconnect();  
                // 关闭OpenOffice服务的进程  
                pro.destroy(); 
                //生成pdf成功
            }
            
            
            
            
            //icepdf  work
            Document document = null;
            float rotation = 0f;
            document = new Document();
            document.setFile(pdfFilePath);
             //获得文档的页数
            int pageNum=document.getNumberOfPages();
            
            imgFilePaths=new String[pageNum];
            String imgFileName;
            for (int i = 0; i < pageNum; i++) {
                //下面这行的代码执行不可调试
                BufferedImage img = (BufferedImage) document.getPageImage(i,GraphicsRenderingHints.SCREEN, Page.BOUNDARY_CROPBOX, rotation,zoom);
                Iterator iter = ImageIO.getImageWritersBySuffix("png");
                ImageWriter writer = (ImageWriter) iter.next();
                imgFileName=UUID.randomUUID().toString();
                File outFile = new File(pngFilePath+File.separator+imgFileName+".png");
                FileOutputStream out = new FileOutputStream(outFile);
                ImageOutputStream outImage = ImageIO.createImageOutputStream(out);
                writer.setOutput(outImage);
                writer.write(new IIOImage(img, null, null));
                imgFilePaths[i]=pngFilePath+File.separator+imgFileName+".png";
            }
            return imgFilePaths;
        }
  • 相关阅读:
    JS数组定义及详解
    JS中script词法分析
    JS函数 -- 功能,语法,返回值,匿名函数,自调用匿名函数,全局变量与局部变量,arguments的使用
    Java面试(1)-- Java逻辑运算符
    Java面试(3)-- Java关系运算符
    让 history 命令显示日期和时间
    mysql 权限管理
    docker基础
    docker 后台运行和进入后台运行的容器
    expect 自动输入密码
  • 原文地址:https://www.cnblogs.com/chonghua/p/4260520.html
Copyright © 2011-2022 走看看