(1)Adobe Acrobat pro软件:用来制做导出模板
(2)itext的jar包java
(1)先用word作出模板界面
(2)文件另存为pdf格式文件
(3)经过Adobe Acrobat pro软件打开刚刚用word转换成的pdf文件
(4)点击右边的"准备表单"按钮,选择"测试.pdf"选择开始(选择工具栏里面添加文本域,能够选择在任意位置添加你想要的文本域。在文本域属性框能够设置文本的属性,例如文本的名称、字体大小、位置等)
(5)作完上面的工做后,直接"另存为"将pdf存储就能够
到此模板就制做完成啦!接下来就开始写代码啦json
(1)pdf工具类PdfUtil.javaapp
import java.io.ByteArrayOutputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.Map; import javax.servlet.ServletOutputStream; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.pdf.AcroFields; import com.itextpdf.text.pdf.PdfCopy; import com.itextpdf.text.pdf.PdfImportedPage; import com.itextpdf.text.pdf.PdfReader; import com.itextpdf.text.pdf.PdfStamper; public class PdfUtil { /** * * @param o 写入的数据 * @param out 自定义保存pdf的文件流 * @param templatePath pdf模板路径 */ // 利用模板生成pdf public void fillTemplate(Map<String,Object> o,ServletOutputStream out,String templatePath) { PdfReader reader; ByteArrayOutputStream bos; PdfStamper stamper; try { reader = new PdfReader(templatePath);// 读取pdf模板 bos = new ByteArrayOutputStream(); stamper = new PdfStamper(reader, bos); AcroFields form = stamper.getAcroFields(); java.util.Iterator<String> it = form.getFields().keySet().iterator(); while (it.hasNext()) { String name = it.next().toString(); System.out.println(name); String value = o.get(name)!=null?o.get(name).toString():null; form.setField(name,value); } stamper.setFormFlattening(true);// 若是为false那么生成的PDF文件还能编辑,必定要设为true stamper.close(); Document doc = new Document(); PdfCopy copy = new PdfCopy(doc, out); doc.open(); PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1); copy.addPage(importPage); doc.close(); } catch (IOException e) { System.out.println(e); } catch (DocumentException e) { System.out.println(e); } } }
(2)action文件里的方法调用PdfUtil生成pdf并导出工具
public String downloadFile(){ String json = ServletActionContext.getRequest().getParameter("json"); try { json = java.net.URLDecoder.decode(json,"UTF-8"); //解码 } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } JSONObject jsonObject = JSONObject.fromObject(json); Map<String, Object> mapJson = JSONObject.fromObject(jsonObject); HttpServletResponse response = ServletActionContext.getResponse(); // 设置response参数,能够打开下载页面 response.reset(); response.setCharacterEncoding("UTF-8"); // 定义输出类型 response.setContentType("application/PDF;charset=utf-8"); response.setHeader("Content-Disposition", "attachment; filename=" + "assessment.pdf"); try { ServletOutputStream out = response.getOutputStream(); PdfUtil pdf = new PdfUtil(); pdf.fillTemplate(mapJson ,out,"模板pdf存放的路径"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
(3)js发起调用(若是用Ajax发起的调用就没法打开下载页面)测试
var json = {}; json.studyNum = "123456789"; json.name = "TOP__ONE"; json.sex = "男"; json.birthday = "1991-01-01"; json.id = "130222111133338888"; json.addr = "河北省保定市"; var strJson = encodeURIComponent(encodeURIComponent(JSON.stringify(json))); //编码 window.open( "../test/downloadFile.action?json="+strJson);
(4)运行结果以下
字体
参考博客地址:https://blog.csdn.net/top__one/article/details/65442390编码