当前位置: 首页 > news >正文

java 生成pdf导出

在这里插入图片描述
代码生成pdf下载,Springboot vue3

//处方下载
function handleDownload() {exportChuFang({id: chufang.value.id}).then((res: any) => {const url = API_BASE + res.msg// 创建一个链接元素const link = document.createElement('a')link.href = urllink.download = res.msg.split('/').pop()document.body.appendChild(link)link.click()document.body.removeChild(link)})
}
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.draw.LineSeparator;@Overridepublic String exportChuFang(TbProjectDrugPrescription tbProjectDrugPrescription) {TbProjectDrugPrescription oldData=tbProjectDrugPrescriptionMapper.selectTbProjectDrugPrescriptionById(tbProjectDrugPrescription.getId());ProjectInitiation projectInitiation=projectInitiationService.findById(oldData.getProjectInitiationId(),null);//查询伦理的项目名称String filePath = RuoYiConfig.getUploadPath("/chufang/download/");String fileName = projectInitiation.getAcceptanceNumber().replace(" ", "")+"_"+ oldData.getVisitNickName().replace(" ", "")  + "_处方_" + DateUtils.dateTimeNow() + ".pdf";//生成pdfcreatePdf(filePath + fileName, oldData,projectInitiation);String fileUrl = "/profile/chufang/download/" + fileName;return fileUrl;}private void createPdf(String fileUrl, TbProjectDrugPrescription tbProjectDrugPrescription,ProjectInitiation projectInitiation) {//生成pdftry {Document document = new Document();PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileUrl));document.open();BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);Font TITLE_FONT = new Font(baseFont, 20, Font.BOLD);Font HEADER_FONT = new Font(baseFont, 12, Font.BOLD);Font CONTENT_FONT = new Font(baseFont, 12, Font.NORMAL, BaseColor.GRAY);Font ITALIC_FONT = new Font(baseFont, 10,Font.ITALIC);// 添加标题PdfPTable table = new PdfPTable(3);table.setWidthPercentage(100);PdfPCell cell = new PdfPCell(new Paragraph("药物/器械临床试验专用处方笺", TITLE_FONT));cell.setHorizontalAlignment(Element.ALIGN_CENTER);cell.setColspan(3);cell.setPaddingBottom(6);table.addCell(cell);//申办方-药名-适应症-分期Paragraph phrase = new Paragraph();phrase.add(new Chunk("试验名称/受理号:", HEADER_FONT));//申办方-药名-适应症-分期StringBuilder s=new StringBuilder();s.append(projectInitiation.getSponsorName()).append("-");s.append(projectInitiation.getProductName()==null?"药品":projectInitiation.getProductName()).append("-");s.append(projectInitiation.getIndication()==null?"":projectInitiation.getIndication()).append("-");SysDictData dictData = new SysDictData();dictData.setDictType("project_staging");List<SysDictData> dictData1 = dictDataService.selectDictDataList(dictData);s.append(dictData1.stream().filter(e -> e.getDictValue().equals(projectInitiation.getProjectStage())).findFirst().get().getDictLabel());s.append("(").append(projectInitiation.getAcceptanceNumber()).append(")");phrase.add(new Chunk(s.toString(), CONTENT_FONT));PdfPCell cell2 = new PdfPCell(phrase);cell2.setColspan(3);cell2.setPaddingBottom(10);cell2.setLeading(2f,1.5f);table.addCell(cell2);phrase = new Paragraph();phrase.add(new Chunk("科别:", HEADER_FONT));phrase.add(new Chunk(projectInitiation.getUndertakingDepartment(), CONTENT_FONT));cell2 = new PdfPCell(phrase);cell2.setColspan(3);cell2.setPaddingBottom(6);table.addCell(cell2);phrase = new Paragraph();phrase.add(new Chunk("姓名:", HEADER_FONT));phrase.add(new Chunk(tbProjectDrugPrescription.getVisitNickName(), CONTENT_FONT));cell2 = new PdfPCell(phrase);cell2.setPaddingBottom(6);table.addCell(cell2);phrase = new Paragraph();phrase.add(new Chunk("性别:", HEADER_FONT));phrase.add(new Chunk("", CONTENT_FONT));cell2 = new PdfPCell(phrase);cell2.setPaddingBottom(6);table.addCell(cell2);phrase = new Paragraph();phrase.add(new Chunk("年龄:", HEADER_FONT));phrase.add(new Chunk("", CONTENT_FONT));cell2 = new PdfPCell(phrase);cell2.setPaddingBottom(6);table.addCell(cell2);document.add(table);// 创建表格,2列PdfPTable table0 = new PdfPTable(2);table0.setWidthPercentage(100);phrase = new Paragraph();phrase.add(new Chunk("受试者编号:", HEADER_FONT));phrase.add(new Chunk(tbProjectDrugPrescription.getSubjectsNo(), CONTENT_FONT));PdfPCell leftCell = new PdfPCell(phrase);leftCell.setBorder(Rectangle.NO_BORDER);leftCell.setPaddingBottom(6);phrase = new Paragraph();phrase.add(new Chunk("访视周期:", HEADER_FONT));TbProjectSubjectsVisit subjectsVisit = tbProjectSubjectsVisitMapper.selectTbProjectSubjectsVisitById(tbProjectDrugPrescription.getSubjectsVisitId());phrase.add(new Chunk(subjectsVisit.getVisitTask(), CONTENT_FONT));PdfPCell rightCell = new PdfPCell(phrase);rightCell.setBorder(Rectangle.NO_BORDER);rightCell.setPaddingBottom(6);table0.addCell(leftCell);table0.addCell(rightCell);document.add(table0);// 添加一条黑色分割线LineSeparator line = new LineSeparator();line.setLineColor(BaseColor.BLACK); // 设置为黑色line.setLineWidth(1f); // 线宽line.setPercentage(100f); // 线的宽度占页面宽度的百分比document.add(line);PdfPTable table1 = new PdfPTable(1);table1.setWidthPercentage(100);phrase = new Paragraph();phrase.add(new Chunk("临床诊断:", HEADER_FONT));phrase.add(new Chunk("", CONTENT_FONT));PdfPCell cell1 = new PdfPCell(phrase);cell1.setBorder(Rectangle.NO_BORDER);cell1.setPaddingBottom(6);table1.addCell(cell1);document.add(table1);document.add(line);PdfPTable table2 = new PdfPTable(1);table2.setWidthPercentage(100);phrase = new Paragraph();phrase.add(new Chunk("RP:", TITLE_FONT));PdfPCell cell3 = new PdfPCell(phrase);cell3.setBorder(Rectangle.NO_BORDER);cell3.setPaddingBottom(6);table2.addCell(cell3);document.add(table2);// 添加药品信息addMedicineInfoTable(document, tbProjectDrugPrescription,ITALIC_FONT,projectInitiation.getDoubleBlindTrial());document.add(Chunk.NEWLINE);// 添加备注phrase = new Paragraph();phrase.add(new Chunk("备注:", HEADER_FONT));phrase.add(new Chunk(tbProjectDrugPrescription.getRemark(), CONTENT_FONT));document.add(phrase);document.add(Chunk.NEWLINE);document.add(Chunk.NEWLINE);document.add(line);// 添加签名信息addSignatureTable(document, tbProjectDrugPrescription,HEADER_FONT,CONTENT_FONT);document.close();} catch (Exception e) {e.printStackTrace();}}/*** 添加药品信息表格*/private void addMedicineInfoTable(Document document,TbProjectDrugPrescription data,Font ITALIC_FONT, String doubleBlindTrial) throws DocumentException {TbProjectDrugOutbound tbProjectDrugOutboundParam = new TbProjectDrugOutbound();tbProjectDrugOutboundParam.setPrescriptionId(data.getId());List<TbProjectDrugOutbound> tbProjectDrugOutbounds = projectDrugOutboundMapper.selectTbProjectDrugOutboundList(tbProjectDrugOutboundParam);if (!CollectionUtils.isEmpty(tbProjectDrugOutbounds)) {for (TbProjectDrugOutbound tbProjectDrugOutbound : tbProjectDrugOutbounds) {TbProjectDrugStorage tbProjectDrugStorage = tbProjectDrugStorageMapper.selectTbProjectDrugStorageById(tbProjectDrugOutbound.getProjectDrugStorageId());PdfPTable table = new PdfPTable(1);table.setWidthPercentage(100);table.setSpacingBefore(5);table.setSpacingAfter(5);addTableCell(table, "药名:", tbProjectDrugStorage.getDrugName()+(doubleBlindTrial.equals("1")?"-试验剂/安慰剂":""), ITALIC_FONT);addTableCell(table, "剂型:",tbProjectDrugOutbound.getUnitMin()+"剂",  ITALIC_FONT);addTableCell(table, "规格:",tbProjectDrugStorage.getMeasurementSpecifications()+" x "+ tbProjectDrugStorage.getDrugSpecifications(),  ITALIC_FONT);addTableCell(table, "药物编号:", tbProjectDrugStorage.getDrugStorageNo(),  ITALIC_FONT);addTableCell(table, "用法用量:",tbProjectDrugStorage.getUsageDosage(),  ITALIC_FONT);addTableCell(table, "取药数量:", tbProjectDrugOutbound.getNum()+tbProjectDrugOutbound.getUnit(),  ITALIC_FONT);document.add(table);}}}/*** 添加签名信息表格*/private static void addSignatureTable(Document document,TbProjectDrugPrescription data,Font HEADER_FONT,Font CONTENT_FONT) throws DocumentException {PdfPTable table = new PdfPTable(4);table.setWidthPercentage(70);table.setSpacingBefore(10);table.setSpacingAfter(10);addTableCell2(table, "授权医师", "", HEADER_FONT);addTableCell2(table, "日期", "", HEADER_FONT);addTableCell2(table, "发药人", "", HEADER_FONT);addTableCell2(table, "日期", "", HEADER_FONT);addTableCell2(table, "领药人", "", HEADER_FONT);addTableCell2(table, "日期", "", HEADER_FONT);document.add(table);}/*** 添加表格单元格*/private static void addTableCell(PdfPTable table, String label, String content, Font ITALIC_FONT) {Phrase phrase = new Phrase();phrase.add(new Chunk(label, ITALIC_FONT));phrase.add(new Chunk(content, ITALIC_FONT));PdfPCell cell2 = new PdfPCell(phrase);cell2.setPaddingBottom(6);cell2.setBorder(Rectangle.NO_BORDER);table.addCell(cell2);}/*** 添加表格单元格*/private static void addTableCell2(PdfPTable table, String label, String content, Font font) {PdfPCell cell = new PdfPCell(new Phrase(label, font));cell.setPaddingBottom(6);table.addCell(cell);cell = new PdfPCell(new Phrase(content, font));table.addCell(cell);}
http://www.lryc.cn/news/612717.html

相关文章:

  • iOS 文件管理实战指南,用户文件、安全访问与开发调试方案
  • OpenCv对图片视频的简单操作
  • Flutter 局部刷新方案对比:ValueListenableBuilder vs. GetBuilder vs. Obx
  • PPT漏斗图,让数据更美观!
  • OpenAI重磅发布:GPT最新开源大模型gpt-oss系列全面解析
  • 【沉浸式解决问题】mysql-connector-python连接数据库:RuntimeError: Failed raising error.
  • 计算机视觉(opencv)——图像本质、数字矩阵、RGB + 基本操作(实战一)
  • Java面试宝典:JVM的垃圾收集算法
  • Linux中chmod命令
  • JAVA,Maven分模块设计
  • 初识C++类的6个默认成员函数
  • 模拟-38.外观数列-力扣(LeetCode)
  • 【数据库】如何从本地电脑连接服务器上的MySQL数据库?
  • 国内主流数据集成厂商有哪些?有那些免费的数据集成平台?
  • 【Java】Predicate使用案例
  • 【CS创世SD NAND征文】额贴式睡眠监测仪的数据守护者:存储芯片如何实现7×24小时安眠状态下的全时稳定记录
  • Nuclei漏洞扫描工具(除了常见漏洞还支持CMS常见漏洞Gitlab、Jira、Splunk、Elastic)
  • 2025年主流开源音视频播放项目深度解析
  • Java技术栈/面试题合集(20)-运维与线上问题排查篇
  • nvm安装,nvm管理node版本
  • 【数据结构初阶】--排序(五)--计数排序,排序算法复杂度对比和稳定性分析
  • MATLAB科研数据可视化
  • 【CDA案例】数据分析案例拆解:解锁数据分析全流程!
  • 图像认知与OpenCV——图像预处理4
  • 计算机视觉--opencv(代码详细教程)
  • Java垃圾回收(GC)探析
  • 网络可视,运维无忧:分钟级定位,告别盲目扩容
  • 华为开源CANN,再次释放“昇腾转向”信号
  • spring boot学习计划
  • Qt: WA_DontCreateNativeAncestors