1.工具类

  import com.hujiang.project.zhgd.Util;

  import com.itextpdf.text.BaseColor;

  import com.itextpdf.text.Document;

  import com.itextpdf.text.Font;

  import com.itextpdf.text.pdf.BaseFont;

  import com.itextpdf.text.pdf.PdfWriter;

  import com.itextpdf.tool.xml.XMLWorkerFontProvider;

  import com.itextpdf.tool.xml.XMLWorkerHelper;

  import freemarker.template.Configuration;

  import freemarker.template.DefaultObjectWrapper;

  import freemarker.template.Template;

  import org.junit.jupiter.api.Test;

  import java.io.*;

  import java.net.URLEncoder;

  import java.nio.charset.Charset;

  import java.util.HashMap;

  import java.util.Map;

  /**

  * @program: Consumer01

  * @description:

  * @author: Mr.LiuYong

  * @create: 2019-06-05 14:40

  **/

  public class PDFUtil {

  private String tempFilePath;

  private String tempFileName;

  public PDFUtil() {}

  public PDFUtil(String tempFilePath, String tempFileName) {

  this.tempFilePath=tempFilePath;

  this.tempFileName=tempFileName;

  }

  public String getTempFilePath() {

  return tempFilePath;

  }

  public void setTempFilePath(String tempFilePath) {

  this.tempFilePath = tempFilePath;

  }

  public String getTempFileName() {

  return tempFileName;

  }

  public void setTempFileName(String tempFileName) {

  this.tempFileName = tempFileName;

  }

  public static void main(String[] args) {

  String name = "jc.html";

  PDFUtil pdfUtil = new PDFUtil("E:\\workspace\\ZHGD\\user-consumer-01\\target\\classes\\static\\upload",name);

  Map paraMap = new HashMap();

  paraMap.put("constructionName","123");

  paraMap.put("empName","123");

  paraMap.put("idCode","123");

  paraMap.put("teamName","123");

  paraMap.put("projectName","123");

  try {

  String uploadfile = pdfUtil.fillTemplate(paraMap);

  System.out.println(uploadfile);

  } catch (Exception e) {

  e.printStackTrace();

  }

  }

  /*

  * 填充模板

  * @param paramMap

  * @throws Exception

  */

  public String fillTemplate(Map paramMap) throws Exception {

  File modelFile = new File(tempFilePath);

  if(!modelFile.exists()) {

  modelFile.mkdirs();

  }

  Configuration configuration = new Configuration(Configuration.VERSION_2_3_23);

  configuration.setDirectoryForTemplateLoading(modelFile);

  configuration.setObjectWrapper(new DefaultObjectWrapper(Configuration.VERSION_2_3_23));

  configuration.setDefaultEncoding("UTF-8"); //这个一定要设置,不然在生成的页面中 会乱码

  //获取或创建一个模版。

  Template template = configuration.getTemplate(tempFileName);

  StringWriter stringWriter = new StringWriter();

  BufferedWriter writer = new BufferedWriter(stringWriter);

  template.process(paramMap, writer); //把值写进模板

  String htmlStr = stringWriter.toString();

  writer.flush();

  writer.close();

  String tmpPath = tempFilePath + "/temp";

  File tmepFilePath = new File(tmpPath);

  if (!tmepFilePath.exists()) {

  tmepFilePath.mkdirs();

  }

  String tmpFileName =System.currentTimeMillis()+".pdf";

  String outputFile = tmpPath + File.separatorChar + tmpFileName;

  FileOutputStream outFile = new FileOutputStream(outputFile);

  createPDFFile(htmlStr, outFile);

  return outputFile;

  }

  /**

  * 根据HTML字符串创建PDF文件

  * @param htmlStr

  * @param os

  * @throws Exception

  */

  private void createPDFFile(String htmlStr, OutputStream os) throws Exception{

  ByteArrayInputStream bais = new ByteArrayInputStream(htmlStr.getBytes("UTF-8"));

  // step 1

  Document document = new Document();

  try {

  // step 2

  PdfWriter writer = PdfWriter.getInstance(document, os);

  // step 3

  document.open();

  FontProvider provider = new FontProvider();

  XMLWorkerHelper.getInstance().parseXHtml(writer, document, bais, Charset.forName("UTF-8"),provider);

  } catch (Exception e) {

  e.printStackTrace();

  throw e;

  }finally {

  try {

  document.close();

  } catch (Exception e) {

  e.printStackTrace();

  }

  try {

  bais.close();

  } catch (Exception e) {

  e.printStackTrace();

  }

  }

  }

  /**

  *

  * 字体

  *

  */

  private class FontProvider extends XMLWorkerFontProvider {

  public Font getFont(final String fontname, final String encoding,

  final boolean embedded, final float size, final int style,

  final BaseColor color) {

  BaseFont bf = null;

  try {

  bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

  } catch (Exception e) {

  e.printStackTrace();

  }

  Font font = new Font(bf, size, style, color);

  font.setColor(color);

  return font;

  }

  }

  /**无锡人流多少钱 http://www.bhnfkyy.com/

  * 生成html模板

  * @param content

  * @return

  */

  public String createdHtmlTemplate(String content){

  String fileName = tempFilePath + "/" + tempFileName;

  try{

  File file = new File(tempFilePath);

  if(!file.isDirectory()) {

  file.mkdir();

  }

  file = new File(fileName);

  if(!file.isFile()) {

  file.createNewFile();

  }

  //打开文件

  PrintStream printStream = new PrintStream(new FileOutputStream(fileName));

  //将HTML文件内容写入文件中

  printStream.println(content);

  printStream.flush();

  printStream.close();

  System.out.println("生成html模板成功!");

  }catch(Exception e){

  e.printStackTrace();

  }

  return fileName;

  }

  }

  2.html

  工人进场承诺书

  本人__${empName}__(身份证号:__${idCode}__)

  是__${constructionName}__承建的__${projectName}

  __项目____${teamName}__班组的工人。本人郑重承诺并保证做到以下几点:

  一、知悉公司的各项规章制度、项目告示牌和权益维护牌上公示的各

  项内容,自愿配合公司落实“项目实名制”各项作。

  二、不隐瞒且如实提供个人的身份信息。

  三、公司用于发放工资的银行卡由本人使用和保管,本人定期与班组

  长核实确定工资款,不虚报出勤,不冒领工资,否则本人自愿

  承担相应法律责任。

  四、当自己合法权益受到侵害时(如劳资纠纷、工伤事故纠纷等),

  向公司(项目部)申诉或向总包单位请求协调解决,在处理无效的情况

  下,依法向有关部门举报投诉。自觉遵守法律法规,决不采取闹访、围

  攻围堵公共设施等非法过激行为,决不恶意或过激讨薪,通过合法途径

  理性反映诉求。如有无理或非法行为愿意承担一切法律责任。

  承诺人(签字并加按指印):

  ____年____月____日

Java 编辑html模板并生成pdf的更多相关文章

  1. Chimm.Excel —— 使用Java 操作 excel 模板文件生成 excel 文档

    Chimm.Excel -- 设置模板,填充数据,就完事儿了~ _____ _ _ _____ _ / __ \ | (_) | ___| | | | / \/ |__ _ _ __ ___ _ __ ...

  2. 在Java代码中使用iTextPDF生成PDF

    1. 生成PDF 载入字体 static { FontFactory.register("/fonts/msyh.ttf"); FontFactory.register(" ...

  3. java代码操作word模板并生成PDF

    这个博客自己现在没时间写,等后面有时间了,自己再写. 这中需求是在实际的项目开发中是会经常遇到的. 下面我们先从简单入手一步一步开始. 1.首先,使用word创建一个6行两列的表格. 点击插入-6行2 ...

  4. Freemarker + iTextRender 实现根据模板网页生成PDF

    #0 背景 工作需要实现导出PDF的功能,在进行简单调研后,我决定采用Freemarker + iTextRender进行实现. 基本思路如下: Freemarker实现根据动态数据渲染出需要导出的H ...

  5. 在.net Core 使用PDF模板文件生成PDF文件,代替WEB打印控件!

    这几天找WEB打印控件,要么收费的,要么免费的只能在IE里用! 我只想简单的打个标签纸!百度2天,看到一老兄说可以用PDF,然后又开始百度..找到了一篇文章 http://www.jianshu.co ...

  6. java根据模板生成pdf

    原文链接:https://www.cnblogs.com/wangpeng00700/p/8418594.html 在网上看了一些Java生成pdf文件的,写的有点乱,有的不支持写入中文字体,有的不支 ...

  7. Java利用模板生成pdf并导出

    1.准备工作 (1)Adobe Acrobat pro软件:用来制作导出模板 (2)itext的jar包 2.开始制作pdf模板 (1)先用word做出模板界面 (2)文件另存为pdf格式文件 (3) ...

  8. JAVA使用itext根据模板生成PDF文档

    1.制作PDF模板 网址打开:https://www.pdfescape.com/open/ 我们这里先在线上把基础的内容用word文档做好,然后转成PDF模板,直接上传到网站上,这样方便点 假设我们 ...

  9. 【Java】itext根据模板生成pdf(包括图片和表格)

    1.导入需要的jar包:itext-asian-5.2.0.jar itextpdf-5.5.11.jar. 2.新建word文档,创建模板,将文件另存为pdf,并用Adobe Acrobat DC打 ...

随机推荐

  1. loj2734「JOISC 2016 Day 2」女装大佬 || 洛谷P3615 如厕计划

    loj2734 洛谷P3615 http://218.5.5.242:9021/problem/185 不会做... 题解(来自ditoly): 这一步更详细的解释(来自kkksc03): 还是从后面 ...

  2. Ubuntu里let's encrypt通配符证书的自动续期更新

    环境与需求: Ubuntu云服务器上,已经做好了ssl证书的免费申请,但是证书的期限是3个月,3个月到期后必须重新申请或者更新.由于k8s集群里的服务一直在使用证书,每三个月人工更新太麻烦,所以想要配 ...

  3. 通过代码理解Asp.net4中的几种ClientIDMode设置.

    以前我们可以通过ClientID在JavaScript脚本中服务器端控件.比如: document.getElementById("<%=控件.ClientID %>" ...

  4. Dev控件工具箱安装

    安装目录\Components\Tools,打开命令行 安装DEV工具 $ ToolboxCreator.exe /ini:toolboxcreator.ini 移除DEV工具 $ ToolboxCr ...

  5. R17下maps新增参数的问题

    今天遇到一个奇怪的问题,我之前写的一个函数在我弟弟的机器上编译出错.代码如下: %%将list [k1,v1,k2,v2...]转换成map {k1=>v1,key2=>v2...} -s ...

  6. Hadoop 2.7.0模拟分布式实验环境搭建[亲测]

    实验目的: 本实验通过在PC电脑上同时运行3个虚拟机,一个为master节点,两个slave节点.    搭建环境: 主机:mac os 10.10   OS:CenOS 6.5 虚拟机:VMware ...

  7. python基础教程总结5——函数

    1.函数创建 1).函数代码块以def关键词开头,后接函数标识符名称和圆括号()2).任何传入参数和自变量必须放在圆括号中间.圆括号之间可以用于定义参数3).函数的第一行语句可以选择性地使用文档字符串 ...

  8. 日常-acm-三位数反转

    输入一个三位数,分理出它的百位,十位和个位,反转后输出. 样例输入: 127 样例输出: 721 tips:注意最后一位为0的情况,如360,输出063 #include <iostream&g ...

  9. MVC批量上传文件(使用uploadify)

    <script src="JS/jquery-1.8.3.js"></script> <script src="uploadify/jque ...

  10. [dp]uestc oj 邱老师看电影

      定义状态dp[w][b]表示有w只白老鼠,b只黑老鼠时妹子赢的概率,分两种情况妹子抓到白老鼠概率为w/(w+b)和否则只有妹子抓黑老鼠和邱老师抓黑老鼠妹子才可能赢,再分两种情况:酱神抓白老鼠,状态 ...