1.先创建文件夹,保存要生成的pdf
2.获得要输出的pdf的文件流
3.保存文件。
4.引入itextpdf包
我用的包的下载链接:https://pan.baidu.com/s/1xFJ-FGz0Ra8N8E62fcOdSg 密码:3x69
或则自己去官网下载
直接上代码:
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactoryImp;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
public void downloadDispensingDetail(@PathVariable("id") Integer id, HttpSession session, HttpServletResponse response, HttpServletRequest request) throws IOException { if (null == id) { return; } User loginUser = LoginUserHelper.getCurrentUser(session); if (loginUser == null) { return; }
//根据自己的项目封装的model PrintDispensingDetailPageModel model = dispensingService.getPrintDispensingDetail(id); if (null == model) { return; } //获取到tomcat路径 String path = request.getSession().getServletContext().getRealPath(""); String realPath = new File(path).getParentFile().getParent(); //新生成的文件地址 // String filename = realPath + "/发药记录-" + model.getId() + ".pdf"; String filename = realPath + File.separator + "发药记录-" + model.getId() + ".pdf"; File oldfile = new File(filename); // 若文件已存在,直接返回,不重复生成 if (oldfile.exists() && oldfile.isFile()) { System.out.println("file is have "); }else{ //生成pdf文件 Document document = new Document(PageSize.A4, 50, 50, 50, 50); PdfWriter writer = null; try { PdfWriter.getInstance(document, new FileOutputStream(filename)); document.open(); FontFactoryImp fontFactory = new FontFactoryImp(); fontFactory.registerDirectories(); Font titleFont = fontFactory.getFont("微软雅黑", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 20, Font.BOLD, null); // 标题 Paragraph title = new Paragraph("发药记录", titleFont); title.setAlignment(Element.ALIGN_CENTER); title.setSpacingAfter(20f); document.add(title); Font font = fontFactory.getFont("微软雅黑", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 12, Font.UNDEFINED, null); PdfPTable table = new PdfPTable(4); table.addCell(new Paragraph("受试者编号", font)); table.addCell(new Paragraph(model.getPatientNum(), font)); table.addCell(new Paragraph("受试者姓名", font)); table.addCell(new Paragraph(model.getPatientName(), font)); table.addCell(new Paragraph("发药时间", font)); table.addCell(new Paragraph(model.getDispensingTime(), font)); table.addCell(new Paragraph("发药数目", font)); table.addCell(new Paragraph("" + model.getDrugRandomIds().size(), font)); PdfPCell cell; cell = new PdfPCell(new Paragraph("医院编号", font)); cell.setColspan(2); table.addCell(cell); cell = new PdfPCell(new Paragraph(loginUser.getHospital().getHospitaNumber(), font)); cell.setColspan(2); table.addCell(cell); cell = new PdfPCell(new Paragraph("医院名称", font)); cell.setColspan(2); table.addCell(cell); cell = new PdfPCell(new Paragraph(loginUser.getHospital().getName(), font)); cell.setColspan(2); table.addCell(cell); cell = new PdfPCell(new Paragraph("药物编号", font)); cell.setColspan(4); table.addCell(cell); for (int i = 0; i < model.getDrugRandomIds().size(); i++) { table.addCell(new Paragraph(model.getDrugRandomIds().get(i), font)); } // 补齐最后一行 if (model.getDrugRandomIds().size() % 4 != 0) { for (int j = model.getDrugRandomIds().size() % 4; j < 4; j++) { table.addCell(""); } } document.add(table); } catch (Exception e) { e.printStackTrace(); System.out.println("pdf crate fail"); } finally { document.close(); if (null != writer) { writer.close(); } } System.out.println("pdf crate success:"+filename); } // 下面是下载 response.setContentType("text/html; charset=UTF-8"); // 设置编码字符 response.setContentType("application/x-msdownload"); // 设置内容类型为下载类型 response.setHeader("Content-Disposition", "attachment; filename=" + new String(("发药记录-" + model.getId() + ".pdf").getBytes(), "iso8859-1")); // response.setHeader("Content-Disposition", "attachment;filename=yltsqb.pdf"); try { ServletOutputStream out = response.getOutputStream(); File file = new File(realPath + File.separator+"发药记录-" + model.getId() + ".pdf"); // 创建文件 FileInputStream fis = new FileInputStream(realPath + File.separator+"发药记录-" + model.getId() + ".pdf"); // 创建文件字节输入流 来源文件 BufferedInputStream buff = new BufferedInputStream(fis); // 创建文件缓冲输入流 byte[] b = new byte[2048]; // 设置每次读取大小 long l = 0; // 用于判断是否等同于文件的长度,即文件下载大小 while (l < file.length()) { // 循环读取文件 int j = buff.read(b, 0, 2048); // 使用缓冲流读取数据,返回缓冲区长度 l += j; out.write(b, 0, j);// 将缓存写入客户端 } buff.close(); out.close(); } catch (IOException e) { e.printStackTrace(); } System.out.println("dowland success"); }
注意:要根据自己的项目来封装model.