| | |
| | | String inputFile = new File(filePath, fileName).getAbsolutePath(); |
| | | String outputDir = new File(filePath, "pdf").getAbsolutePath(); |
| | | |
| | | log.info("开始转换文件:{}", inputFile); |
| | | // 创建输出目录 |
| | | new File(outputDir).mkdirs(); |
| | | |
| | |
| | | |
| | | // 创建进程构建器 |
| | | ProcessBuilder pb = new ProcessBuilder(command); |
| | | log.info("执行命令:{}", command); |
| | | |
| | | // 设置环境变量 |
| | | Map<String, String> env = pb.environment(); |
| | | env.put("LC_ALL", "zh_CN.UTF-8"); |
| | | env.put("LANG", "zh_CN.UTF-8"); |
| | | env.put("LANGUAGE", "zh_CN.UTF-8"); |
| | | |
| | | log.info("环境变量:{}", env); |
| | | // 重定向错误流到标准输出 |
| | | pb.redirectErrorStream(true); |
| | | |
| | | log.info("启动进程"); |
| | | // 启动进程 |
| | | Process process = pb.start(); |
| | | |
| | | log.info("等待进程完成"); |
| | | // 读取输出 |
| | | StringBuilder output = new StringBuilder(); |
| | | try (BufferedReader reader = new BufferedReader( |
| | |
| | | String line; |
| | | while ((line = reader.readLine()) != null) { |
| | | output.append(line).append("\n"); |
| | | System.out.println(line); |
| | | } |
| | | } |
| | | |
| | | log.info("进程完成,输出:{}", output); |
| | | // 等待进程完成,设置超时 |
| | | if (!process.waitFor(120, TimeUnit.SECONDS)) { |
| | | process.destroyForcibly(); |
| | | throw new RuntimeException("转换超时"); |
| | | } |
| | | |
| | | log.info("进程完成,退出码"); |
| | | int exitCode = process.exitValue(); |
| | | if (exitCode != 0) { |
| | | throw new RuntimeException("转换失败,退出码:" + exitCode + "\n输出:" + output); |
| | | } |
| | | |
| | | log.info("进程完成,退出码:{}", exitCode); |
| | | // 检查生成的PDF文件 |
| | | String pdfFileName = fileName.substring(0, fileName.lastIndexOf(".")) + ".pdf"; |
| | | File pdfFile = new File(outputDir, pdfFileName); |
| | | |
| | | log.info("PDF文件:{}", pdfFile); |
| | | if (!pdfFile.exists() || pdfFile.length() == 0) { |
| | | throw new RuntimeException("PDF文件未生成或为空"); |
| | | } |
| | | String absolutePath = pdfFile.getAbsolutePath(); |
| | | |
| | | log.info("PDF文件绝对路径:{}", absolutePath); |
| | | MultipartFile file = convertFileToMultipartFile(pdfFile); |
| | | log.info("PDF文件转换完成"); |
| | | // String s = tencentCosUtil.upLoadFile(multipartFile,"/wordToPdf"); |
| | | String url = ObsUploadUtil.obsUpload(file); |
| | | log.info("PDF文件上传完成"); |
| | | return url; |
| | | |
| | | } catch (Exception e) { |
| | |
| | | return result; |
| | | } |
| | | |
| | | |
| | | |
| | | } |