1、参考
2、原理
3、代码
| <!DOCTYPE html> |
| <html lang="en"> |
| |
| <head> |
| <meta charset="UTF-8"> |
| <title>pdf</title> |
| </head> |
| <div> |
| <input style="display:none" id="pdf_id" type="text" value="9715" /> |
| <input style="display:none" id="nowUrl" type="text" value="http://127.0.0.1:46111" /> |
| <input onclick="f111_print()" type="button" value="打印" /> |
| <input onclick="f222_create()" type="button" value="生成" /> |
| |
| <input style="display:none" id="pdf_id" type="text" value="7890"> |
| <input style="display:none" id="nowUrl" type="text" value="https://www.baidu.com/"> |
| </div> |
| |
| <body> |
| </body> |
| <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script> |
| <script src="https://www.layuicdn.com/layui-v2.5.6/layui.all.js"></script> |
| |
| <script> |
| let request_url_prefix = "http://127.0.0.1:46111"; |
| $(function () { |
| |
| |
| |
| show_warn("bbbb"); |
| }) |
| |
| function f111_print() { |
| let pdf_id = $("#pdf_id").val(); |
| $.ajax({ |
| type: 'get', |
| url: request_url_prefix + '/dialog/get_medical_record_data_is_ok?id=' + pdf_id, |
| dataType: "json", |
| success: function (obj) { |
| console.log("get_medical_record_data_is_ok ", obj); |
| if (obj.code == "01") { |
| showOk("正在处理中"); |
| print_medical_record_pdf(pdf_id); |
| } else { |
| alert(obj.message); |
| } |
| } |
| }) |
| } |
| |
| function f222_create() { |
| create_pdf($("#pdf_id").val()); |
| } |
| |
| function print_medical_record_pdf(id) { |
| var url = request_url_prefix + "/dialog/get_medical_record_data?id=" + id; |
| var xhr = new XMLHttpRequest(); |
| xhr.open('get', url, true); |
| |
| xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); |
| xhr.responseType = "arraybuffer"; |
| xhr.onload = function () { |
| console.log("get_medical_record_data", this.response); |
| const binaryData = []; |
| binaryData.push(this.response); |
| let pdfUrl = window.URL.createObjectURL( |
| new Blob(binaryData, { type: "application/pdf" }) |
| ); |
| if (pdfUrl) { |
| handle_print(pdfUrl); |
| } |
| }; |
| xhr.send() |
| } |
| |
| function handle_print(pdf_url) { |
| if (document.getElementById("print-iframe")) { |
| document.body.removeChild(document.getElementById("print-iframe")); |
| } |
| |
| let iframe = document.getElementById("print-iframe"); |
| if (!iframe) { |
| iframe = document.createElement("IFRAME"); |
| iframe.style = 'display: none'; |
| let doc = null; |
| iframe.setAttribute("src", pdf_url); |
| iframe.setAttribute("id", "print-iframe"); |
| document.body.appendChild(iframe); |
| doc = iframe.contentWindow.document; |
| doc.close(); |
| iframe.contentWindow.focus(); |
| } |
| iframe.contentWindow.print(); |
| } |
| |
| function create_pdf(id) { |
| $.ajax({ |
| type: 'post', |
| url: request_url_prefix + '/dialog/exportPdf?id=' + id, |
| dataType: "json", |
| success: function (obj) { |
| console.log("create_pdf ", obj); |
| if (obj.code == "01") { |
| alert(obj.message) |
| } else { |
| alert(obj.message) |
| } |
| } |
| }) |
| } |
| function show_warn(msg) { |
| layer.msg("<span style='color: #000;'>" + msg + "</span>", { icon: 7, time: 2000 }); |
| } |
| function showOk(msg) { |
| layer.msg("<span style='color: #000'>" + msg + "</span>", { icon: 6, time: 2000 }); |
| } |
| |
| function showError(msg) { |
| layer.msg("<span" + msg + "</span>", { icon: 2, time: 2000 }); |
| } |
| </script> |
| |
| |
| </html> |
4、java
| @CrossOrigin |
| @GetMapping("/get_medical_record_data_is_ok") |
| @ResponseBody |
| public Result get_medical_record_data_is_ok(Integer id, HttpServletResponse response) { |
| Result result = new Result(); |
| try { |
| if (redisTemplate.opsForValue().get(treatmentPdf.getRedisKey(id)) != null) { |
| return new Result("02", "正在生成中,请稍后"); |
| } |
| System.out.println(" ===== get_medical_record_data 接收到的id=" + id); |
| String folder = treatmentPdf.getMedicalRecordFolder(); |
| File file = new File(folder + File.separator + IDMake.createId() + ".html"); |
| String absolutePath = file.getParentFile().getAbsolutePath(); |
| String pdfName = FileUtil.getTreatmentPdfName(id); |
| String pdfUrl = absolutePath + File.separator + pdfName; |
| |
| File filePdf = new File(pdfUrl); |
| if (!filePdf.exists()) { |
| return new Result("02", "请先点击生成"); |
| } |
| return new Result("01", "可以下载打印"); |
| } catch (Exception e) { |
| LogbackUtil.getErrorLogger().error(e.getMessage(), e); |
| return new Result("02", "下载失败 " + e.getMessage()); |
| } |
| } |
| |
| @CrossOrigin |
| @GetMapping("/get_medical_record_data") |
| public Result get_medical_record_data(Integer id, HttpServletResponse response) { |
| Result result = new Result(); |
| try { |
| if (redisTemplate.opsForValue().get(treatmentPdf.getRedisKey(id)) != null) { |
| return new Result("02", "正在生成中,请稍后"); |
| } |
| System.out.println(" ===== get_medical_record_data 接收到的id=" + id); |
| String folder = treatmentPdf.getMedicalRecordFolder(); |
| File file = new File(folder + File.separator + IDMake.createId() + ".html"); |
| String absolutePath = file.getParentFile().getAbsolutePath(); |
| String pdfName = FileUtil.getTreatmentPdfName(id); |
| String pdfUrl = absolutePath + File.separator + pdfName; |
| |
| File filePdf = new File(pdfUrl); |
| if (!filePdf.exists()) { |
| return new Result("02", "请先点击生成"); |
| } |
| FileInputStream fis = new FileInputStream(filePdf); |
| response.setContentType("application/pdf"); |
| |
| |
| ServletOutputStream out = response.getOutputStream(); |
| int len = 0; |
| byte[] buffer = new byte[1024]; |
| while ((len = fis.read(buffer)) > 0) { |
| out.write(buffer, 0, len); |
| } |
| fis.close(); |
| out.close(); |
| } catch (Exception e) { |
| LogbackUtil.getErrorLogger().error(e.getMessage(), e); |
| return new Result("02", "下载失败 " + e.getMessage()); |
| } |
| return result; |
| } |
| |
| |
| @GetMapping("/getDiseaseMessage") |
| public String getDiseaseMessage(String id) { |
| String html = ""; |
| try { |
| |
| ResourceLoader resourceLoader = new DefaultResourceLoader(); |
| org.springframework.core.io.Resource resource = resourceLoader.getResource("classpath:/pdfFolder/diseaseTemplate.html"); |
| |
| InputStream inputStream = resource.getInputStream(); |
| String folder = System.getProperty("user.dir"); |
| String htmlId = IDMake.createId(); |
| File file = new File(folder + File.separator + htmlId + ".html"); |
| FileUtils.copyInputStreamToFile(inputStream, file); |
| html = service.exportHtmlPDF(file, id); |
| file.delete(); |
| inputStream.close(); |
| |
| } catch (Exception e) { |
| |
| e.printStackTrace(); |
| } |
| return html; |
| } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· [翻译] 为什么 Tracebit 用 C# 开发
· 腾讯ima接入deepseek-r1,借用别人脑子用用成真了~
· Deepseek官网太卡,教你白嫖阿里云的Deepseek-R1满血版
· DeepSeek崛起:程序员“饭碗”被抢,还是职业进化新起点?
· RFID实践——.NET IoT程序读取高频RFID卡/标签
2021-04-18 SpringBoot Ribbon负载均衡策略配置
2021-04-18 SpringBoot Eureka集群配置
2021-04-18 SpringBoot集成Eureka
2021-04-18 知 识 收 录