springboot高级功能(九)jodconverter实现在线预览

思路:其他文档转成配pdf然后通过流发送到前台,前台支持pdf

1.jodconverter-spring-boot-starter方法

1.pom文件

        
        
            org.jodconverter
            jodconverter-core
            4.2.2
        

        
        
            org.jodconverter
            jodconverter-spring-boot-starter
            4.2.2
        


        
        
            org.jodconverter
            jodconverter-local
            4.2.2
        

2.application.xml

jodconverter:
  local:
    enabled: true
    max-tasks-per-process: 10
    port-numbers: 8100

3.调用

@RestController
public class OnlinePreviewController {

    // 第一步:转换器直接注入
    @Autowired
    DocumentConverter documentConverter;

    @GetMapping("/toPdfFile")
    public String toPdfFile(FileMessage fileMessage) {
        // 获取HttpServletResponse
        HttpServletResponse response =
            ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getResponse();
        // 需要转换的文件
        File file = new File(fileMessage.getFileDownloadUri());
        try {
            // 转换之后文件生成的地址
            File newFile = new File("D:/common_files/pdf");
            if (!newFile.exists()) {
                newFile.mkdirs();
            }
            String converterPdf = "D:/common_files/pdf" + "/" + fileMessage.getFileName() + "-pdf.pdf";
            // 文件转化
            documentConverter.convert(file).to(new File(converterPdf)).execute();
            // 使用response,将pdf文件以流的方式发送的前段
            ServletOutputStream outputStream = response.getOutputStream();
            // 读取文件
            InputStream in = new FileInputStream(new File(converterPdf));
            // copy文件
            int i = IOUtils.copy(in, outputStream);
            System.out.println(i);
            in.close();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "This is to pdf";
    }
}

但是这种会发生excle过长穿行的情况;

2.使用com.artofsolving

1.pom文件


            com.artofsolving
            jodconverter
            2.2.2
        

        
            org.openoffice
            jurt
            3.0.1
        
        
            org.openoffice
            ridl
            3.0.1
        
        
            org.openoffice
            juh
            3.0.1
        
        
            org.openoffice
            unoil
            3.0.1
        

注意2.2.2是在maven服务器上没有 所以需要单独下载处理 直接上网盘地址

https://pan.baidu.com/s/1dwfLHhUUg9nXf1aRAYfJ8Q

提取码:57nm

2.Controller

import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

@RestController
public class OnlinePreviewController {

    @GetMapping("/toPdfFile")
    public void toPdfFile(FileMessage fileMessage) throws IOException {
        // 获取HttpServletResponse
        HttpServletResponse response =
            ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getResponse();
        // 源文件目录
        File inputFile = new File(fileMessage.getFileDownloadUri());
        if (!inputFile.exists()) {
            System.out.println("源文件不存在!");
            return;
        }
        // 转换之后文件生成的地址
        File newFile = new File("D:/common_files/pdf");
        if (!newFile.exists()) {
            newFile.mkdirs();
        }
        String converterPdf = "D:/common_files/pdf" + "/" + fileMessage.getFileName() + "-pdf.pdf";
        // 输出文件目录
        File outputFile = new File(converterPdf);
        if (!outputFile.getParentFile().exists()) {
            outputFile.getParentFile().exists();
        }
        // 调用openoffice服务线程
        String command =
            "C:/Program Files (x86)/OpenOffice 4/program/soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\"";
        Process p = Runtime.getRuntime().exec(command);
        // 连接openoffice服务
        OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);
        connection.connect();
        // 转换
        DocumentConverter converter = new ConverterDocument(connection);
        converter.convert(inputFile, outputFile);
        ServletOutputStream outputStream = response.getOutputStream();
        // 读取文件
        InputStream in = new FileInputStream(new File(converterPdf));
        // copy文件
        int i = IOUtils.copy(in, outputStream);
        in.close();
        outputStream.close();
        // 关闭连接
        connection.disconnect();
        // 关闭进程
        p.destroy();
        System.out.println("转换完成!");
    }
}

转换方法为

DocumentConverter converter = new ConverterDocument(connection);
        converter.convert(inputFile, outputFile);

因为excel折行问题 所以我们把纸张改变 重写了ConverterDocument

3.ConverterDocument 

import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;
import com.sun.star.awt.Size;
import com.sun.star.beans.PropertyValue;
import com.sun.star.lang.XComponent;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.view.PaperFormat;
import com.sun.star.view.XPrintable;

/**
 * 应用模块名称:
 * 代码描述:
 * Copyright: Copyright (C) 2020, Inc. All rights reserved.
 * Company: 万朋测绘科技有限公司
 *
 * @author baocl
 * @since 2020/5/27 14:49
 */
public class ConverterDocument extends StreamOpenOfficeDocumentConverter {

    public ConverterDocument(OpenOfficeConnection connection) {
        super(connection);
    }

    public final static Size A5, A4, A3;
    public final static Size B4, B5, B6;
    public final static Size paperSize;
    static {
        A5 = new Size(14800, 21000);
        A4 = new Size(21000, 29700);
        A3 = new Size(29700, 42000);
        B4 = new Size(25000, 35300);
        B5 = new Size(17600, 25000);
        B6 = new Size(12500, 17600);
        // 最大限度 宽 1600000
        paperSize = new Size(29700, 27940);
    }

    @Override
    protected void refreshDocument(XComponent document) {
        super.refreshDocument(document);
        XPrintable xPrintable = (XPrintable)UnoRuntime.queryInterface(XPrintable.class, document);
        PropertyValue[] printerDesc = new PropertyValue[2];
        // 转换
        printerDesc[0] = new PropertyValue();
        printerDesc[0].Name = "PaperFormat";
        printerDesc[0].Value = PaperFormat.USER;
        // 纸张大小
        printerDesc[1] = new PropertyValue();
        printerDesc[1].Name = "PaperSize";
        printerDesc[1].Value = paperSize;
        try {
            xPrintable.setPrinter(printerDesc);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

但是这里有一个问题 多sheet页 只有第一页改变了 所以解决办法想了两个

1:将多个sheet合成一个

2:将多sheet也分开打印

(暂时没实现)

然后发现了kkFileView 请参考:https://blog.csdn.net/qq_20143059/article/details/106427297

emmmmmm真香

你可能感兴趣的:(springboot高级功能(九)jodconverter实现在线预览)