Commit e31fb1f3 by zhangxingmin

push

parent 774ccada
...@@ -5,6 +5,8 @@ import com.documents4j.api.IConverter; ...@@ -5,6 +5,8 @@ import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter; import com.documents4j.job.LocalConverter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import java.io.*; import java.io.*;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
@Slf4j @Slf4j
public class PdfUtil { public class PdfUtil {
...@@ -27,7 +29,10 @@ public class PdfUtil { ...@@ -27,7 +29,10 @@ public class PdfUtil {
} }
/** /**
* linux系统word转pdf   * 使用LibreOffice转换。系统需安装LibreOffice   * 转换命令 libreoffice --invisible --convert-to pdf --outdir output_dir source_path   * 转换后的pdf文件名使用的是源文件的名称,所以如果要指定输出文件名称,就需把源文件名称改成想要输出的名称 * linux系统word转pdf
* 使用LibreOffice转换。系统需安装LibreOffice
* 转换命令 libreoffice --invisible --convert-to pdf --outdir output_dir source_path
* 转换后的pdf文件名使用的是源文件的名称,所以如果要指定输出文件名称,就需把源文件名称改成想要输出的名称
* @param pdfFile 转换后的pdf文件 * @param pdfFile 转换后的pdf文件
* @param wordFile word源文件 * @param wordFile word源文件
*/ */
...@@ -35,11 +40,24 @@ public class PdfUtil { ...@@ -35,11 +40,24 @@ public class PdfUtil {
String sourcePath = wordFile.getAbsolutePath(); String sourcePath = wordFile.getAbsolutePath();
String outDir = pdfFile.getAbsolutePath().substring(0, pdfFile.getAbsolutePath().lastIndexOf(File.separator)); String outDir = pdfFile.getAbsolutePath().substring(0, pdfFile.getAbsolutePath().lastIndexOf(File.separator));
log.info("PDF转换参数 - 源文件: {}, 输出目录: {}, 目标PDF: {}", sourcePath, outDir, pdfFile.getAbsolutePath());
// 检查源文件是否存在
if (!wordFile.exists()) {
throw new RuntimeException("源文件不存在: " + sourcePath);
}
log.info("源文件存在,大小: {} bytes", wordFile.length());
// 确保输出目录存在
new File(outDir).mkdirs();
// 使用绝对路径尝试不同的命令 // 使用绝对路径尝试不同的命令
String[] commands = { String[] commands = {
"/usr/bin/libreoffice --invisible --convert-to pdf --outdir " + outDir + " " + sourcePath, "/usr/bin/libreoffice --invisible --headless --convert-to pdf --outdir " + outDir + " " + sourcePath,
"/usr/bin/soffice --invisible --convert-to pdf --outdir " + outDir + " " + sourcePath, "/usr/bin/soffice --invisible --headless --convert-to pdf --outdir " + outDir + " " + sourcePath,
"/usr/lib64/libreoffice/program/soffice --invisible --convert-to pdf --outdir " + outDir + " " + sourcePath "/usr/lib64/libreoffice/program/soffice --invisible --headless --convert-to pdf --outdir " + outDir + " " + sourcePath,
// 添加超时和详细输出
"timeout 30s /usr/bin/libreoffice --invisible --headless --convert-to pdf:writer_pdf_Export --outdir " + outDir + " " + sourcePath
}; };
boolean success = false; boolean success = false;
...@@ -47,26 +65,92 @@ public class PdfUtil { ...@@ -47,26 +65,92 @@ public class PdfUtil {
for (String command : commands) { for (String command : commands) {
try { try {
log.info("尝试命令: " + command); log.info("尝试命令: {}", command);
executeLinuxCmd(command); String output = executeLinuxCmdWithOutput(command);
log.info("命令输出: {}", output);
// 等待一下让文件生成
Thread.sleep(2000);
// 检查PDF文件是否生成 // 检查PDF文件是否生成
if (pdfFile.exists() && pdfFile.length() > 0) { if (pdfFile.exists() && pdfFile.length() > 0) {
log.info("PDF转换成功,使用命令: " + command); log.info("PDF转换成功! 文件: {}, 大小: {} bytes, 使用命令: {}",
pdfFile.getAbsolutePath(), pdfFile.length(), command);
success = true; success = true;
break; break;
} else { } else {
log.warn("命令执行成功但未生成PDF文件: " + command); log.warn("命令执行成功但未生成目标PDF文件: {}", command);
// 检查输出目录中是否有其他PDF文件(LibreOffice可能使用不同的命名)
File outDirFile = new File(outDir);
File[] pdfFiles = outDirFile.listFiles((dir, name) -> name.toLowerCase().endsWith(".pdf"));
if (pdfFiles != null && pdfFiles.length > 0) {
log.info("在输出目录中找到以下PDF文件:");
for (File f : pdfFiles) {
log.info(" - {} (大小: {} bytes)", f.getName(), f.length());
// 如果找到PDF文件,复制到目标位置
if (f.length() > 0) {
Files.copy(f.toPath(), pdfFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
if (pdfFile.exists() && pdfFile.length() > 0) {
log.info("已复制PDF文件到目标位置: {}", pdfFile.getAbsolutePath());
success = true;
break;
}
}
}
if (success) break;
} else {
log.warn("输出目录中未找到任何PDF文件");
}
} }
} catch (Exception e) { } catch (Exception e) {
lastException = e; lastException = e;
log.warn("命令执行失败: " + command + ", 错误: " + e.getMessage()); log.error("命令执行失败: {}, 错误: {}", command, e.getMessage());
} }
} }
if (!success) { if (!success) {
throw new RuntimeException("所有LibreOffice转换命令都失败了", lastException); throw new RuntimeException("所有LibreOffice转换命令都失败,源文件: " + sourcePath, lastException);
}
} }
/**
* 执行命令行并返回输出
*/
private static String executeLinuxCmdWithOutput(String cmd) throws Exception {
log.info("执行命令: {}", cmd);
Process process = Runtime.getRuntime().exec(new String[]{"sh", "-c", cmd});
// 读取标准输出
BufferedReader inputReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
// 读取错误输出
BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
StringBuilder output = new StringBuilder();
StringBuilder error = new StringBuilder();
String line;
while ((line = inputReader.readLine()) != null) {
output.append(line).append("\n");
log.info("命令输出: {}", line);
}
while ((line = errorReader.readLine()) != null) {
error.append(line).append("\n");
log.error("命令错误: {}", line);
}
int exitCode = process.waitFor();
log.info("命令退出码: {}", exitCode);
if (exitCode != 0) {
throw new RuntimeException("命令执行失败,退出码: " + exitCode + ", 错误: " + error.toString());
}
inputReader.close();
errorReader.close();
return output.toString();
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment