当前位置: 首页 > 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.dtcms.com/a/319869.html

相关文章:

  • 【tip】font-family的设置可能导致的文字奇怪展示
  • 《P3275 [SCOI2011] 糖果》
  • 运营商面向政企客户推出的DICT项目
  • 【ee类保研面试】数学类---概率论
  • 5G专网提高产业生产力
  • 别墅泳池设计综述:从理念创新到技术实现的系统性研究
  • 基于 PyTorch 从零实现 Transformer 模型:从核心组件到训练推理全流程
  • Java 大视界 -- Java 大数据在智能安防门禁系统中的人员行为分析与异常事件预警(385)
  • nvm安装,nvm管理node版本
  • Java设计模式总结
  • 【设计模式精解】什么是代理模式?彻底理解静态代理和动态代理
  • Vue自定义流程图式菜单解决方案
  • [激光原理与应用-171]:测量仪器 - 能量型 - 激光能量计(单脉冲能量测量)
  • DicomObjects COM 8.XX
  • VUE+SPRINGBOOT从0-1打造前后端-前后台系统-文章列表
  • [TIP 2025] 轻量级光谱注意力LSA,极致优化,减少99.8%参数,提升性能!
  • kafka安装与参数配置
  • MPC-in-the-Head 转换入门指南
  • 抖音、快手、视频号等多平台视频解析下载 + 磁力嗅探下载、视频加工(提取音频 / 压缩等)
  • 【性能测试】---测试工具篇(jmeter)
  • Java垃圾回收(GC)探析
  • 图像理解、计算机视觉相关名词解释
  • 最新教程 | CentOS 7 内网环境 Nginx + ECharts 页面离线部署手册(RPM 安装方式)
  • yolo目标检测技术:基础概念(一)
  • Vscode Data Wrangler 数据查看和处理工具
  • Docker容器技术详解
  • 施易德智慧门店管理系统:零售品牌出海的高效引擎
  • mysql 索引失效分析
  • Cesium粒子系统模拟风场动态效果
  • 国内使用 npm 时配置镜像源