zoukankan      html  css  js  c++  java
  • openOffice预览图片

    1.安装oppenoffice
      下载地址:https://www.openoffice.org/download/

      将下载好的解压包,解压:  
      windows :tar -zxvf Apache_OpenOffice_4.1.3_Linux_x86-64_install-rpm_zh-CN.tar.gz 直接将安装包放到要安装的目录直接运行
        解压完 找到zh-cn文件夹 找到RPMS文件夹
        安装rm:yum localinstall *.rpm
        然后找到desktop-integration文件夹 在下面运行:rpm -ivh openoffice4.1.3-redhat-menus-4.1.3-9783.noarch.rpm
        安装过程如果有选择项 都选Y
        安装完成后在openoffice 4/program目录下运行:soffice -headless -accept="socket,host=127.0.0.1,port=8100:urp;" -nofirststartwizard
        期间不要关闭DOS窗口 安装完后检测是否正常启动
           netstat -ano|findstr 8100

        
        启动成功!

        Linux查看是否启动 netstat -lnp |grep 8100

         启动: sudo soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &



    2.下载jar包

      在https://mvnrepository.com/ 下载jodconverter-2.2.2
    下载链接:https://mvnrepository.com/search?q=jodconverter word转pdf的jar包


    https://mvnrepository.com/search?q=itextpdf 图片转换pdf的jar包


    package com.thinkgem.jeesite.modules.sys.utils;
    
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    
    import com.artofsolving.jodconverter.DocumentConverter;
    import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
    import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
    import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
    //word转pdf工具类
    public class DocConverterPdf {private static final int environment = 1;
            private String fileString;
            private String outputPath = "";
            private String fileName;
            private File pdfFile;
            private File swfFile;
            private File docFile;
            private File odtFile;
            private String outFileString;
            private String outFileName;
            
            
    
            public DocConverterPdf(String fileString,String outFileString) {
                ini(fileString,outFileString);
            }
    
            public void setFile(String fileString,String outFileString) {
                ini(fileString,outFileString);
            }
    
            private void ini(String fileString,String outFileString) {    
                 try {    
                     this.fileString = fileString;    
                     this.outFileString = outFileString;    
                     System.out.println("***********fileString:"+fileString);
                     fileName = fileString.substring(0, fileString.lastIndexOf("/"));    
                     outFileName = outFileString.substring(0, outFileString.lastIndexOf("/"));   
                     docFile = new File(fileString);    
                     String s = fileString.substring(fileString.lastIndexOf("/") + 1,fileString.lastIndexOf("."));    
                     String o = outFileString.substring(outFileString.lastIndexOf("/") + 1,outFileString.lastIndexOf("."));    
                     fileName = fileName + "/" + s;   
                     outFileName = outFileName+ "/" + o;   
                   
                     String txtName = fileString.substring(fileString.lastIndexOf("."));                    
                     if (txtName.equalsIgnoreCase(".txt")) {    
                         
                         odtFile = new File(fileName + ".odt");    
                         this.copyFile(docFile, odtFile);    
                         pdfFile = new File(outFileName + ".pdf"); 
                     } else if (txtName.equals(".pdf") || txtName.equals(".PDF")) {    
                         pdfFile = new File(outFileName + ".pdf");    
                         this.copyFile(docFile, pdfFile);    
                         File file =new File(docFile.getPath());
                         file.delete();
                     } else {    
                         pdfFile = new File(outFileName + ".pdf");    
                     }    
                   //  swfFile = new File(fileName + ".swf");    
                 } catch (Exception e) {    
                     e.printStackTrace();    
                 }    
             }    
    
            /**
             * @Title: copyFile
             * @Description: TODO
             * @param: @param docFile2
             * @param: @param odtFile2
             * @return: void
             * @author: hl
             * @time: 2014-4-17 下午9:41:52
             * @throws
             */
            private void copyFile(File sourceFile,File targetFile)throws Exception{
                FileInputStream input = new FileInputStream(sourceFile);
                BufferedInputStream inBuff = new BufferedInputStream(input);      
                FileOutputStream output = new FileOutputStream(targetFile);
                BufferedOutputStream outBuff  = new BufferedOutputStream(output);
                byte[]b = new byte[1024 * 5];
                int len;
                while((len = inBuff.read(b)) != -1){
                    outBuff.write(b,0,len);
                }
                // 刷新此缓冲的输出流
                outBuff.flush();
                // 关闭流
                inBuff.close();
                outBuff.close();
                output.close();
                input.close();
            }
            
            
    
    
            private String  doc2pdf(String curstyle) throws Exception {
                String curUrl = "";
                if (docFile.exists()) {
                    if (!pdfFile.exists()) {
                        OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
                        try {
                            connection.connect();
                            DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
                            if("txt".equals(curstyle)){
                             converter.convert(odtFile, pdfFile);
                            }else{
                             converter.convert(docFile, pdfFile);    
                            }
                            // close the connection
                            connection.disconnect();
                            curUrl=pdfFile.getPath();
                             System.out.println("****pdf转换成功,PDF输出:" + pdfFile.getPath() + "****");  
                        } catch (java.net.ConnectException e) {
                            // ToDo Auto-generated catch block
                            e.printStackTrace();
                            System.out.println("****swf转换异常,openoffice服务未启动!****");  
                            throw e;
                        } catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) {
                            e.printStackTrace();
                             System.out.println("****swf转换器异常,读取转换文件失败****");  
                            throw e;
                        } catch (Exception e) {
                            e.printStackTrace();
                            throw e;
                        }
                    } else {
                        curUrl=pdfFile.getPath();
                        System.out.println("****已经转换为pdf,不需要再进行转化****");  
                    }
                } else {
                     System.out.println("****swf转换器异常,需要转换的文档不存在,无法转换****");  
                }
                return curUrl;
            }
    static String loadStream(InputStream in) throws IOException {
                int ptr = 0;       
                BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                StringBuilder buffer = new StringBuilder();
                while ((ptr = reader.read()) != -1) {
                    buffer.append((char) ptr);
                }
                return buffer.toString();
            }
    
            /*
             * 转换主方法
             */
            public String conver(String curstyle) {
                String str="";
                try {
                    str = doc2pdf(curstyle);
                } catch (Exception e) {
                    // TODO: Auto-generated catch block
                    e.printStackTrace();
                }
    
                return str;
            }
    
            public String getswfPath() {
                if (swfFile.exists()) {
                    String tempString = swfFile.getPath();
                    tempString = tempString.replaceAll("\\", "/");
                    return tempString;
                } else {
                    return "";
                }
            }
    
        }


    在其他文件直接打点调用
    //localFi :word文件存放地址 outFileString:pdf输出路径

    DocConverterPdf d = new DocConverterPdf(localFi,outFileString);
    curUrl = d.conver(idtxt);

    前台:
    将控制层返回的路径编辑好!!!编辑好!!! 路径不对会导致文件读取失败(最好看看文件输出地址中是否有文件 好定位是哪里出错)
      window.open("${ctxStatic}/pdfjs/web/viewer.html?file=" + curUrls);
    package com.thinkgem.jeesite.modules.sys.utils;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.ArrayList;
    
    import com.itextpdf.text.Document;
    import com.itextpdf.text.DocumentException;
    import com.itextpdf.text.Image;
    import com.itextpdf.text.PageSize;
    import com.itextpdf.text.pdf.PdfWriter;
         
    /**
     * 图片转pdf工具类
     */
    public class ImgToPdfUtil {
     
        public static File Pdf(String imageUrl, String mOutputPdfFileName) {
            Document doc = new Document(PageSize.A4, 0, 0, 0, 0); //new一个pdf文档
            try {
                PdfWriter.getInstance(doc, new FileOutputStream(mOutputPdfFileName)); //pdf写入
                doc.open();//打开文档
               
                doc.newPage();  //在pdf创建一页
                Image png1 = Image.getInstance(imageUrl); //通过文件路径获取image
                float heigth = png1.getHeight();
                float width = png1.getWidth();
                int percent = getPercent2(heigth, width);
                png1.setAlignment(Image.MIDDLE);
                png1.scalePercent(percent + 1);// 表示是原来图像的比例;
                doc.add(png1);
                
                doc.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (DocumentException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
     
            File mOutputPdfFile = new File(mOutputPdfFileName);  //输出流
            if (!mOutputPdfFile.exists()) {
                mOutputPdfFile.deleteOnExit();
                return null;
            }
            return mOutputPdfFile; //反回文件输出流
        }
     
        public static int getPercent(float h, float w) {
            int p = 0;
            float p2 = 0.0f;
            if (h > w) {
                p2 = 297 / h * 100;
            } else {
                p2 = 210 / w * 100;
            }
            p = Math.round(p2);
            return p;
        }
     
        public static int getPercent2(float h, float w) {
            int p = 0;
            float p2 = 0.0f;
            p2 = 530 / w * 100;
            p = Math.round(p2);
            return p;
        }
     
        /** 
        * @Description: 通过图片路径及生成pdf路径,将图片转成pdf
        * @Author: zd
        * @Date: 2019/9/29
        */ 
        public void imgOfPdf(String filepath, String imgUrl) {
            try {
                String pdfUrl =  filepath;  //输出pdf文件路径
                File file = this.Pdf(imgUrl, pdfUrl);//生成pdf
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    同样的 在控制层调用工具类
    File pdf = ImgToPdfUtil.Pdf(localFi, outFileString); 返回前台响应即可
     //读取文件   如果文件是txt格式 需要转换 filePath:文件路径 filePaths:文件所在文件夹路径 fiesty:文件类型
         public static String readTxtFile(String filePath,String filePaths,String fiesty){
                 String backFileName="";
                try {
                        String encoding="GBK";
                        File file=new File(filePath);
                        long newcurtimeName = System.currentTimeMillis();
                        String relativeName=newcurtimeName+"."+fiesty;
                        String newlocalFi=filePaths+relativeName;//新的文件名称和路径
                        backFileName=relativeName+"-"+newlocalFi;
                        //File newfile=new File(newlocalFi);
                        if(file.isFile() && file.exists()){ //判断文件是否存在
                            InputStreamReader read = new InputStreamReader(
                            new FileInputStream(file),encoding);//考虑到编码格式
                            BufferedReader bufferedReader = new BufferedReader(read);
                            String lineTxt = null;
                            //这个地方要注意,如果是本地的话请化成GBK不然会乱码,linux上用UTF-8
                            OutputStreamWriter fileWritter = new OutputStreamWriter(new FileOutputStream(newlocalFi, true),"UTF-8");
                            BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
                            while((lineTxt = bufferedReader.readLine()) != null){
                                 bufferWritter.write(lineTxt);
                                 bufferWritter.newLine();
                                 System.out.println(lineTxt);
                            }
                            bufferWritter.close();
                            read.close();
                            File delfile =new File(filePath);
                            delfile.delete();
                               
                }else{
                    System.out.println("找不到指定的文件");
                }
                } catch (Exception e) {
                    System.out.println("读取文件内容出错");
                    e.printStackTrace();
                }
                return backFileName;
            }
  • 相关阅读:
    DB2原因码7,解决方法
    打包项目成war包并部署到服务器上,项目运行一直显示加载中
    解决js中对象中属性是数组中对应元素,不能使用点数组元素(.数组[i])来获取value值来循环,属性不能是数组元素array[i]的问题
    javaweb项目中jsp的from表单提交action内容与web.xml的servlet-mapping对应
    Collection迭代器Iterator的使用
    使用switch计算出某年某月某日是今年的第几天,输出一直是当月天数
    DB2添加联合主键
    您的主机中的软件中止了一个已建立的连接
    java中在构造方法中修改线程名,修改失败原因(现已修改成功)
    String字符串加号的作用与基本数据类型加号的作用的区别
  • 原文地址:https://www.cnblogs.com/ljc1212/p/12934224.html
Copyright © 2011-2022 走看看