无关风月
2025-03-06 d8b5536c53c7eddd0c6705d63babeaea005944b0
ruoyi-admin/src/main/java/com/ruoyi/web/controller/tool/PdfUtils.java
@@ -12,6 +12,7 @@
import java.io.*;
import java.net.URL;
import java.nio.file.Files;
import java.util.concurrent.TimeUnit;
@Slf4j
@Component
@@ -23,44 +24,85 @@
     *
     * @param url
     */
//    public  String wordToPdf(String url,String filePath, String fileName) {
//        try {
//            DocumentType documentType = DocumentType.DOC;
//            if(url.contains(".docx")){
//                documentType = DocumentType.DOCX;
//            }
//            if(url.contains(".doc")){
//                documentType = DocumentType.DOC;
//            }
//            if(url.contains(".xlsx")){
//                documentType = DocumentType.XLSX;
//            }else {
//                if(url.contains(".xls")){
//                    documentType = DocumentType.XLS;
//                }
//            }
//            InputStream inputStream = new URL(url).openStream();
//            ByteArrayOutputStream stream = new ByteArrayOutputStream();
//            IConverter converter = LocalConverter.builder().build();
//            converter.convert(inputStream)
//                    .as(documentType)
//                    .to(stream)
//                    .as(DocumentType.PDF).execute();
//
//            //上传图片
//            byte2File(stream.toByteArray(),filePath + "/pdf",fileName.substring(0,fileName.lastIndexOf(".")) + ".pdf");
//            MultipartFile multipartFile = convertToMultipartFile(stream,fileName.substring(0,fileName.lastIndexOf(".")) );
//            String s = tencentCosUtil.upLoadFile(multipartFile,"/wordToPdf");
//
//            stream.close();
//            inputStream.close();
//            return s;
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return null;
//    }
    public  String wordToPdf(String url,String filePath, String fileName) {
        try {
            DocumentType documentType = DocumentType.DOC;
            if(url.contains(".docx")){
                documentType = DocumentType.DOCX;
            }
            if(url.contains(".doc")){
            } else if (url.contains(".doc")) {
                documentType = DocumentType.DOC;
            }
            if(url.contains(".xlsx")){
            } else if (url.contains(".xlsx")) {
                documentType = DocumentType.XLSX;
            }else {
                if(url.contains(".xls")){
            } else if (url.contains(".xls")) {
                    documentType = DocumentType.XLS;
                }
            // Ensure the URL has a protocol part
            if (!url.startsWith("file://") && !url.startsWith("http://") && !url.startsWith("https://")) {
                url = "file://" + url;
            }
            InputStream inputStream = new URL(url).openStream();
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            IConverter converter = LocalConverter.builder().build();
            converter.convert(inputStream)
                    .as(documentType)
                    .to(stream)
                    .as(DocumentType.PDF).execute();
            //上传图片
            byte2File(stream.toByteArray(),filePath + "/pdf",fileName.substring(0,fileName.lastIndexOf(".")) + ".pdf");
            MultipartFile multipartFile = convertToMultipartFile(stream,fileName.substring(0,fileName.lastIndexOf(".")) );
            String s = tencentCosUtil.upLoadFile(multipartFile,"/wordToPdf");
            // 使用LibreOffice进行转换
            ProcessBuilder pb = new ProcessBuilder(
                    "soffice",
                    "--headless",
                    "--convert-to", "pdf",
                    "--outdir", new File(filePath+ "/pdf"+fileName.substring(0, fileName.lastIndexOf(".")) + ".pdf").getParent(),
                    filePath+fileName
            );
            Process process = pb.start();
            stream.close();
            inputStream.close();
            return s;
            // 等待转换完成
            int exitCode = process.waitFor();
            if (exitCode == 0) {
                System.out.println("PDF转换成功!");
            } else {
                System.out.println("PDF转换失败!");
            }
            return "";
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("Failed to generate PDF: " + e.getMessage(), e);
        }
        return null;
    }
    public static MultipartFile convertToMultipartFile(ByteArrayOutputStream baos, String fileName) throws IOException {
@@ -209,10 +251,10 @@
    }
    public String test(String fileName){
        String url = "file:///E:\\"+fileName;
        String url = "file:///usr/local/project/file/"+fileName;
//        String filePath = "E:\\qiyeweixin\\WXWork\\1688855207501340\\Cache\\File\\2024-09";
//        String fileName = "专业技术工作总结.docx";4
        String filePath = "E:\\";
        String filePath = "/usr/local/project/file/";
        String s = wordToPdf(url, filePath, fileName);
        System.err.println(s);