文本记录Java使用iText7生成PDF
iText 7是iText强大的PDF工具包的最新版本,用于PDF生成,PDF编程,处理和操作,如数字签名等
官方文档:https://kb.itextpdf.com/home/it7kb/ebooks

简单生成PDF

官方文档:https://kb.itextpdf.com/home/it7kb/ebooks/itext-7-jump-start-tutorial-for-java
根据文档说明,我们引入依赖

  1. <properties>
  2. <!-- 省略其他部分...-->
  3. <itext7.version>7.1.7</itext7.version>
  4. </properties>
  5. <dependencies>
  6. <!-- 省略其他部分...-->
  7. <!-- itextpdf -->
  8. <dependency>
  9. <groupId>com.itextpdf</groupId>
  10. <artifactId>kernel</artifactId>
  11. <version>${itext7.version}</version>
  12. </dependency>
  13. <dependency>
  14. <groupId>com.itextpdf</groupId>
  15. <artifactId>io</artifactId>
  16. <version>${itext7.version}</version>
  17. </dependency>
  18. <dependency>
  19. <groupId>com.itextpdf</groupId>
  20. <artifactId>layout</artifactId>
  21. <version>${itext7.version}</version>
  22. </dependency>
  23. <dependency>
  24. <groupId>com.itextpdf</groupId>
  25. <artifactId>forms</artifactId>
  26. <version>${itext7.version}</version>
  27. </dependency>
  28. <dependency>
  29. <groupId>com.itextpdf</groupId>
  30. <artifactId>pdfa</artifactId>
  31. <version>${itext7.version}</version>
  32. </dependency>
  33. </dependencies>

代码

  1. <properties>
  2. <!-- 省略其他部分...-->
  3. <itext7.version>7.1.7</itext7.version>
  4. </properties>
  5. <dependencies>
  6. <!-- 省略其他部分...-->
  7. <!-- itextpdf -->
  8. <dependency>
  9. <groupId>com.itextpdf</groupId>
  10. <artifactId>kernel</artifactId>
  11. <version>${itext7.version}</version>
  12. </dependency>
  13. <dependency>
  14. <groupId>com.itextpdf</groupId>
  15. <artifactId>io</artifactId>
  16. <version>${itext7.version}</version>
  17. </dependency>
  18. <dependency>
  19. <groupId>com.itextpdf</groupId>
  20. <artifactId>layout</artifactId>
  21. <version>${itext7.version}</version>
  22. </dependency>
  23. <dependency>
  24. <groupId>com.itextpdf</groupId>
  25. <artifactId>forms</artifactId>
  26. <version>${itext7.version}</version>
  27. </dependency>
  28. <dependency>
  29. <groupId>com.itextpdf</groupId>
  30. <artifactId>pdfa</artifactId>
  31. <version>${itext7.version}</version>
  32. </dependency>
  33. </dependencies>

效果

image.png

生成的PDF

image.png
文档属性
image.png

HTML转PDF

官方文档:https://kb.itextpdf.com/home/it7kb/ebooks/itext-7-converting-html-to-pdf-with-pdfhtml
pdfHTML是iText 7的一个附加组件,需要添加依赖

  1. <properties>
  2. <!-- 省略其他部分...-->
  3. <itext7.html2pdf.version>2.1.4</itext7.html2pdf.version>
  4. </properties>
  5. <dependencies>
  6. <!-- 省略其他部分...-->
  7. <!-- itextpdf html2pdf组件-->
  8. <dependency>
  9. <groupId>com.itextpdf</groupId>
  10. <artifactId>html2pdf</artifactId>
  11. <version>${itext7.html2pdf.version}</version>
  12. </dependency>
  13. </dependencies>

代码

代码同上,仅main测试函数不同!

  1. //测试
  2. public static void main(String[] args) {
  3. //test();
  4. html2pdf();
  5. }

PS:html内容往下拉

效果

image.png
html页面

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>简单简历</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <meta http-equiv="cache-control" content="no-cache, no-store, must-revalidate"/>
  7. <meta http-equiv="pragma" content="no-cache"/>
  8. <meta http-equiv="expires" content="0"/>
  9. <!-- 自定义样式 -->
  10. <style>
  11. *{
  12. font-family: "Helvetica Neue",Helvetica,"PingFang SC","Hiragino Sans GB","Microsoft YaHei","微软雅黑",Arial,sans-serif;
  13. }
  14. body {
  15. margin: 0;
  16. padding: 0;
  17. }
  18. a {
  19. text-decoration: none;
  20. padding: 0;
  21. margin: 5px 0;
  22. color: black;
  23. }
  24. a:hover {
  25. color: #5c8dff;
  26. }
  27. b{
  28. margin: 0 10px;
  29. }
  30. /* 主体 */
  31. .main {
  32. /*margin: 0 auto;*/
  33. /*width: 770px;*/
  34. /*box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);*/
  35. /*border: 1px solid #dad8d8;*/
  36. /*background: #fbfbfb;*/
  37. }
  38. /* 1级标题 */
  39. h3.list1 {
  40. color: #409EFF;
  41. border-bottom: 1px solid #409EFF;
  42. padding: 5px;
  43. margin: 50px 0 0 0;
  44. }
  45. /* 大模块 */
  46. .block,.block1 {
  47. /*background: #efefef;*/
  48. /*border: 1px solid #dad8d8;*/
  49. margin: 0 0 20px 0;
  50. padding: 0 10px;
  51. }
  52. .block,.block1 p {
  53. text-indent:2em;
  54. }
  55. .block:hover {
  56. background: #eeeeee;
  57. }
  58. .block1:hover {
  59. background: #cecece;
  60. }
  61. .button-list{
  62. text-align: center;
  63. margin: 20px auto;
  64. padding: 10px;
  65. width: 1024px;
  66. }
  67. </style>
  68. </head>
  69. <body>
  70. <!-- 简历主体 -->
  71. <div id="body" class="main">
  72. <div class="section">
  73. <div class="module">
  74. <h3 class="list1" style="margin: 0;">基本信息</h3>
  75. <div class="block">
  76. <p>XXX<b>/</b>男<b>/</b>25岁</p>
  77. <p>本科<b>/</b>XX学校<b>/</b>XX专业<b>/</b>2014-2018</p>
  78. <p>工龄:X年</p>
  79. <p>手机:XXXXXXXXXX</p>
  80. <p>邮箱:XXXXX@qq.com</p>
  81. <p>GitHub:<a href="https://github.com/huanzi-qch">https://github.com/huanzi-qch</a></p>
  82. <p>博客园:<a href="https://www.cnblogs.com/huanzi-qch">https://www.cnblogs.com/huanzi-qch</a></p>
  83. <br/>
  84. <p>求职岗位:Java开发<b>/</b>目标城市:南宁市<b>/</b>期望薪资:面议</p>
  85. <p>注:已离职,一个月可到岗</p>
  86. <img style="position: relative;top: -350px;left: 500px;width: 100px;height: 130px;" src="logo.png"/>
  87. </div>
  88. </div>
  89. <div class="module">
  90. <h3 class="list1">技能清单</h3>
  91. <div class="block">
  92. <p>熟悉XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX。</p>
  93. <p>熟悉XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX。</p>
  94. <p>熟悉XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX。</p>
  95. <p>熟悉XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX。</p>
  96. <p>熟悉XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX。</p>
  97. <p>熟悉XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX。</p>
  98. <p>熟悉XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX。</p>
  99. <p>熟悉XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX。</p>
  100. <p>熟悉XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX。</p>
  101. <p>熟悉XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX。</p>
  102. <p>熟悉XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX。</p>
  103. </div>
  104. </div>
  105. <div class="module">
  106. <h3 class="list1">工作经历</h3>
  107. <div class="block">
  108. <p>XXX技术有限公司<b>/</b>Java开发工程师<b>/</b>2018.01 - 至今</p>
  109. <p>参与多个项目开发、测试、部署等工作,包括:</p>
  110. <p>1XXX。</p>
  111. <p>2XXX。</p>
  112. <p>3XXX。</p>
  113. </div>
  114. </div>
  115. <div class="module">
  116. <h3 class="list1">项目经历</h3>
  117. <div class="block">
  118. <h4 class="list2">项目1</h4>
  119. <div class="block1">
  120. <p>项目名称:XXX2020-11 2021-11)</p>
  121. <p>项目介绍:XXXXXXXXXXXXXXX。</p>
  122. <p>技术架构:XXX + XXX + XXX。</p>
  123. <p>职责描述:</p>
  124. <p>1XXXX。</p>
  125. <p>2XXXX。</p>
  126. <p>3XXXX。</p>
  127. <p>4XXXX。</p>
  128. </div>
  129. <h4 class="list2">项目2</h4>
  130. <div class="block1">
  131. <p>项目名称:XXX2020-11 2021-11)</p>
  132. <p>项目介绍:XXXXXXXXXXXXXXX。</p>
  133. <p>技术架构:SpringBoot + Vue + Element-UI + ECharts。</p>
  134. <p>职责描述:</p>
  135. <p>1XXXX。</p>
  136. <p>2XXXX。</p>
  137. <p>3XXXX。</p>
  138. <p>4XXXX。</p>
  139. </div>
  140. </div>
  141. </div>
  142. <div class="module">
  143. <h3 class="list1">自我评价</h3>
  144. <div class="block">
  145. <p>1XXXX;</p>
  146. <p>2XXXX;</p>
  147. <p>3XXXX;</p>
  148. </div>
  149. </div>
  150. </div>
  151. </div>
  152. </body>
  153. </html>

image.png

生成的PDF

image.png

后记

不管是前端生成PDF,还是后端生成PDF,能实现需求就是好技术!