itext生成pdf

itext生成pdf

最近写了一个java后端生成pdf程序,因此我在这里提供自己生成pdf的一个策略。

使用itext生成pdf

iText的依赖

1
2
3
4
5
6
7
8
9
10
11
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.6</version>
</dependency>

<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>

这两程序是都需要的,特别是第二个依赖,是itext能够使用字体的重要的依赖

代码实践

学习参考

iText中文帮助文档 - 豆丁网 (docin.com)

生成pdf的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
package com.dwx.utils;

import com.dwx.pojo.g_hld1;
import com.dwx.pojo.g_hld2;
import com.itextpdf.text.*;
import com.itextpdf.text.Font;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;

import javax.swing.*;
import java.awt.*;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

//这是一个生成pdf的工具类
public class pdfUtils {

public Map<String,Object> setPDF(String E,String I,String S,String N,String TT,String F,String J,String P,String csrul,String personalityProfile,String personalityDescription,String field,String careerStudent,String career) throws Exception{

ByteArrayOutputStream stream = new ByteArrayOutputStream();
//创建一个新的文档
//创建一个空的文档并且设置文档的大小
Document document = new Document(PageSize.A4);

PdfWriter.getInstance(document, stream);


//打开文档 准备添加数据
document.open();

//添加数据
//创建字体 基本字体 字体需要不同定制都是以这个基础字体为基础的
BaseFont baseFont = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
//固定格式部分
//主标题
String T = "MBTI职业性格测试报告";
Paragraph Title = new Paragraph();
Title.setFont(new Font(baseFont,23,0));
//居中
Title.setAlignment(Element.ALIGN_CENTER);
Title.add(T);

Title.setSpacingAfter(25);
Title.setSpacingBefore(25);

//副标题
String t = "你的性格类型是:"+csrul;
Paragraph title = new Paragraph();
title.setFont(new Font(baseFont,12,0));
//居中
title.setAlignment(Element.ALIGN_CENTER);
title.add(t);
title.setSpacingAfter(10);
title.setSpacingBefore(0);

//添加百分比部分
String TT1 = E+"% 外向(E) ---- 内向(I) "+I+"%";
String TT2 = S+"% 感觉(S) ---- 直觉(N) "+N+"%";
String TT3 = TT+"% 思考(T) ---- 感情(F) "+F+"%";
String TT4 = J+"% 判断(J) ---- 知觉(P) "+P+"%";

Paragraph tt1 = new Paragraph();
tt1.setFont(new Font(baseFont,10,0));
tt1.add(TT1);
tt1.setAlignment(Element.ALIGN_CENTER);
Paragraph tt2 = new Paragraph();
tt2.setFont(new Font(baseFont,10,0));
tt2.add(TT2);
tt2.setAlignment(Element.ALIGN_CENTER);
Paragraph tt3 = new Paragraph();
tt3.setFont(new Font(baseFont,10,0));
tt3.add(TT3);
tt3.setAlignment(Element.ALIGN_CENTER);
Paragraph tt4 = new Paragraph();
tt4.setFont(new Font(baseFont,10,0));
tt4.add(TT4);
tt4.setAlignment(Element.ALIGN_CENTER);




//第一部分
String t1 = "一、性格特点";
Paragraph T1 = new Paragraph();
T1.setFont(new Font(baseFont,15,0));
T1.add(t1);
T1.setSpacingAfter(10);
T1.setSpacingBefore(10);

//第二部分

String t2 = "1.概要";
Paragraph T2 = new Paragraph();
T2.setFont(new Font(baseFont,13,0));
T2.add(t2);
T2.setSpacingAfter(10);
T2.setSpacingBefore(10);

//第三部分
String t3 = "2.描述";
Paragraph T3 = new Paragraph();
T3.setFont(new Font(baseFont,13,0));
T3.add(t3);
T3.setSpacingAfter(10);
T3.setSpacingBefore(10);

//第四部分
String t4 = "二、适合领域";
Paragraph T4 = new Paragraph();
T4.setFont(new Font(baseFont,15,0));
T4.add(t4);
T4.setSpacingAfter(10);
T4.setSpacingBefore(10);

String t45 = "三、适合职业";
Paragraph T45 = new Paragraph();
T45.setFont(new Font(baseFont,15,0));
T45.add(t45);
T45.setSpacingAfter(10);
T45.setSpacingBefore(10);


//第五部分
String t5 = "1.学生";
Paragraph T5 = new Paragraph();
T5.setFont(new Font(baseFont,13,0));
T5.add(t5);

T5.setSpacingAfter(10);
T5.setSpacingBefore(10);

//第六部分
String t6 = "2.职场";
Paragraph T6 = new Paragraph();
T6.setFont(new Font(baseFont,13,0));
T6.add(t6);

T6.setSpacingAfter(10);
T6.setSpacingBefore(10);


//第七部分
String t7 = "四、用户须知";
Paragraph T7 = new Paragraph();
T7.setFont(new Font(baseFont,15,0));
T7.add(t7);

T7.setSpacingAfter(10);
T7.setSpacingBefore(10);


//第八部分
String p6 = "《性格分析报告》展示的是你的性格倾向,而不是你的知识、技能、经验。\n" +
"MBTI提供的性格类型描述仅供测试者确定自己的性格类型之用,性格类型没有好坏,只有不同。每一种性格特征都有其价值和优点,也有缺点和需要注意的地方。清楚地了解自己的性格优劣势,有利于更好地发挥自己的特长,而尽可能的在为人处事中避免自己性格中的劣势,更好地和他人相处,更好地作重要的决策。\n" +
"";
Paragraph P6 = new Paragraph();
P6.setFont(new Font(baseFont,10,0));
P6.add(p6);
P6.setLeading(15.5f);

//尾缀一
String l1 = "[性格无好坏,人生各不同]";
Paragraph L1 = new Paragraph();
L1.setFont(new Font(baseFont,10,0));
L1.setSpacingBefore(50);
//设置居中
L1.setAlignment(Element.ALIGN_RIGHT);
L1.add(l1);
//尾缀二
String l2 = "留心智愿";
Paragraph L2 = new Paragraph();
L2.setFont(new Font(baseFont,10,0));
//设置居中
L2.setAlignment(Element.ALIGN_RIGHT);
L2.add(l2);
//最后的时间

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu/MM/dd");
LocalDate localDate = LocalDate.now();
int year = localDate.getYear();
int monthValue = localDate.getMonthValue();
int dayOfMonth = localDate.getDayOfMonth();
System.out.println(year);
System.out.println(monthValue );
System.out.println(dayOfMonth);

String data = year+"年"+monthValue+"月"+dayOfMonth+"日";
Paragraph Data = new Paragraph();
Data.setAlignment(Element.ALIGN_RIGHT);
Data.setFont(new Font(baseFont,10,0));
Data.add(data);



//五个移动的数据
String p1 = personalityProfile;
String p2 = personalityDescription;
String p3 = field;
String p4 = careerStudent;
String p5 = career;

Paragraph P1 = new Paragraph();
P1.setFont(new Font(baseFont,10,0));
P1.add(p1);

Paragraph P2 = new Paragraph();
P2.setFont(new Font(baseFont,10,0));
P2.add(p2);

Paragraph P3 = new Paragraph();
P3.setFont(new Font(baseFont,10,0));
P3.add(p3);

Paragraph P4 = new Paragraph();
P4.setFont(new Font(baseFont,10,0));
P4.add(p4);

Paragraph P5 = new Paragraph();
P5.setFont(new Font(baseFont,10,0));
P5.add(p5);




//将元素添加进去
document.add(Title);
document.add(title);
document.add(tt1);
document.add(tt2);
document.add(tt3);
document.add(tt4);
document.add(T1);
document.add(T2);
document.add(P1);
document.add(T3);
document.add(P2);
document.add(T4);
document.add(P3);
document.add(T45);
document.add(T5);
document.add(P4);
document.add(T6);
document.add(P5);
document.add(T7);
document.add(P6);
document.add(L1);
document.add(L2);
document.add(Data);




//关闭文档
document.close();

Map<String, Object> map = new HashMap<>();

map.put("document",document);
map.put("stream",stream);
return map;
}


public Map<String,Object> setHldPDF(String R, String C, String E, String S, String A, String I, String d1, String d2, String d3, g_hld1 n1, g_hld1 n2, g_hld1 n3, g_hld2 d) throws Exception {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
//创建一个新的文档
//创建一个空的文档并且设置文档的大小
Document document = new Document(PageSize.A4);

PdfWriter.getInstance(document, stream);


//打开文档 准备添加数据
document.open();

//添加数据
//创建字体 基本字体 字体需要不同定制都是以这个基础字体为基础的
BaseFont baseFont = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);

//创建标题
String title = "霍兰德职业兴趣测试\n"+"深度分析报告";
Paragraph Title = new Paragraph();
Title.setFont(new Font(baseFont,20,0));
//设置居中
Title.setAlignment(Element.ALIGN_CENTER);
Title.setLeading((float) (15.5f*1.5));
Title.add(title);
Title.setSpacingAfter(20);

//创建文档中固定的部分
String t1 = "测试结果 "+d.getAll_type();
Paragraph T1 = new Paragraph();
T1.setFont(new Font(baseFont,15));
T1.add(t1);
T1.setSpacingBefore(10);
T1.setSpacingAfter(10);

String t2 = "维度分析";
Paragraph T2 = new Paragraph();
T2.setFont(new Font(baseFont,15));
T2.add(t2);
T2.setAlignment(Element.ALIGN_CENTER);
T2.setSpacingBefore(10);
T2.setSpacingAfter(10);


String t3 = "职业匹配";
Paragraph T3 = new Paragraph();
T3.setFont(new Font(baseFont,15));
//设置居中
T3.setAlignment(Element.ALIGN_CENTER);
T3.add(t3);
T3.setSpacingBefore(10);
T3.setSpacingAfter(10);

//设置可以活动的部分
String p6 = d.getAll_career();
Paragraph P6 = new Paragraph();
P6.setFont(new Font(baseFont,10));
P6.add(p6);

String p1 = "现实型R ---- "+R+"%"+"\n"
+"传统型C ---- "+C+"%"+"\n"
+"企业型E ---- "+E+"%"+"\n"
+"社会型S ---- "+S+"%"+"\n"
+"艺术型A ---- "+A+"%"+"\n"
+"研究型I ---- "+I+"%"+"\n";
Paragraph P1 = new Paragraph();
P1.setFont(new Font(baseFont,13));
P1.add(p1);
P1.setAlignment(Element.ALIGN_CENTER);
P1.setLeading((float) (15.5f*1.5));

P1.setSpacingBefore(10);
P1.setSpacingAfter(10);

String t4 = d1;
switch (t4){
case "R现实型": t4 = "R";break;
case "C": t4 = "C";break;
case "E": t4 = "E";break;
case "S": t4 = "S";break;
case "A": t4 = "A";break;
case "I": t4 = "I";break;
default:break;
}
Paragraph T4 = new Paragraph();
T4.add(t4);
T4.setFont(new Font(baseFont,15));
String p3 = n1.getPersonality()+"\n"+"\n"+n1.getCareer();
Paragraph P3 = new Paragraph();
P3.setFont(new Font(baseFont,10));
P3.add(p3);
P3.setSpacingAfter(10);
P3.setSpacingBefore(10);


String t5 = d2;
switch (t5){
case "R现实型": t4 = "R"+"现实性";break;
case "C": t4 = "C";break;
case "E": t4 = "E";break;
case "S": t4 = "S";break;
case "A": t4 = "A";break;
case "I": t4 = "I";break;
default:break;
}
Paragraph T5 = new Paragraph();
T5.add(t5);
T5.setFont(new Font(baseFont,15));
String p4 = n2.getPersonality()+"\n"+"\n"+n2.getCareer();
Paragraph P4 = new Paragraph();
P4.setFont(new Font(baseFont,10));
P4.add(p4);
P4.setSpacingAfter(10);
P4.setSpacingBefore(10);


String t6 = d3;
switch (t6){
case "R现实型": t4 = "R"+"现实性";break;
case "C": t4 = "C";break;
case "E": t4 = "E";break;
case "S": t4 = "S";break;
case "A": t4 = "A";break;
case "I": t4 = "I";break;
default:break;
}
Paragraph T6 = new Paragraph();
T6.add(t6);
T6.setFont(new Font(baseFont,15));


String p5 = n3.getPersonality()+"\n"+"\n"+n3.getCareer();
Paragraph P5 = new Paragraph();
P5.setFont(new Font(baseFont,10));
P5.add(p5);
P5.setSpacingAfter(10);
P5.setSpacingBefore(10);


//尾缀一
String l1 = "[兴趣无好坏,人生各不同]";
Paragraph L1 = new Paragraph();
L1.setFont(new Font(baseFont,10,0));
L1.setSpacingBefore(50);
//设置居中
L1.setAlignment(Element.ALIGN_RIGHT);
L1.add(l1);
//尾缀二
String l2 = "留心智愿";
Paragraph L2 = new Paragraph();
L2.setFont(new Font(baseFont,10,0));
//设置居中
L2.setAlignment(Element.ALIGN_RIGHT);
L2.add(l2);
//最后的时间

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu/MM/dd");
LocalDate localDate = LocalDate.now();
int year = localDate.getYear();
int monthValue = localDate.getMonthValue();
int dayOfMonth = localDate.getDayOfMonth();
System.out.println(year);
System.out.println(monthValue );
System.out.println(dayOfMonth);

String data = year+"年"+monthValue+"月"+dayOfMonth+"日";
Paragraph Data = new Paragraph();
Data.setAlignment(Element.ALIGN_RIGHT);
Data.setFont(new Font(baseFont,10,0));
Data.add(data);




//将片段加到文档中
document.add(Title);
document.add(P1);
document.add(T1);

document.add(T2);
document.add(T4);
document.add(P3);

document.add(T5);
document.add(P4);

document.add(T6);
document.add(P5);

document.add(T3);
document.add(P6);

document.add(L1);
document.add(L2);
document.add(Data);

document.close();

Map<String, Object> map = new HashMap<>();

map.put("document",document);
map.put("stream",stream);
return map;
}


}

将pdf写到网页的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package com.dwx.controller;

import com.dwx.pojo.g_hld1;
import com.dwx.pojo.g_hld2;
import com.dwx.pojo.g_report;
import com.dwx.service.hld1Service;
import com.dwx.service.hld2Service;
import com.dwx.service.reportService;
import com.dwx.utils.pdfUtils;
import com.itextpdf.text.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;

import java.nio.charset.StandardCharsets;
import java.util.Map;

@Controller
public class ReportController {

//嗲用service层
@Autowired
private reportService reportService;

//调用hld1的service和hld2的service
@Autowired
private hld1Service hld1Service;
@Autowired
private hld2Service hld2Service;


@RequestMapping("/getPDF")
@ResponseBody
public String getPdf(String E,String I,String S,String N,String T,String F,String J,String P,String csrul,HttpServletResponse resp) throws Exception {
//设置下载pdf程序
System.out.println(csrul);
g_report report = reportService.getReport(csrul);
System.out.println(report);

System.out.println(E);
System.out.println(I);
System.out.println(S);
System.out.println(N);
System.out.println(T);
System.out.println(F);
System.out.println(J);
System.out.println(P);

//首先创建一个输出流

//获得我们生成的pdf文档
pdfUtils utils = new pdfUtils();
String profile = report.getPersonalityProfile();
String description = report.getPersonalityDescription();
String career = report.getCareer();
String student = report.getCareerStudent();
String field = report.getField();
Map<String, Object> map = utils.setPDF(E,I,S,N,T,F,J,P,csrul, profile,description,field,student,career );

Document document = (Document) map.get("document");
ByteArrayOutputStream stream = (ByteArrayOutputStream) map.get("stream");


//转化成流 直接输出
resp.setHeader("Content-Disposition", "attachment; filename=" + new String((csrul + ".pdf").getBytes(), StandardCharsets.ISO_8859_1));
resp.setContentLength(stream.size());

ServletOutputStream streamOut = resp.getOutputStream();

stream.writeTo(streamOut);


return "ok";
}


@RequestMapping("/getHldPDF")
@ResponseBody
public String getHldPDF(String R,String C,String E,String S,String A,String I,String d1,String d2,String d3,String dimension,HttpServletResponse resp) throws Exception{


switch (d1){
case "R": d1 = "R现实型";break;
case "C": d1 = "C传统型";break;
case "E": d1 = "E管理型";break;
case "S": d1 = "S社会型";break;
case "A": d1 = "A艺术型";break;
case "I": d1 = "I研究型";break;
default:break;
}
switch (d2){
case "R": d2 = "R现实型";break;
case "C": d2 = "C传统型";break;
case "E": d2 = "E管理型";break;
case "S": d2 = "S社会型";break;
case "A": d2 = "A艺术型";break;
case "I": d2 = "I研究型";break;
default:break;
}
switch (d3){
case "R": d3 = "R现实型";break;
case "C": d3 = "C传统型";break;
case "E": d3 = "E管理型";break;
case "S": d3 = "S社会型";break;
case "A": d3 = "A艺术型";break;
case "I": d3 = "I研究型";break;
default:break;
}
//通过service进行分别的查询
g_hld1 hld1 = hld1Service.getHld1(d1);
System.out.println(hld1);
g_hld1 hld11 = hld1Service.getHld1(d2);
System.out.println(hld11);
g_hld1 hld12 = hld1Service.getHld1(d3);
System.out.println(hld12);

//查询hld2的数据
g_hld2 hld2 = hld2Service.getHld2(dimension);
System.out.println(hld2);


pdfUtils utils = new pdfUtils();
Map<String, Object> map = utils.setHldPDF(R, C, E, S, A, I, d1, d2, d3, hld1, hld11, hld12, hld2);

Document document = (Document) map.get("document");
ByteArrayOutputStream stream = (ByteArrayOutputStream) map.get("stream");


//转化成流 直接输出
resp.setHeader("Content-Disposition", "attachment; filename=" + new String((dimension + ".pdf").getBytes(), StandardCharsets.ISO_8859_1));
resp.setContentLength(stream.size());

ServletOutputStream streamOut = resp.getOutputStream();

stream.writeTo(streamOut);


return "ok";
}



}

代码的细小但重要的代码

1
2
3
4
5
6
7

ByteArrayOutputStream stream = new ByteArrayOutputStream();
//创建一个新的文档
//创建一个空的文档并且设置文档的大小
Document document = new Document(PageSize.A4);

PdfWriter.getInstance(document, stream);

注意这里有个stream流,将pdf转化成流

在这个程序流是在写到网页的时候所需要的

1
2
3
4
5
6
7
8
9
10
11
12
Document document = (Document) map.get("document");
ByteArrayOutputStream stream = (ByteArrayOutputStream) map.get("stream");


//转化成流 直接输出
resp.setHeader("Content-Disposition", "attachment; filename=" + new String((dimension + ".pdf").getBytes(), StandardCharsets.ISO_8859_1));
resp.setContentLength(stream.size());

ServletOutputStream streamOut = resp.getOutputStream();

stream.writeTo(streamOut);

这里就拿到了util方法生成的流和文档 将这个流直接通过springMVC中的HttpService直接将我们的流写到网页上生成文档。

Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

扫一扫,分享到微信

微信分享二维码
  • Copyrights © 2015-2023 dwx
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信