Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
Y
yifu-mvp
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
fangxinjiang
yifu-mvp
Commits
a537a4b4
Commit
a537a4b4
authored
May 29, 2025
by
hongguangwu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
MVP1.7.11-是否自动生成解除劳动合同书 0否1是
parent
7badd3a1
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
50 additions
and
277 deletions
+50
-277
pom.xml
yifu-social/yifu-social-biz/pom.xml
+6
-15
TSocialFriendWordController.java
...1/yifu/social/controller/TSocialFriendWordController.java
+0
-29
WordToImageUtil.java
.../yifu/cloud/plus/v1/yifu/social/util/WordToImageUtil.java
+44
-84
WordToImageWithSeal.java
...u/cloud/plus/v1/yifu/social/util/WordToImageWithSeal.java
+0
-93
WordUtil.java
...ava/com/yifu/cloud/plus/v1/yifu/social/util/WordUtil.java
+0
-56
No files found.
yifu-social/yifu-social-biz/pom.xml
View file @
a537a4b4
...
...
@@ -107,24 +107,15 @@
<type>
jar
</type>
</dependency>
<!-- word转pdf -->
<dependency>
<groupId>
org.jodconverter
</groupId>
<artifactId>
jodconverter-loca
l
</artifactId>
<version>
4.4
.0
</version>
<groupId>
com.deepoove
</groupId>
<artifactId>
poi-t
l
</artifactId>
<version>
1.8
.0
</version>
</dependency>
<!-- pdf转图片 -->
<dependency>
<groupId>
org.apache.pdfbox
</groupId>
<artifactId>
pdfbox
</artifactId>
<version>
2.0.27
</version>
</dependency>
<dependency>
<groupId>
com.
itextpdf
</groupId>
<artifactId>
itextpdf
</artifactId>
<version>
5.5.13.3
</version>
<groupId>
com.
luhuiguo
</groupId>
<artifactId>
aspose-words
</artifactId>
<version>
23.1
</version>
</dependency>
</dependencies>
...
...
yifu-social/yifu-social-biz/src/main/java/com/yifu/cloud/plus/v1/yifu/social/controller/TSocialFriendWordController.java
View file @
a537a4b4
...
...
@@ -27,8 +27,6 @@ import com.yifu.cloud.plus.v1.yifu.social.config.SocialFriendConfig;
import
com.yifu.cloud.plus.v1.yifu.social.entity.TSocialFriendBackLog
;
import
com.yifu.cloud.plus.v1.yifu.social.mapper.TAttaInfoMapper
;
import
com.yifu.cloud.plus.v1.yifu.social.util.WordToImageUtil
;
import
com.yifu.cloud.plus.v1.yifu.social.util.WordToImageWithSeal
;
import
com.yifu.cloud.plus.v1.yifu.social.util.WordUtil
;
import
io.swagger.v3.oas.annotations.Operation
;
import
io.swagger.v3.oas.annotations.tags.Tag
;
import
lombok.RequiredArgsConstructor
;
...
...
@@ -121,31 +119,4 @@ public class TSocialFriendWordController {
}
return
R
.
ok
();
}
public
R
<
String
>
replaceText
(
String
inputPath
,
String
outputPath
,
String
outputImagePath
)
throws
Exception
{
String
src
=
"1735185038602皖信.png"
;
URL
url
=
ossUtil
.
getObjectUrl
(
null
,
src
);
if
(
Common
.
isEmpty
(
url
))
{
throw
new
RuntimeException
(
"获取印章图片失败"
);
}
try
(
InputStream
imageStream
=
url
.
openStream
())
{
// 准备替换参数
Map
<
String
,
String
>
replacements
=
new
HashMap
<>();
replacements
.
put
(
"empName"
,
"张三丰"
);
replacements
.
put
(
"empSex"
,
"女"
);
replacements
.
put
(
"empAge"
,
"33"
);
replacements
.
put
(
"idCard"
,
"340826199012121212"
);
replacements
.
put
(
"startDate"
,
"2023年11月28日"
);
replacements
.
put
(
"leaveReason"
,
"个人原因"
);
replacements
.
put
(
"leaveDate"
,
"2025年01月20日"
);
replacements
.
put
(
"nowDate"
,
DateUtil
.
formatDatePatten
(
new
Date
(),
DateUtil
.
CHINA_PATTEN_YMD
));
// 执行替换 + 插入印章
WordUtil
.
replaceTextInWord
(
inputPath
,
outputPath
,
replacements
);
WordToImageWithSeal
.
convertAndAddSeal
(
outputPath
,
imageStream
,
outputImagePath
);
}
return
R
.
ok
();
}
}
yifu-social/yifu-social-biz/src/main/java/com/yifu/cloud/plus/v1/yifu/social/util/WordToImageUtil.java
View file @
a537a4b4
package
com
.
yifu
.
cloud
.
plus
.
v1
.
yifu
.
social
.
util
;
import
com.itextpdf.text.pdf.PdfWriter
;
import
com.aspose.words.Document
;
import
com.aspose.words.SaveFormat
;
import
com.yifu.cloud.plus.v1.yifu.common.core.constant.CommonConstants
;
import
com.yifu.cloud.plus.v1.yifu.common.core.util.Common
;
import
com.yifu.cloud.plus.v1.yifu.social.config.SocialFriendConfig
;
import
com.yifu.cloud.plus.v1.yifu.social.entity.TSocialFriendBackLog
;
import
org.apache.pdfbox.pdmodel.PDDocument
;
import
org.apache.pdfbox.rendering.ImageType
;
import
org.apache.pdfbox.rendering.PDFRenderer
;
import
org.apache.poi.xwpf.usermodel.*
;
import
org.jodconverter.core.office.OfficeException
;
import
org.jodconverter.core.office.OfficeManager
;
import
org.jodconverter.local.LocalConverter
;
import
org.jodconverter.local.office.LocalOfficeManager
;
import
org.springframework.stereotype.Component
;
import
org.springframework.web.client.RestTemplate
;
import
javax.annotation.PostConstruct
;
import
javax.imageio.ImageIO
;
import
java.awt.*
;
import
java.awt.image.BufferedImage
;
import
java.io.*
;
import
java.nio.file.DirectoryNotEmptyException
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.nio.file.Paths
;
import
java.util.Base64
;
import
java.util.HashMap
;
import
java.util.Map
;
/**
...
...
@@ -83,16 +72,6 @@ public class WordToImageUtil {
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
finally
{
Path
outputPath
=
Paths
.
get
(
outputFilePath
);
if
(
Files
.
exists
(
outputPath
))
{
try
{
Files
.
delete
(
outputPath
);
// 如果文件不存在会抛异常,先判断是否存在
}
catch
(
DirectoryNotEmptyException
e
)
{
e
.
printStackTrace
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
if
(
imageStream
!=
null
)
{
try
{
imageStream
.
close
();
...
...
@@ -104,48 +83,32 @@ public class WordToImageUtil {
return
"-"
;
}
/*private static OfficeManager officeManager;
@PostConstruct
public void init() {
String tempDir = System.getProperty("java.io.tmpdir");
String officeHome = tempDir + "officeManager";
officeManager = LocalOfficeManager.builder()
.officeHome(officeHome)
.portNumbers(2003)
.build();
try {
officeManager.start();
} catch (OfficeException e) {
e.printStackTrace();
}
}*/
public
String
convertAndAddSeal
(
String
inputDocx
,
InputStream
sealImagePath
,
String
outputImagePath
,
String
projectRoot
,
RestTemplate
restTemplate
,
SocialFriendConfig
socialFriendConfig
,
TSocialFriendBackLog
backLog
)
throws
IOException
,
Office
Exception
{
,
TSocialFriendBackLog
backLog
)
throws
Exception
{
File
pdfFile
=
null
;
Document
doc
=
null
;
File
tempImgFile
=
null
;
File
imgFile
=
null
;
try
{
// Step 1: Word 转为 PDF(中间格式)
/*pdfFile = new File(inputDocx.replace(".docx", ".pdf"));
Map<String, Object> pdfProps = new HashMap<>();
pdfProps.put("FilterName", "writer_pdf_Export");
pdfProps.put("SelectPdfVersion", 1); // PDF/A-1
LocalConverter.builder()
.officeManager(officeManager)
.storeProperties(pdfProps)
.build()
.convert(new File(inputDocx))
.to(pdfFile)
.execute();*/
convertDocxToPdf
(
inputDocx
,
PDF_TEMP
);
// Step 2: PDF 转为 PNG 图片(可使用 Apache PDFBox 或 ImageMagick 转换)
BufferedImage
pageImage
=
convertPdfToImage
(
pdfFile
.
getAbsolutePath
());
pdfFile
=
new
File
(
inputDocx
);
String
filename
=
pdfFile
.
getName
();
// 打开生成的 Word 文件
doc
=
new
Document
(
new
FileInputStream
(
pdfFile
));
String
pathPre
=
filename
.
substring
(
0
,
filename
.
lastIndexOf
(
"."
));
// 逐页将 Word 文件保存为图片(PNG格式)
String
path
=
pathPre
+
".png"
;
if
(
doc
.
getPageCount
()
>
0
)
{
Document
extractedPage
=
doc
.
extractPages
(
0
,
1
);
// 拼接上文件名 test1.png
// 将 Word 文件保存为图片PNG格式
extractedPage
.
save
(
path
,
SaveFormat
.
PNG
);
}
tempImgFile
=
new
File
(
path
);
BufferedImage
pageImage
=
ImageIO
.
read
(
tempImgFile
);
// Step 3: 加盖公章
BufferedImage
sealedImage
=
addSealToImage
(
pageImage
,
sealImagePath
);
...
...
@@ -167,30 +130,21 @@ public class WordToImageUtil {
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
finally
{
if
(
tempImgFile
!=
null
)
{
tempImgFile
.
delete
();
}
if
(
doc
!=
null
)
{
doc
.
cleanup
();
}
if
(
pdfFile
!=
null
)
{
pdfFile
.
delete
();
}
if
(
imgFile
!=
null
)
{
imgFile
.
delete
();
}
}
return
"---"
;
}
public
void
convertDocxToPdf
(
String
inputPath
,
String
outputPath
)
throws
Exception
{
FileInputStream
fis
=
new
FileInputStream
(
inputPath
);
XWPFDocument
document
=
new
XWPFDocument
(
fis
);
com
.
itextpdf
.
text
.
Document
pdfDoc
=
new
com
.
itextpdf
.
text
.
Document
();
PdfWriter
.
getInstance
(
pdfDoc
,
new
FileOutputStream
(
outputPath
));
pdfDoc
.
open
();
for
(
XWPFParagraph
para
:
document
.
getParagraphs
())
{
pdfDoc
.
add
(
new
com
.
itextpdf
.
text
.
Paragraph
(
para
.
getText
()));
}
pdfDoc
.
close
();
document
.
close
();
return
"---"
;
}
private
File
compressImage
(
String
projectRoot
,
File
originalFile
)
throws
IOException
{
...
...
@@ -211,30 +165,36 @@ public class WordToImageUtil {
return
originalFile
;
}
private
BufferedImage
convertPdfToImage
(
String
pdfPath
)
throws
IOException
{
try
(
PDDocument
document
=
PDDocument
.
load
(
new
File
(
pdfPath
)))
{
PDFRenderer
pdfRenderer
=
new
PDFRenderer
(
document
);
// 渲染第一页为图像,分辨率为 300 DPI
return
pdfRenderer
.
renderImageWithDPI
(
0
,
300
,
ImageType
.
RGB
);
}
}
private
BufferedImage
addSealToImage
(
BufferedImage
image
,
InputStream
sealImagePath
)
throws
IOException
{
BufferedImage
seal
=
ImageIO
.
read
(
sealImagePath
);
// 设置公章目标大小
int
targetWidth
=
200
;
// 宽度
int
targetHeight
=
200
;
// 高度
// 创建缩放后的公章图像
Image
scaledSeal
=
seal
.
getScaledInstance
(
targetWidth
,
targetHeight
,
Image
.
SCALE_SMOOTH
);
BufferedImage
resizedSeal
=
new
BufferedImage
(
targetWidth
,
targetHeight
,
BufferedImage
.
TYPE_INT_ARGB
);
Graphics2D
g2d
=
resizedSeal
.
createGraphics
();
g2d
.
setRenderingHint
(
RenderingHints
.
KEY_INTERPOLATION
,
RenderingHints
.
VALUE_INTERPOLATION_BILINEAR
);
g2d
.
drawImage
(
scaledSeal
,
0
,
0
,
null
);
g2d
.
dispose
();
// 合成最终图像
BufferedImage
combined
=
new
BufferedImage
(
image
.
getWidth
(),
image
.
getHeight
(),
BufferedImage
.
TYPE_INT_ARGB
);
Graphics2D
g
=
combined
.
createGraphics
();
g
.
setRenderingHint
(
RenderingHints
.
KEY_RENDERING
,
RenderingHints
.
VALUE_RENDER_QUALITY
);
g
.
drawImage
(
image
,
0
,
0
,
null
);
// 设置公章位置(右下角)
int
x
=
160
0
;
int
y
=
115
0
;
int
x
=
55
0
;
int
y
=
40
0
;
g
.
drawImage
(
s
eal
,
x
,
y
,
null
);
g
.
drawImage
(
resizedS
eal
,
x
,
y
,
null
);
g
.
dispose
();
return
combined
;
...
...
yifu-social/yifu-social-biz/src/main/java/com/yifu/cloud/plus/v1/yifu/social/util/WordToImageWithSeal.java
deleted
100644 → 0
View file @
7badd3a1
package
com
.
yifu
.
cloud
.
plus
.
v1
.
yifu
.
social
.
util
;
import
org.apache.pdfbox.pdmodel.PDDocument
;
import
org.apache.pdfbox.rendering.ImageType
;
import
org.apache.pdfbox.rendering.PDFRenderer
;
import
org.jodconverter.core.office.OfficeException
;
import
org.jodconverter.core.office.OfficeManager
;
import
org.jodconverter.local.LocalConverter
;
import
org.jodconverter.local.office.LocalOfficeManager
;
import
javax.imageio.ImageIO
;
import
java.awt.*
;
import
java.awt.image.BufferedImage
;
import
java.io.File
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.util.HashMap
;
import
java.util.Map
;
/**
* @author hgw2
* @description word转图片
* @date 2025/5/15
*/
public
class
WordToImageWithSeal
{
// 在生产环境中,建议在整个应用生命周期中共享一个OfficeManager:
public
static
OfficeManager
OFFICE_MANAGER
;
static
{
OFFICE_MANAGER
=
LocalOfficeManager
.
builder
()
.
portNumbers
(
2002
)
.
build
();
try
{
OFFICE_MANAGER
.
start
();
}
catch
(
OfficeException
e
)
{
e
.
printStackTrace
();
}
}
public
static
void
convertAndAddSeal
(
String
inputDocx
,
InputStream
sealImagePath
,
String
outputImagePath
)
throws
IOException
,
OfficeException
{
// Step 1: Word 转为 PDF(中间格式)
File
pdfFile
=
new
File
(
inputDocx
.
replace
(
".docx"
,
".pdf"
));
Map
<
String
,
Object
>
pdfProps
=
new
HashMap
<>();
pdfProps
.
put
(
"FilterName"
,
"writer_pdf_Export"
);
pdfProps
.
put
(
"SelectPdfVersion"
,
1
);
// PDF/A-1
LocalConverter
.
builder
()
.
officeManager
(
OFFICE_MANAGER
)
.
storeProperties
(
pdfProps
)
.
build
()
.
convert
(
new
File
(
inputDocx
))
.
to
(
pdfFile
)
.
execute
();
// Step 2: PDF 转为 PNG 图片(可使用 Apache PDFBox 或 ImageMagick 转换)
BufferedImage
pageImage
=
convertPdfToImage
(
pdfFile
.
getAbsolutePath
());
// Step 3: 加盖公章
BufferedImage
sealedImage
=
addSealToImage
(
pageImage
,
sealImagePath
);
// Step 4: 输出图片
ImageIO
.
write
(
sealedImage
,
"png"
,
new
File
(
outputImagePath
));
}
private
static
BufferedImage
convertPdfToImage
(
String
pdfPath
)
throws
IOException
{
try
(
PDDocument
document
=
PDDocument
.
load
(
new
File
(
pdfPath
)))
{
PDFRenderer
pdfRenderer
=
new
PDFRenderer
(
document
);
// 渲染第一页为图像,分辨率为 300 DPI
return
pdfRenderer
.
renderImageWithDPI
(
0
,
300
,
ImageType
.
RGB
);
}
}
private
static
BufferedImage
addSealToImage
(
BufferedImage
image
,
InputStream
sealImagePath
)
throws
IOException
{
BufferedImage
seal
=
ImageIO
.
read
(
sealImagePath
);
BufferedImage
combined
=
new
BufferedImage
(
image
.
getWidth
(),
image
.
getHeight
(),
BufferedImage
.
TYPE_INT_ARGB
);
Graphics2D
g
=
combined
.
createGraphics
();
g
.
drawImage
(
image
,
0
,
0
,
null
);
// 设置公章位置(右下角)
int
x
=
1600
;
int
y
=
1150
;
g
.
drawImage
(
seal
,
x
,
y
,
null
);
g
.
dispose
();
return
combined
;
}
}
yifu-social/yifu-social-biz/src/main/java/com/yifu/cloud/plus/v1/yifu/social/util/WordUtil.java
deleted
100644 → 0
View file @
7badd3a1
package
com
.
yifu
.
cloud
.
plus
.
v1
.
yifu
.
social
.
util
;
import
com.yifu.cloud.plus.v1.yifu.common.core.util.Common
;
import
org.apache.poi.xwpf.usermodel.*
;
import
java.io.FileInputStream
;
import
java.io.FileOutputStream
;
import
java.io.IOException
;
import
java.util.Map
;
/**
* @author hgw2
* @description word工具类
* @date 2025/5/15
*/
public
class
WordUtil
{
public
static
void
replaceTextInWord
(
String
filePath
,
String
outputFilePath
,
Map
<
String
,
String
>
replacements
)
throws
IOException
{
try
(
FileInputStream
fis
=
new
FileInputStream
(
filePath
);
XWPFDocument
document
=
new
XWPFDocument
(
fis
))
{
// 替换段落中的文本
for
(
XWPFParagraph
paragraph
:
document
.
getParagraphs
())
{
for
(
String
key
:
replacements
.
keySet
())
{
if
(
paragraph
.
getText
().
contains
(
key
))
{
String
newText
=
paragraph
.
getText
().
replace
(
key
,
replacements
.
get
(
key
));
paragraph
.
getRuns
().
clear
();
XWPFRun
newRun
=
paragraph
.
createRun
();
newRun
.
setText
(
newText
);
}
}
}
String
text
;
// 替换表格中的文本
for
(
XWPFTable
table
:
document
.
getTables
())
{
for
(
XWPFTableRow
row
:
table
.
getRows
())
{
for
(
XWPFTableCell
cell
:
row
.
getTableCells
())
{
for
(
XWPFParagraph
p
:
cell
.
getParagraphs
())
{
for
(
XWPFRun
run
:
p
.
getRuns
())
{
text
=
run
.
getText
(
0
);
if
(
Common
.
isNotNull
(
text
)
&&
replacements
.
get
(
text
)
!=
null
)
{
run
.
setText
(
replacements
.
get
(
text
),
0
);
}
}
}
}
}
}
// 保存输出文件
try
(
FileOutputStream
fos
=
new
FileOutputStream
(
outputFilePath
))
{
document
.
write
(
fos
);
}
}
}
}
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