
代码生成pdf下载,Springboot vue3
javascript
//处方下载
function handleDownload() {
exportChuFang({
id: chufang.value.id
}).then((res: any) => {
const url = API_BASE + res.msg
// 创建一个链接元素
const link = document.createElement('a')
link.href = url
link.download = res.msg.split('/').pop()
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
})
}
java
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;
@Override
public 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";
//生成pdf
createPdf(filePath + fileName, oldData,projectInitiation);
String fileUrl = "/profile/chufang/download/" + fileName;
return fileUrl;
}
private void createPdf(String fileUrl, TbProjectDrugPrescription tbProjectDrugPrescription,ProjectInitiation projectInitiation) {
//生成pdf
try {
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);
}