public void viewPDF() { String tid = (String) getSessionAttr(SessionConstants.SELECTED_TENANT_ID); int personId = getSessionAttr(SessionConstants.USER_PERSON_ID_INT); Person applyPerson = PersonService.service.getPersonById(personId); String savePath = PropKit.get("uploadedFileSaveDirectory") + tid + File.separator + "sys" + File.separator; //String fileName = createPDF(applyPerson, tid); /* SysUploadFile suf = new SysUploadFile(); suf.set("filename", "ABEC-IncomeCertificate.pdf"); suf.set("savename", ""); suf.set("filepath", savePath); suf.save(); OnlineProof onlineProof = new OnlineProof(); onlineProof.set("id", getPara("id")); onlineProof.set("upload_id", suf.get("id")); onlineProof.update();*/ File htmlFile; File htmlMidFile = null; //FileReader reader = null; InputStreamReader reader = null; FileWriter writer = null; String url = this.getRequest().getServletContext().getRealPath("/"); String proofType = getPara("proofType"); String fileName = ""; String country = getPara("country"); String travelMode = getPara("travelMode"); String fileUrl = url + "/resources/kong/"; switch (proofType) { case "1": fileUrl += "Working Certificate.html"; fileName += "Working Certificate"; break; case "2": fileUrl += "Working and Income Certificate.html"; fileName += "Working and Income Certificate"; break; case "3": fileUrl += "Certificate for Loan.html"; fileName += "Certificate for Loan"; break; case "4": if ("1".equals(travelMode)) { if ("1".equals(country) || "2".equals(country) || "3".equals(country) || "5".equals(country) || "6".equals(country) || "7".equals(country)) { fileUrl += "Certificate for Visa - EU&Business.html"; } else if ("8".equals(country)) { fileUrl += "Certificate for Visa - UK&Business.html"; } else if ("9".equals(country) || "10".equals(country) || "12".equals(country)) { fileUrl += "Certificate for Visa - Asia&Business.html"; } else if ("11".equals(country)) { fileUrl += "Certificate for Visa - Taiwan&Businessl.html"; } } else if ("2".equals(travelMode)) { if ("1".equals(country) || "2".equals(country) || "3".equals(country) || "5".equals(country) || "6".equals(country) || "7".equals(country)) { fileUrl += "Certificate for Visa - EU&Personal.html"; } else if ("8".equals(country)) { fileUrl += "Certificate for Visa - UK&Personal.html"; } else if ("9".equals(country) || "10".equals(country) || "12".equals(country)) { fileUrl += "Certificate for Visa - Asia&Personal.html"; } else if ("11".equals(country)) { fileUrl += "Certificate for Visa - Taiwan&Personal.html"; } } fileName += "Certificate for Visa"; break; case "5": fileUrl += ""; fileName += "Certificate"; break; } htmlFile = new File(fileUrl);// 指定要读取的文件 try { reader = new InputStreamReader(new FileInputStream(htmlFile), "utf-8"); //reader = new FileReader(htmlFile); // 获取该文件的输入流 char[] bb = new char[1024];// 用来保存每次读取到的字符 String str = "";// 用来将每次读取到的字符拼接,当然使用StringBuffer类更好 int n;// 每次读取到的字符长度 while ((n = reader.read(bb)) != -1) { str += (new String(bb, 0, n)); } System.out.println(str); //替换HTML参数 //str = str.replace("@Name_Cn@","test"); htmlMidFile = new File(savePath + fileName+ ".html");// 要写入的文本文件 if (!htmlMidFile.exists()) {// 如果文件不存在,则创建该文件 htmlMidFile.createNewFile(); } writer = new FileWriter(htmlMidFile);// 获取该文件的输出流 writer.write(str);// 写内容 writer.flush(); // 清空缓冲区,立即将输出流里的内容写到文件里 } catch (IOException e) { e.printStackTrace(); } finally { if (writer != null) { try { writer.close(); // 关闭输出流,施放资源 } catch (IOException ignored) { } } if (reader != null) { try { reader.close(); // 关闭输入流,释放连接 } catch (IOException ignored) { } } } //下载PDF文档 Document document; // 将html转为pdf document = new Document(PageSize.A4, 36, 36, 108, 108); // 解决中文支持问题 PdfWriter pdfwriter; try { pdfwriter = PdfWriter.getInstance(document, new FileOutputStream(savePath + fileName + ".pdf")); document.open(); String path = this.getRequest().getServletContext().getRealPath("/"); Image img = Image.getInstance(path + "/img/ABEC.png"); img.setAbsolutePosition(0, 0); img.scaleToFit(900, 860); document.add(img); //加载HTML XMLWorkerHelper.getInstance() .parseXHtml( pdfwriter, document, new InputStreamReader(new FileInputStream(htmlFile), "utf-8")); } catch (DocumentException | IOException e) { e.printStackTrace(); } finally { document.close(); // 删除临时html htmlMidFile.delete(); } //下载PDF try { FileOperateUtil.downloadx(getRequest(), getResponse(), savePath + fileName + ".pdf", "application/octet-stream", fileName + ".pdf"); } catch (IOException e) { e.printStackTrace(); } }
java IO创建HTML版PDF
最新推荐文章于 2021-03-19 17:52:13 发布