Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
Y
yn-health-science
Project
Project
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
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
徐俊
yn-health-science
Commits
116526fd
Commit
116526fd
authored
Feb 14, 2025
by
徐俊
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
xujun
parent
9d182f57
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
195 additions
and
6 deletions
+195
-6
pom.xml
science-admin/pom.xml
+12
-5
ComProjectController.java
...ava/com/yiboshi/science/rest/v1/ComProjectController.java
+1
-0
WordToPdfConverter.java
...in/java/com/yiboshi/science/utils/WordToPdfConverter.java
+182
-1
No files found.
science-admin/pom.xml
View file @
116526fd
...
...
@@ -163,21 +163,28 @@
<artifactId>
poi-tl
</artifactId>
<version>
1.10.0
</version>
</dependency>
<!-- Apache POI - 用于处理 Microsoft Office 文档 -->
<dependency>
<groupId>
org.apache.poi
</groupId>
<artifactId>
poi
</artifactId>
<version>
4.1.2
</version>
<version>
5.2.3
</version>
</dependency>
<dependency>
<groupId>
org.apache.poi
</groupId>
<artifactId>
poi-ooxml
</artifactId>
<version>
4.1.2
</version>
<version>
5.2.3
</version>
</dependency>
<dependency>
<groupId>
org.apache.poi
</groupId>
<artifactId>
poi-ooxml-schemas
</artifactId>
<version>
4.1.2
</version>
<artifactId>
poi-scratchpad
</artifactId>
<version>
5.2.3
</version>
</dependency>
<!-- Apache PDFBox - 用于创建和操作 PDF -->
<dependency>
<groupId>
org.apache.pdfbox
</groupId>
<artifactId>
pdfbox
</artifactId>
<version>
2.0.27
</version>
</dependency>
<dependency>
...
...
science-admin/src/main/java/com/yiboshi/science/rest/v1/ComProjectController.java
View file @
116526fd
...
...
@@ -215,6 +215,7 @@ public class ComProjectController extends BaseController<ComProjectService, ComP
@RequestMapping
(
"/projectExport/{id}"
)
@PostMapping
public
ResponseDataModel
<
String
>
projectExport
(
@PathVariable
String
id
)
throws
DocumentException
,
IOException
{
ComProjectDTO
dto
=
comProjectService
.
getProjectById
(
id
);
List
<
SystemParameter
>
list
=
systemParameterService
.
getListByType
(
67
);
String
outputPath
=
"D:\\申请书和合同书.pdf"
;
...
...
science-admin/src/main/java/com/yiboshi/science/utils/WordToPdfConverter.java
View file @
116526fd
package
com
.
yiboshi
.
science
.
utils
;
public
class
WordToPdfConverter
{
package
com
.
yiboshi
.
science
.
utils
;
import
org.apache.poi.xwpf.usermodel.XWPFDocument
;
import
org.apache.poi.hwpf.HWPFDocument
;
import
org.apache.pdfbox.pdmodel.PDDocument
;
import
org.apache.pdfbox.pdmodel.PDPage
;
import
org.apache.pdfbox.pdmodel.PDPageContentStream
;
import
org.apache.pdfbox.pdmodel.font.PDType0Font
;
import
org.apache.poi.xwpf.usermodel.XWPFParagraph
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
java.io.*
;
import
java.util.List
;
public
class
WordToPdfConverter
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
WordToPdfConverter
.
class
);
/**
* 将Word文档转换为PDF
* @param wordPath Word文档路径
* @param pdfPath 输出PDF路径
* @return 转换是否成功
*/
public
static
boolean
convertToPdf
(
String
wordPath
,
String
pdfPath
)
{
// 检查文件是否存在
File
wordFile
=
new
File
(
wordPath
);
if
(!
wordFile
.
exists
())
{
logger
.
error
(
"Word文件不存在: {}"
,
wordPath
);
return
false
;
}
// 检查输出目录是否存在
File
pdfFile
=
new
File
(
pdfPath
);
File
parentDir
=
pdfFile
.
getParentFile
();
if
(!
parentDir
.
exists
())
{
parentDir
.
mkdirs
();
}
try
{
if
(
wordPath
.
toLowerCase
().
endsWith
(
".docx"
))
{
return
convertDocxToPdf
(
wordPath
,
pdfPath
);
}
else
if
(
wordPath
.
toLowerCase
().
endsWith
(
".doc"
))
{
return
convertDocToPdf
(
wordPath
,
pdfPath
);
}
else
{
logger
.
error
(
"不支持的文件格式: {}"
,
wordPath
);
return
false
;
}
}
catch
(
Exception
e
)
{
logger
.
error
(
"Word转PDF失败"
,
e
);
return
false
;
}
}
/**
* 将Word文档转换为PDF(字节数组版本)
* @param wordBytes Word文档的字节数组
* @param pdfPath 输出PDF路径
* @return 转换是否成功
*/
public
static
boolean
convertToPdf
(
byte
[]
wordBytes
,
String
pdfPath
)
{
if
(
wordBytes
==
null
||
wordBytes
.
length
==
0
)
{
logger
.
error
(
"Word文档字节数组为空"
);
return
false
;
}
// 检查输出目录是否存在
File
pdfFile
=
new
File
(
pdfPath
);
File
parentDir
=
pdfFile
.
getParentFile
();
if
(!
parentDir
.
exists
())
{
parentDir
.
mkdirs
();
}
try
{
// 创建临时文件
File
tempFile
=
File
.
createTempFile
(
"temp"
,
".docx"
);
try
(
FileOutputStream
fos
=
new
FileOutputStream
(
tempFile
))
{
fos
.
write
(
wordBytes
);
}
// 转换文件
boolean
result
=
convertToPdf
(
tempFile
.
getAbsolutePath
(),
pdfPath
);
// 删除临时文件
tempFile
.
delete
();
return
result
;
}
catch
(
Exception
e
)
{
logger
.
error
(
"Word转PDF失败"
,
e
);
return
false
;
}
}
/**
* 转换DOCX文件到PDF
*/
private
static
boolean
convertDocxToPdf
(
String
docxPath
,
String
pdfPath
)
throws
IOException
{
try
(
FileInputStream
fis
=
new
FileInputStream
(
docxPath
);
XWPFDocument
document
=
new
XWPFDocument
(
fis
);
PDDocument
pdfDocument
=
new
PDDocument
())
{
// 获取文档内容
List
<
XWPFParagraph
>
paragraphs
=
document
.
getParagraphs
();
// 创建PDF页面
PDPage
page
=
new
PDPage
();
pdfDocument
.
addPage
(
page
);
// 加载字体
PDType0Font
font
=
PDType0Font
.
load
(
pdfDocument
,
WordToPdfConverter
.
class
.
getResourceAsStream
(
"/fonts/simsun.ttc"
));
// 创建内容流
try
(
PDPageContentStream
contentStream
=
new
PDPageContentStream
(
pdfDocument
,
page
))
{
float
y
=
page
.
getMediaBox
().
getHeight
()
-
50
;
// 写入段落
for
(
XWPFParagraph
para
:
paragraphs
)
{
String
text
=
para
.
getText
();
contentStream
.
beginText
();
contentStream
.
setFont
(
font
,
12
);
contentStream
.
newLineAtOffset
(
50
,
y
);
contentStream
.
showText
(
text
);
contentStream
.
endText
();
y
-=
15
;
}
}
// 保存PDF
pdfDocument
.
save
(
pdfPath
);
return
true
;
}
}
/**
* 转换DOC文件到PDF
*/
private
static
boolean
convertDocToPdf
(
String
docPath
,
String
pdfPath
)
throws
IOException
{
try
(
FileInputStream
fis
=
new
FileInputStream
(
docPath
);
HWPFDocument
document
=
new
HWPFDocument
(
fis
);
PDDocument
pdfDocument
=
new
PDDocument
())
{
// 获取文档内容
String
text
=
document
.
getDocumentText
();
// 创建PDF页面
PDPage
page
=
new
PDPage
();
pdfDocument
.
addPage
(
page
);
// 加载字体
PDType0Font
font
=
PDType0Font
.
load
(
pdfDocument
,
WordToPdfConverter
.
class
.
getResourceAsStream
(
"/fonts/simsun.ttc"
));
// 创建内容流
try
(
PDPageContentStream
contentStream
=
new
PDPageContentStream
(
pdfDocument
,
page
))
{
contentStream
.
beginText
();
contentStream
.
setFont
(
font
,
12
);
contentStream
.
newLineAtOffset
(
50
,
page
.
getMediaBox
().
getHeight
()
-
50
);
contentStream
.
showText
(
text
);
contentStream
.
endText
();
}
// 保存PDF
pdfDocument
.
save
(
pdfPath
);
return
true
;
}
}
/**
* 检查文件是否为Word文档
*/
public
static
boolean
isWordFile
(
String
fileName
)
{
if
(
fileName
==
null
||
fileName
.
trim
().
isEmpty
())
{
return
false
;
}
String
lowerFileName
=
fileName
.
toLowerCase
();
return
lowerFileName
.
endsWith
(
".doc"
)
||
lowerFileName
.
endsWith
(
".docx"
);
}
}
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