Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yd-oss
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
xingmin
yd-oss
Commits
e31fb1f3
Commit
e31fb1f3
authored
Oct 13, 2025
by
zhangxingmin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
push
parent
774ccada
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
94 additions
and
10 deletions
+94
-10
yd-oss-service/src/main/java/com/yd/oss/service/utils/PdfUtil.java
+94
-10
No files found.
yd-oss-service/src/main/java/com/yd/oss/service/utils/PdfUtil.java
View file @
e31fb1f3
...
...
@@ -5,6 +5,8 @@ import com.documents4j.api.IConverter;
import
com.documents4j.job.LocalConverter
;
import
lombok.extern.slf4j.Slf4j
;
import
java.io.*
;
import
java.nio.file.Files
;
import
java.nio.file.StandardCopyOption
;
@Slf4j
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 wordFile word源文件
*/
...
...
@@ -35,11 +40,24 @@ public class PdfUtil {
String
sourcePath
=
wordFile
.
getAbsolutePath
();
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
=
{
"/usr/bin/libreoffice --invisible --convert-to pdf --outdir "
+
outDir
+
" "
+
sourcePath
,
"/usr/bin/soffice --invisible --convert-to pdf --outdir "
+
outDir
+
" "
+
sourcePath
,
"/usr/lib64/libreoffice/program/soffice --invisible --convert-to pdf --outdir "
+
outDir
+
" "
+
sourcePath
"/usr/bin/libreoffice --invisible --headless --convert-to pdf --outdir "
+
outDir
+
" "
+
sourcePath
,
"/usr/bin/soffice --invisible --headless --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
;
...
...
@@ -47,29 +65,95 @@ public class PdfUtil {
for
(
String
command
:
commands
)
{
try
{
log
.
info
(
"尝试命令: "
+
command
);
executeLinuxCmd
(
command
);
log
.
info
(
"尝试命令: {}"
,
command
);
String
output
=
executeLinuxCmdWithOutput
(
command
);
log
.
info
(
"命令输出: {}"
,
output
);
// 等待一下让文件生成
Thread
.
sleep
(
2000
);
// 检查PDF文件是否生成
if
(
pdfFile
.
exists
()
&&
pdfFile
.
length
()
>
0
)
{
log
.
info
(
"PDF转换成功,使用命令: "
+
command
);
log
.
info
(
"PDF转换成功! 文件: {}, 大小: {} bytes, 使用命令: {}"
,
pdfFile
.
getAbsolutePath
(),
pdfFile
.
length
(),
command
);
success
=
true
;
break
;
}
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
)
{
lastException
=
e
;
log
.
warn
(
"命令执行失败: "
+
command
+
", 错误: "
+
e
.
getMessage
());
log
.
error
(
"命令执行失败: {}, 错误: {}"
,
command
,
e
.
getMessage
());
}
}
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
();
}
/**
* 执行命令行
*
* @param cmd 命令行
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment