123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524 |
- package thyyxxk.wxservice_server.utils;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import com.itextpdf.text.*;
- import com.itextpdf.text.pdf.*;
- import com.itextpdf.text.pdf.draw.LineSeparator;
- import lombok.extern.slf4j.Slf4j;
- import thyyxxk.wxservice_server.entity.ResultVo;
- import javax.servlet.http.HttpServletResponse;
- import java.io.File;
- import java.io.OutputStream;
- import java.net.URLEncoder;
- import java.nio.file.Files;
- import java.util.Map;
- import java.util.UUID;
- @Slf4j
- public class PhysicalCheckPDFUtil {
- private static final String PDF_PATH = "/home/medical-report-pdf/";
- private static final String LOGO_PATH = "/home/images/thyylogo.png";
- public static void createPDF(HttpServletResponse response, JSONObject tjIndex, JSONObject tjResult) {
- try {
- response.setContentType("application/x-download");
- response.addHeader("content-disposition", "attachment;filename="
- + URLEncoder.encode("长沙泰和医院体检报告.pdf", "UTF-8"));
- OutputStream outputStream = response.getOutputStream();
- makePdfFile(tjIndex, tjResult, outputStream);
- } catch (Exception e) {
- e.printStackTrace();
- throw new RuntimeException(e);
- }
- }
- public static ResultVo<String> savePdfToDisk(JSONObject tjIndex, JSONObject tjResult) {
- String today = DateUtil.today().replaceAll("-", "");
- String uid = UUID.randomUUID().toString().replaceAll("-", "");
- String fileName = today + uid + ".pdf";
- try {
- File pdfFile = new File(PDF_PATH + fileName);
- if (pdfFile.exists()) {
- pdfFile.delete();
- }
- OutputStream outputStream = Files.newOutputStream(pdfFile.toPath());
- makePdfFile(tjIndex, tjResult, outputStream);
- } catch (Exception e) {
- e.printStackTrace();
- }
- String downloadUrl = "https://staticweb.hnthyy.cn/medicalReport/" + fileName;
- log.info("生成体检报告下载链接:{}", downloadUrl);
- return ResultVoUtil.success();
- }
- private static void makePdfFile(JSONObject tjIndex, JSONObject tjResult, OutputStream outputStream) throws Exception {
- Document document = getDocument();
- PdfWriter pdfWriter = PdfWriter.getInstance(document, outputStream);
- document.open();
- document.add(getBarcodeParagraph(pdfWriter, tjIndex.getString("条码号")));
- BaseFont baseFont = BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
- Font font = new Font(baseFont, 30, Font.BOLD);
- document.add(getTitleParagraph(font));
- document.add(getBriefContent(font, tjIndex));
- document.add(getFooter(baseFont));
- pdfWriter.setPageEvent(new PdfPageEventHelper() {
- @Override
- public void onStartPage(PdfWriter writer, Document document) {
- PdfContentByte pdfContentByte = writer.getDirectContent();
- pdfContentByte.saveState();
- pdfContentByte.beginText();
- pdfContentByte.setFontAndSize(baseFont, 10);
- // header
- float y = document.top(-20);
- pdfContentByte.showTextAligned(PdfContentByte.ALIGN_LEFT, "H-Left", document.left(), y, 0);
- pdfContentByte.showTextAligned(PdfContentByte.ALIGN_CENTER, "第" + writer.getPageNumber() + "页",
- (document.right() + document.left()) / 2, y, 0);
- pdfContentByte.showTextAligned(PdfContentByte.ALIGN_RIGHT, "H-Right", document.right(), y, 0);
- pdfContentByte.endText();
- LineSeparator line = new LineSeparator(1, 100, BaseColor.BLACK, Element.ALIGN_CENTER, 10);
- try {
- document.add(line);
- } catch (DocumentException e) {
- throw new RuntimeException(e);
- }
- pdfContentByte.restoreState();
- }
- });
- document.newPage();
- document.add(getEntryWord(font));
- document.add(getSummaryTitle(font));
- document.add(getSummaryContent(font, tjResult.getJSONObject("summary")));
- document.add(getSummaryFooter(font, tjResult.getJSONObject("summary")));
- document.newPage();
- JSONObject examResults = tjResult.getJSONObject("examResult");
- JSONArray checkItems = tjResult.getJSONArray("checkItems");
- if (!examResults.isEmpty() && !checkItems.isEmpty()) {
- addExamResult(baseFont, document, examResults, checkItems);
- }
- document.add(getTailPageLogo(baseFont));
- document.close();
- outputStream.close();
- }
- private static Document getDocument() {
- Document document = new Document(PageSize.A4);
- document.addAuthor("长沙泰和医院");
- document.addCreationDate();
- document.addSubject("体检报告");
- return document;
- }
- private static Barcode128 getBarcode(String tjid) {
- Barcode128 barcode128 = new Barcode128();
- barcode128.setSize(10);
- barcode128.setBarHeight(28);
- barcode128.setBaseline(10);
- barcode128.setCode(tjid);
- barcode128.setStartStopText(true);
- barcode128.setExtended(true);
- return barcode128;
- }
- private static Paragraph getBarcodeParagraph(PdfWriter pdfWriter, String tjid) {
- PdfContentByte pdfContentByte = new PdfContentByte(pdfWriter);
- Barcode128 barcode128 = getBarcode(tjid);
- Image barcodeImage = barcode128.createImageWithBarcode(pdfContentByte, null, null);
- Chunk barcodeChunk = new Chunk(barcodeImage, 420, -20, false);
- return new Paragraph(barcodeChunk);
- }
- private static Paragraph getTitleParagraph(Font font) {
- Paragraph titleParagraph = new Paragraph();
- Chunk titleChunkLine1 = new Chunk("长 沙 泰 和 医 院");
- titleChunkLine1.setFont(font);
- titleChunkLine1.setTextRise(-70);
- titleParagraph.add(titleChunkLine1);
- titleParagraph.add(Chunk.NEWLINE);
- Chunk titleChunkLine2 = new Chunk("体 检 报 告");
- titleChunkLine2.setFont(font);
- titleChunkLine2.setTextRise(-90);
- titleParagraph.setAlignment(Element.ALIGN_CENTER);
- titleParagraph.add(titleChunkLine2);
- return titleParagraph;
- }
- private static Paragraph getBriefContent(Font font, JSONObject tjIndex) {
- font.setSize(12);
- font.setStyle(Font.NORMAL);
- Paragraph paragraph = new Paragraph();
- paragraph.setAlignment(Element.ALIGN_LEFT);
- paragraph.setSpacingBefore(470);
- paragraph.setIndentationLeft(146);
- String personalFlag = tjIndex.getString("团体or个人");
- String psntext = personalFlag.equals("0") ? "[ ]个人 [√]团体" : "[√]个人 [ ]团体";
- Chunk tjidChunk = new Chunk("体检编号:" + tjIndex.getString("条码号") + " " + psntext);
- tjidChunk.setFont(font);
- paragraph.add(tjidChunk);
- paragraph.add(Chunk.NEWLINE);
- String gender = tjIndex.getString("性别").equals("1") ? "男" : "女";
- String name = tjIndex.getString("姓名");
- String age = tjIndex.getString("年龄");
- Chunk psnChunk = new Chunk("姓名:" + (null == name ? "" : name) + " 性别:"
- + gender + " 年龄:" + (null == age ? "" : age));
- psnChunk.setFont(font);
- paragraph.add(psnChunk);
- paragraph.add(Chunk.NEWLINE);
- String unit = tjIndex.getString("工作单位");
- Chunk unitChunk = new Chunk("单位:" + (null == unit ? "" : unit));
- unitChunk.setFont(font);
- paragraph.add(unitChunk);
- paragraph.add(Chunk.NEWLINE);
- String department = tjIndex.getString("所在部门");
- Chunk departmentChunk = new Chunk("部门:" + (null == department ? "" : department));
- departmentChunk.setFont(font);
- paragraph.add(departmentChunk);
- paragraph.add(Chunk.NEWLINE);
- String checkTime = tjIndex.getString("登记日期");
- if (null == checkTime) {
- checkTime = "";
- }
- if (StringUtil.notBlank(checkTime) && checkTime.length() >= 8) {
- checkTime = checkTime.substring(0, 4) + "-" + checkTime.substring(4, 6) + "-" + checkTime.substring(6);
- }
- String phone = tjIndex.getString("手机");
- Chunk dateChunk = new Chunk("体检日期:" + checkTime
- + " 电话:" + (null == phone ? "" : phone));
- dateChunk.setFont(font);
- paragraph.add(dateChunk);
- return paragraph;
- }
- private static Paragraph getFooter(BaseFont baseFont) {
- Font font = new Font(baseFont, 10, Font.NORMAL);
- Paragraph paragraph = new Paragraph();
- paragraph.setAlignment(Element.ALIGN_CENTER);
- paragraph.setSpacingBefore(100);
- LineSeparator line = new LineSeparator(1, 100, BaseColor.BLACK, Element.ALIGN_CENTER, 0);
- paragraph.add(line);
- paragraph.add(Chunk.NEWLINE);
- Chunk footerLine1 = new Chunk("长沙泰和医院");
- footerLine1.setFont(font);
- paragraph.add(footerLine1);
- paragraph.add(Chunk.NEWLINE);
- Chunk footerLine2 = new Chunk("咨询电话:0731-88518505");
- footerLine2.setFont(font);
- paragraph.add(footerLine2);
- paragraph.add(Chunk.NEWLINE);
- Chunk footerLine3 = new Chunk("本体检报告仅供临床参考,不作为诊断依据,谢谢您的光临!");
- footerLine3.setFont(new Font(baseFont, 12, Font.NORMAL));
- paragraph.add(footerLine3);
- return paragraph;
- }
- private static Paragraph getEntryWord(Font font) {
- font.setSize(12);
- Paragraph paragraph = new Paragraph();
- paragraph.setFont(font);
- paragraph.add(new Chunk("尊敬的客户:"));
- paragraph.add(Chunk.NEWLINE);
- paragraph.add(new Chunk(" 您好!欢迎您莅临长沙泰和医院体检中心。身体健康是学习和工作的基础,注意健康是生活和事业发展的必须。" +
- "世界卫生组织(WHO)提出:“健康是人的生理、心里和社会的完美状态,而不仅仅是指无疾病或非体弱的状态”。通过健康体检," +
- "检查和发现影响健康的有关因素,成为促进您身心健康的重要措施和保证。为了您的健康,我们真诚的建议您定期进行健康体检。"));
- return paragraph;
- }
- private static PdfPTable getSummaryTitle(Font font) {
- font.setSize(12);
- PdfPTable table = new PdfPTable(1);
- table.setSpacingBefore(20);
- table.setWidthPercentage(100);
- PdfPCell cell = new PdfPCell(new Paragraph("一、体检结论及健康建议", font));
- cell.setPaddingBottom(8);
- table.addCell(cell);
- return table;
- }
- private static PdfPTable getSummaryContent(Font font, JSONObject summary) {
- font.setSize(12);
- font.setStyle(Font.NORMAL);
- PdfPTable table = new PdfPTable(1);
- table.setWidthPercentage(100);
- table.setSplitLate(false);
- PdfPCell cell = new PdfPCell(new Paragraph(summary.getString("汇总建议"), font));
- cell.disableBorderSide(3);
- cell.setPaddingLeft(10);
- cell.setPaddingRight(10);
- table.addCell(cell);
- return table;
- }
- private static PdfPTable getSummaryFooter(Font font, JSONObject summary) {
- font.setSize(12);
- PdfPTable table = new PdfPTable(1);
- table.setWidthPercentage(100);
- table.setSplitLate(false);
- Paragraph paragraph = new Paragraph();
- paragraph.setFont(font);
- paragraph.add(new Chunk("汇总:" + summary.getString("汇总医生")));
- paragraph.add(Chunk.NEWLINE);
- paragraph.add(Chunk.NEWLINE);
- paragraph.add(new Chunk("主检:" + summary.getString("审核医生")));
- PdfPCell cell = new PdfPCell(paragraph);
- cell.disableBorderSide(1);
- cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
- cell.setPaddingRight(30);
- cell.setPaddingTop(15);
- cell.setPaddingBottom(15);
- table.addCell(cell);
- return table;
- }
- private static void addExamResult(BaseFont baseFont, Document document, JSONObject itemResults, JSONArray checkItems) throws Exception {
- int entryIndex = 0;
- Font font = new Font(baseFont, 12, Font.NORMAL);
- for (Map.Entry<String, Object> entry : itemResults.entrySet()) {
- Paragraph paragraph = new Paragraph();
- font.setColor(BaseColor.BLUE);
- paragraph.setFont(font);
- paragraph.setSpacingAfter(8);
- if (entryIndex > 0) {
- paragraph.setSpacingBefore(15);
- }
- paragraph.add(new Chunk(entry.getKey()));
- document.add(paragraph);
- font.setColor(BaseColor.BLACK);
- JSONArray results = (JSONArray) entry.getValue();
- String restype = results.getJSONObject(0).getString("结果类型");
- if (entry.getKey().equals("碳呼气试验") || restype.equals("3")) {
- restype = "3";
- for (int i = 0; i < results.size(); i++) {
- JSONObject item = results.getJSONObject(i);
- if (StringUtil.notBlank(item.getString("单位")) || StringUtil.notBlank(item.getString("参考范围显示效果"))) {
- restype = "2";
- break;
- }
- }
- }
- String sumContent = "";
- String examDoctor = "";
- String auditDoctor = "";
- for (int i = 0; i < checkItems.size(); i++) {
- JSONObject item = checkItems.getJSONObject(i);
- if (item.getString("name").equals(entry.getKey())) {
- sumContent = item.getString("诊断内容");
- examDoctor = item.getString("检查医生签名");
- auditDoctor = item.getString("审核医生签名");
- break;
- }
- }
- if (restype.equals("3") || restype.equals("2")) {
- PdfPTable table = new PdfPTable(4);
- table.setWidthPercentage(100);
- PdfPCell header1 = new PdfPCell(new Phrase("项目", font));
- header1.disableBorderSide(8);
- header1.setPaddingBottom(6);
- header1.setBackgroundColor(BaseColor.LIGHT_GRAY);
- PdfPCell header2 = new PdfPCell(new Phrase("体检结果", font));
- header2.disableBorderSide(12);
- header2.setPaddingBottom(6);
- header2.setBackgroundColor(BaseColor.LIGHT_GRAY);
- if (restype.equals("3")) {
- header2.setColspan(3);
- }
- table.addCell(header1);
- table.addCell(header2);
- if (restype.equals("2")) {
- PdfPCell header3 = new PdfPCell(new Phrase("单位", font));
- header3.disableBorderSide(12);
- header3.setPaddingBottom(6);
- header3.setBackgroundColor(BaseColor.LIGHT_GRAY);
- PdfPCell header4 = new PdfPCell(new Phrase("参考范围", font));
- header4.disableBorderSide(4);
- header4.setPaddingBottom(6);
- header4.setBackgroundColor(BaseColor.LIGHT_GRAY);
- table.addCell(header3);
- table.addCell(header4);
- }
- for (int i = 0; i < results.size(); i++) {
- JSONObject item = results.getJSONObject(i);
- PdfPCell res1 = new PdfPCell(new Phrase(item.getString("检查项目"), font));
- res1.disableBorderSide(8);
- res1.setPaddingBottom(6);
- PdfPCell res2 = new PdfPCell(new Phrase(item.getString("结果"), font));
- res2.disableBorderSide(12);
- res2.setPaddingBottom(6);
- if (restype.equals("3")) {
- res2.setColspan(3);
- }
- table.addCell(res1);
- table.addCell(res2);
- if (restype.equals("2")) {
- PdfPCell res3 = new PdfPCell(new Phrase(item.getString("单位"), font));
- res3.disableBorderSide(12);
- res3.setPaddingBottom(6);
- PdfPCell res4 = new PdfPCell(new Phrase(item.getString("参考范围显示效果"), font));
- res4.disableBorderSide(4);
- res4.setPaddingBottom(6);
- table.addCell(res3);
- table.addCell(res4);
- }
- }
- PdfPCell sum1 = new PdfPCell(new Phrase("科室小结:", font));
- sum1.setHorizontalAlignment(Element.ALIGN_CENTER);
- sum1.setVerticalAlignment(Element.ALIGN_CENTER);
- sum1.setBackgroundColor(BaseColor.LIGHT_GRAY);
- sum1.setPaddingTop(20);
- sum1.setPaddingBottom(20);
- table.addCell(sum1);
- PdfPCell sum2 = new PdfPCell(new Phrase(sumContent, font));
- sum2.setColspan(3);
- table.addCell(sum2);
- if (StringUtil.notBlank(examDoctor)) {
- examDoctor = "检查医生:" + examDoctor;
- }
- if (StringUtil.notBlank(auditDoctor)) {
- auditDoctor = "审核医生:" + auditDoctor;
- }
- PdfPCell doctorCell = new PdfPCell(new Phrase(examDoctor + " " + auditDoctor, font));
- doctorCell.setColspan(4);
- doctorCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
- doctorCell.setPaddingRight(30);
- doctorCell.setPaddingTop(10);
- doctorCell.disableBorderSide(15);
- table.addCell(doctorCell);
- table.setSplitLate(false);
- document.add(table);
- } else {
- PdfPTable table = new PdfPTable(8);
- table.setWidthPercentage(100);
- String[] headers = new String[]{"化验项目", "结果", "单位", "参考值", "化验项目", "结果", "单位", "参考值"};
- for (String header : headers) {
- PdfPCell headerCell = new PdfPCell(new Phrase(header, font));
- headerCell.disableBorderSide(14);
- table.addCell(headerCell);
- }
- for (int i = 0; i < results.size(); i++) {
- font = new Font(baseFont, 10, Font.NORMAL);
- JSONObject item = results.getJSONObject(i);
- String resSign = item.getString("阳性标识");
- if (StringUtil.notBlank(resSign)) {
- font.setColor(BaseColor.RED);
- } else {
- font.setColor(BaseColor.BLACK);
- }
- PdfPCell nameCell = new PdfPCell(new Phrase(item.getString("检查项目"), font));
- nameCell.disableBorderSide(15);
- table.addCell(nameCell);
- PdfPCell resCell = new PdfPCell(new Phrase(item.getString("结果") + resSign, font));
- resCell.disableBorderSide(15);
- table.addCell(resCell);
- PdfPCell unitCell = new PdfPCell(new Phrase(item.getString("单位"), font));
- unitCell.disableBorderSide(15);
- table.addCell(unitCell);
- PdfPCell rangeCell = new PdfPCell(new Phrase(item.getString("参考范围显示效果"), font));
- rangeCell.disableBorderSide(i % 2 == 0 ? 7 : 15);
- table.addCell(rangeCell);
- }
- if (StringUtil.notBlank(examDoctor)) {
- examDoctor = "检验:" + examDoctor;
- }
- if (StringUtil.notBlank(auditDoctor)) {
- auditDoctor = "审核:" + auditDoctor;
- }
- font = new Font(baseFont, 12, Font.NORMAL);
- font.setColor(BaseColor.BLACK);
- PdfPCell doctorCell = new PdfPCell(new Phrase(examDoctor + " " + auditDoctor, font));
- doctorCell.setColspan(8);
- doctorCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
- doctorCell.setPaddingRight(30);
- doctorCell.setPaddingTop(10);
- doctorCell.disableBorderSide(15);
- table.addCell(doctorCell);
- PdfPCell tipCell = new PdfPCell(new Phrase("注:此检验报告只对本次标本负责,如有疑问请七日内复查。", font));
- tipCell.setColspan(8);
- tipCell.setHorizontalAlignment(Element.ALIGN_LEFT);
- tipCell.setPaddingTop(10);
- tipCell.disableBorderSide(15);
- table.addCell(tipCell);
- table.setSplitLate(false);
- document.add(table);
- }
- entryIndex += 1;
- }
- document.newPage();
- }
- private static Paragraph getTailPageLogo(BaseFont baseFont) throws Exception {
- Image logo = Image.getInstance(LOGO_PATH);
- logo.scaleAbsolute(240, 110);
- Chunk logoChunk = new Chunk(logo, 0, -260, false);
- Paragraph paragraph = new Paragraph();
- paragraph.setAlignment(Element.ALIGN_CENTER);
- paragraph.add(logoChunk);
- paragraph.add(Chunk.NEWLINE);
- Font font = new Font(baseFont, 30, Font.NORMAL);
- Chunk ending = new Chunk("泰和伴您,健康同行!");
- ending.setFont(font);
- ending.setTextRise(-330);
- paragraph.add(ending);
- return paragraph;
- }
- }
|