Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
V
vehicle-quality-review
Project
Project
Details
Activity
Releases
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
王飞
vehicle-quality-review
Commits
f8714f4d
Commit
f8714f4d
authored
Jan 19, 2024
by
盖献康
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
调用PDF工具类方法(测试+修改)
parent
1a1b4234
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
119 additions
and
67 deletions
+119
-67
BookServiceImpl.java
...src/main/java/com/ruoyi/service/impl/BookServiceImpl.java
+86
-48
BookController.java
...ty-review/src/main/java/com/ruoyi/web/BookController.java
+3
-3
PdfBaseWriter.java
...n/src/main/java/com/ruoyi/common/utils/PdfBaseWriter.java
+30
-16
No files found.
quality-review/src/main/java/com/ruoyi/service/impl/BookServiceImpl.java
View file @
f8714f4d
package
com
.
ruoyi
.
service
.
impl
;
package
com
.
ruoyi
.
service
.
impl
;
import
cn.hutool.core.date.DateUtil
;
import
cn.hutool.core.lang.hash.Hash
;
import
com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
;
import
com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
;
import
com.google.common.collect.ArrayListMultimap
;
import
com.itextpdf.text.*
;
import
com.itextpdf.text.*
;
import
com.itextpdf.text.pdf.*
;
import
com.itextpdf.text.pdf.*
;
import
com.itextpdf.text.pdf.codec.Base64
;
import
com.itextpdf.text.pdf.codec.Base64
;
...
@@ -21,10 +24,8 @@ import javax.servlet.http.HttpServletResponse;
...
@@ -21,10 +24,8 @@ import javax.servlet.http.HttpServletResponse;
import
java.io.ByteArrayOutputStream
;
import
java.io.ByteArrayOutputStream
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.io.OutputStream
;
import
java.io.OutputStream
;
import
java.util.ArrayList
;
import
java.util.*
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.stream.IntStream
;
import
java.util.stream.IntStream
;
/**
/**
...
@@ -55,56 +56,93 @@ public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements Bo
...
@@ -55,56 +56,93 @@ public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements Bo
@Override
@Override
public
Document
generateItextPdfDocument
(
OutputStream
os
)
throws
Exception
{
public
Document
generateItextPdfDocument
(
OutputStream
os
)
throws
Exception
{
// document
// 创建一个A4大小的PDF
PdfBaseWriter
document
=
new
PdfBaseWriter
(
os
);
PdfBaseWriter
document
=
new
PdfBaseWriter
(
os
);
// Document document = new Document(PageSize.A4);
// PdfWriter.getInstance(document, os);
//
open
//
打开
document
.
open
();
document
.
open
();
// add content - pdf meta information
// PDF文档属性
document
.
addAuthor
(
"pdai"
);
document
.
addTitle
(
"标题(测试基本PDF导出)"
);
document
.
addAuthor
(
"作者"
);
document
.
addSubject
(
"主题(基本PDF导出)"
);
document
.
addKeywords
(
"关键字(基本PDF导出)"
);
document
.
addCreator
(
"谁创建的(xxx有限公司)"
);
document
.
addCreationDate
();
document
.
addCreationDate
();
document
.
addTitle
(
"pdai-pdf-itextpdf"
);
document
.
addKeywords
(
"pdf-pdai-keyword"
);
// 测试标题加文本内容
document
.
addCreator
(
"pdai"
);
// 添加第一个标题
Chapter
chapterOne
=
document
.
setTitle1
(
"模拟标题1"
,
1
);
// add content - page content
// 添加第一个标题下的第一个二级标题
document
.
setTitle2
(
chapterOne
,
"标题1-1"
);
// Title
// 添加内容
document
.
add
(
document
.
getParagraph
(
1
,
"JAVA全栈知识体系"
));
document
.
writeText
(
"石油是指气态、液态和固态的烃类混合物,具有天然的产状。石油又分为原油、天然气、天然气液及天然焦油等形式,但习惯上仍将“石油”作为“原油”的定义用。"
);
// 添加第一个标题下的第二个二级标题
// Chapter 1
document
.
setTitle2
(
chapterOne
,
"标题1-2"
);
Chapter
title1
=
document
.
setTitle1
(
"知识准备"
,
1
);
document
.
writeText
(
"石油是一种黏稠的、深褐色液体,被称为“工业的血液”。地壳上层部分地区有石油储存。主要成分是各种烷烃、环烷烃、芳香烃的混合物。是地质勘探的主要对象之一。"
);
document
.
setTitle2
(
title1
,
"什么是POI"
);
// 添加第二个标题
document
.
writeText
(
"Apache POI 是创建和维护操作各种符合Office Open XML(OOXML)标准和微软的OLE 2复合文档格式(OLE2)的Java API。用它可以使用Java读取和创建,修改MS Excel文件.而且,还可以使用Java读取和创建MS Word和MSPowerPoint文件。"
);
Chapter
chapterTwo
=
document
.
setTitle1
(
"模拟标题2"
,
2
);
document
.
setTitle2
(
title1
,
"POI中基础概念"
);
document
.
setTitle2
(
chapterTwo
,
"标题2-1"
);
document
.
writeText
(
"生成xls和xlsx有什么区别?POI对Excel中的对象的封装对应关系?"
);
document
.
writeText
(
"石油的成油机理有生物沉积变油和石化油两种学说,前者较广为接受,认为石油是古代海洋或湖泊中的生物经过漫长的演化形成,属于生物沉积变油,不可再生;后者认为石油是由地壳内本身的碳生成,与生物无关,可再生。石油主要被用来作为燃油和汽油,也是许多化学工业产品,如溶液、化肥、杀虫剂和塑料等的原料。"
);
document
.
setTitle2
(
chapterTwo
,
"标题2-2"
);
// Chapter 2
document
.
writeText
(
"2023年2月16日,中国石油和化学工业联合会发布数据称,中国炼油产能已超过美国,成为世界第一炼油大国。2023年2月16日,中国石油和化学工业联合会发布数据称,中国炼油产能已超过美国,成为世界第一炼油大国。"
);
Chapter
title2
=
document
.
setTitle1
(
"实现案例"
,
2
);
document
.
setTitle2
(
title2
,
"用户列表示例"
);
// 测试自定义段(可当标题、正文使用)
document
.
writeText
(
"以导出用户列表为例"
);
document
.
add
(
document
.
getParagraph
(
1
,
"Paragraph"
));
// 表格
// 测试自定义段方向
PdfPTable
table
=
new
PdfPTable
(
new
float
[]{
20
,
40
,
50
,
40
,
40
});
Paragraph
paragraph
=
document
.
getParagraph
(
2
,
"左对齐"
);
table
.
setTotalWidth
(
500
);
paragraph
.
setAlignment
(
Element
.
ALIGN_RIGHT
);
table
.
setLockedWidth
(
true
);
document
.
add
(
paragraph
);
table
.
setHorizontalAlignment
(
Element
.
ALIGN_CENTER
);
table
.
getDefaultCell
().
setBorder
(
1
);
// 测试(List)列
List
<
String
>
list
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
5
;
i
++)
{
list
.
add
(
"第一行"
);
table
.
addCell
(
document
.
createLeftCell
(
"1"
));
list
.
add
(
"第二行"
);
table
.
addCell
(
document
.
createLeftCell
(
"2"
));
list
.
add
(
"第三行"
);
table
.
addCell
(
document
.
createLeftCell
(
"3"
));
document
.
writeList
(
list
,
true
);
table
.
addCell
(
document
.
createLeftCell
(
"4"
));
table
.
addCell
(
document
.
createLeftCell
(
"5"
));
// 测试(Map)列
Map
<
String
,
String
>
map
=
new
HashMap
<>();
map
.
put
(
"甲方"
,
"可填入动态公司"
);
map
.
put
(
"乙方"
,
"可填入动态公司"
);
map
.
put
(
"时间"
,
String
.
valueOf
(
DateUtil
.
date
()));
map
.
put
(
"地点"
,
"可填入动态地点"
);
document
.
writeList
(
map
,
false
);
// 测试带表头的表格
ArrayList
<
LinkedHashMap
<
String
,
Object
>>
rows
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
10
;
i
++)
{
LinkedHashMap
<
String
,
Object
>
row
=
new
LinkedHashMap
<>();
row
.
put
(
"姓名"
,
"张三"
+
i
);
row
.
put
(
"年龄"
,
23
+
i
);
row
.
put
(
"成绩"
,
88.32
+
i
);
row
.
put
(
"是否合格"
,
true
);
row
.
put
(
"考试日期"
,
DateUtil
.
date
());
rows
.
add
(
row
);
}
}
document
.
writeTable
(
rows
);
// 测试自定义表格
PdfPTable
table
=
new
PdfPTable
(
new
float
[]{
40
,
150
});
table
.
setWidthPercentage
(
100
);
// 上下边距
table
.
setSpacingBefore
(
10
);
table
.
setSpacingAfter
(
10
);
document
.
writeCell
(
"电话:"
,
1
,
1
,
table
);
document
.
writeCell
(
"地址:"
,
1
,
1
,
table
);
document
.
writeCell
(
"手机号:"
,
1
,
1
,
table
);
// 一个cell图文混合
List
<
String
>
list2
=
new
ArrayList
<>();
list2
.
add
(
"哈哈哈"
);
list2
.
add
(
"C:/Users/gxk/Pictures/Saved Pictures/nvm.png"
);
list2
.
add
(
"啊啊啊"
);
list2
.
add
(
"C:/Users/gxk/Pictures/Saved Pictures/123.jpg"
);
document
.
writeTextAndImageCell
(
list2
,
table
);
// 把表格添加进入PDF
document
.
add
(
table
);
document
.
add
(
table
);
document
.
setTitle2
(
title2
,
"图片导出示例"
);
document
.
writeText
(
"以导出图片为例"
);
// 图片
// 图片
Image
image
=
Image
.
getInstance
(
"C:/Users/gxk/Pictures/Saved Pictures/nvm.png"
);
Image
image
=
Image
.
getInstance
(
"C:/Users/gxk/Pictures/Saved Pictures/nvm.png"
);
image
.
setAlignment
(
Element
.
ALIGN_CENTER
);
image
.
setAlignment
(
Element
.
ALIGN_CENTER
);
...
@@ -112,7 +150,7 @@ public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements Bo
...
@@ -112,7 +150,7 @@ public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements Bo
image
.
scalePercent
(
60
);
image
.
scalePercent
(
60
);
document
.
add
(
image
);
document
.
add
(
image
);
//
close
//
关闭
document
.
close
();
document
.
close
();
return
document
;
return
document
;
}
}
...
@@ -173,11 +211,11 @@ public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements Bo
...
@@ -173,11 +211,11 @@ public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements Bo
table
.
addCell
(
document
.
createCenterCell
(
"测试详情"
));
table
.
addCell
(
document
.
createCenterCell
(
"测试详情"
));
PdfPCell
cell
=
new
PdfPCell
();
PdfPCell
cell
=
new
PdfPCell
();
cell
.
addElement
(
document
.
createText
And
ImageCell
(
pdfTestVO
.
getTestDetails
()));
cell
.
addElement
(
document
.
createText
In
ImageCell
(
pdfTestVO
.
getTestDetails
()));
Image
img
=
Image
.
getInstance
(
"C:/Users/gxk/Pictures/Saved Pictures/nvm.png"
);
Image
img
=
Image
.
getInstance
(
"C:/Users/gxk/Pictures/Saved Pictures/nvm.png"
);
img
.
scaleToFit
(
100
,
100
);
img
.
scaleToFit
(
100
,
100
);
cell
.
addElement
(
img
);
cell
.
addElement
(
img
);
cell
.
addElement
(
document
.
createText
And
ImageCell
(
"车辆采用白名单策略,需客户端设置IP地址为192.168.69.71/24才可以与之通信,车辆IP地址为192.168.69.8。"
));
cell
.
addElement
(
document
.
createText
In
ImageCell
(
"车辆采用白名单策略,需客户端设置IP地址为192.168.69.71/24才可以与之通信,车辆IP地址为192.168.69.8。"
));
Image
imgTwo
=
Image
.
getInstance
(
"C:/Users/gxk/Pictures/Saved Pictures/123.jpg"
);
Image
imgTwo
=
Image
.
getInstance
(
"C:/Users/gxk/Pictures/Saved Pictures/123.jpg"
);
img
.
scaleToFit
(
100
,
100
);
img
.
scaleToFit
(
100
,
100
);
cell
.
addElement
(
imgTwo
);
cell
.
addElement
(
imgTwo
);
...
...
quality-review/src/main/java/com/ruoyi/web/BookController.java
View file @
f8714f4d
...
@@ -84,12 +84,12 @@ public class BookController extends BaseController {
...
@@ -84,12 +84,12 @@ public class BookController extends BaseController {
public
void
download
(
HttpServletResponse
response
)
{
public
void
download
(
HttpServletResponse
response
)
{
response
.
setHeader
(
"content-disposition"
,
"attachment;fileName="
+
"ReceiptPrinter.pdf"
);
response
.
setHeader
(
"content-disposition"
,
"attachment;fileName="
+
"ReceiptPrinter.pdf"
);
try
{
try
{
// demo-API
选软
// demo-API
渲染
//
bookService.generateItextPdfDocument(response.getOutputStream());
bookService
.
generateItextPdfDocument
(
response
.
getOutputStream
());
// demo-模板
// demo-模板
// bookService.generateTempPDF(response);
// bookService.generateTempPDF(response);
// demo-动态表格
// demo-动态表格
bookService
.
testGeneratePDF
(
response
.
getOutputStream
());
//
bookService.testGeneratePDF(response.getOutputStream());
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
e
.
printStackTrace
();
}
}
...
...
ruoyi-common/src/main/java/com/ruoyi/common/utils/PdfBaseWriter.java
View file @
f8714f4d
package
com
.
ruoyi
.
common
.
utils
;
package
com
.
ruoyi
.
common
.
utils
;
import
com.google.common.collect.ListMultimap
;
import
com.itextpdf.text.*
;
import
com.itextpdf.text.*
;
import
com.itextpdf.text.pdf.BaseFont
;
import
com.itextpdf.text.pdf.BaseFont
;
import
com.itextpdf.text.pdf.PdfPCell
;
import
com.itextpdf.text.pdf.PdfPCell
;
...
@@ -8,10 +9,8 @@ import com.itextpdf.text.pdf.PdfWriter;
...
@@ -8,10 +9,8 @@ import com.itextpdf.text.pdf.PdfWriter;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.io.OutputStream
;
import
java.io.OutputStream
;
import
java.util.ArrayList
;
import
java.util.*
;
import
java.util.LinkedHashMap
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.regex.Matcher
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
import
java.util.regex.Pattern
;
...
@@ -265,34 +264,49 @@ public class PdfBaseWriter extends Document{
...
@@ -265,34 +264,49 @@ public class PdfBaseWriter extends Document{
cell
.
setBorderColor
(
GREY_COLOR
);
cell
.
setBorderColor
(
GREY_COLOR
);
return
cell
;
return
cell
;
}
}
/**
/**
* 创建内容为图片的单元格 {@link PdfPCell}
* 创建内容为图片的单元格 {@link PdfPCell}
* @param bgColor 背景 {@link BaseColor}
* @param border 边框 {@link Rectangle}
* @param align 对齐方式 {@link Element}
* @param colspan 所占列数
* @param colspan 所占列数
* @param rowspan 所占行数
* @param rowspan 所占行数
* @param image 内容(文字或图片对象) {@link Image}
* @param image 内容(文字或图片对象) {@link Image}
* @param fitWidth 宽度自适应
* @param fitHeight 高度自适应
* @return 单元格 {@link PdfPCell}
* @return 单元格 {@link PdfPCell}
*/
*/
public
PdfPCell
newPdfPCellOfImage
(
BaseColor
bgColor
,
int
border
,
int
align
,
int
colspan
,
int
rowspan
,
Image
image
)
{
public
PdfPCell
newPdfPCellOfImage
(
int
colspan
,
int
rowspan
,
Image
image
,
float
fitWidth
,
float
fitHeight
)
{
PdfPCell
cell
=
new
PdfPCell
();
PdfPCell
cell
=
new
PdfPCell
();
cell
.
setBorderColor
(
BaseColor
.
BLACK
);
cell
.
setBorderColorLeft
(
BaseColor
.
BLACK
);
cell
.
setBorderColorRight
(
BaseColor
.
BLACK
);
cell
.
setBackgroundColor
(
bgColor
);
cell
.
setBorder
(
border
);
cell
.
setUseAscender
(
Boolean
.
TRUE
);
cell
.
setUseAscender
(
Boolean
.
TRUE
);
cell
.
setVerticalAlignment
(
PdfPCell
.
ALIGN_MIDDLE
);
cell
.
setVerticalAlignment
(
PdfPCell
.
ALIGN_MIDDLE
);
cell
.
setHorizontalAlignment
(
align
);
cell
.
setBorder
(
Rectangle
.
BOX
);
cell
.
setBorderColor
(
BaseColor
.
GRAY
);
cell
.
setBackgroundColor
(
BaseColor
.
WHITE
);
cell
.
setColspan
(
colspan
);
cell
.
setColspan
(
colspan
);
cell
.
setRowspan
(
rowspan
);
cell
.
setRowspan
(
rowspan
);
image
.
scaleToFit
(
fitWidth
,
fitHeight
);
cell
.
addElement
(
image
);
cell
.
addElement
(
image
);
return
cell
;
return
cell
;
}
}
/**
* 把图文混合写入单元格
* @param list
* @param table
* @return
*/
public
PdfPCell
writeTextAndImageCell
(
List
<
String
>
list
,
PdfPTable
table
)
throws
DocumentException
,
IOException
{
PdfPCell
cell
=
new
PdfPCell
();
cell
.
setBorderColor
(
BaseColor
.
GRAY
);
for
(
String
value
:
list
)
{
if
(
value
.
contains
(
"/"
)
&&
(
value
.
endsWith
(
".jpg"
)
||
value
.
endsWith
(
".png"
)
||
value
.
endsWith
(
".jpeg"
)))
{
Image
image
=
Image
.
getInstance
(
value
);
cell
.
addElement
(
image
);
}
else
{
cell
.
addElement
(
createTextInImageCell
(
value
));
}
}
table
.
addCell
(
cell
);
return
cell
;
}
//------------------------------------文本内容------------------------------------
//------------------------------------文本内容------------------------------------
...
@@ -588,7 +602,7 @@ public class PdfBaseWriter extends Document{
...
@@ -588,7 +602,7 @@ public class PdfBaseWriter extends Document{
* @throws IOException
* @throws IOException
* @throws DocumentException
* @throws DocumentException
*/
*/
public
Phrase
createText
And
ImageCell
(
String
content
)
throws
IOException
,
DocumentException
{
public
Phrase
createText
In
ImageCell
(
String
content
)
throws
IOException
,
DocumentException
{
return
new
Phrase
(
content
,
defaultFont
());
return
new
Phrase
(
content
,
defaultFont
());
}
}
...
...
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