Commit 7f8a546ff958a0a149251a29a47ae7528770c48b
1 parent
b1d49d5012
Exists in
master
and in
6 other branches
导出pdf
Showing 3 changed files with 251 additions and 18 deletions
platform-operate-api/pom.xml
View file @
7f8a546
... | ... | @@ -129,6 +129,16 @@ |
129 | 129 | <artifactId>jxls-poi</artifactId> |
130 | 130 | <version>1.0.9</version> |
131 | 131 | </dependency> |
132 | + <dependency> | |
133 | + <groupId>com.itextpdf</groupId> | |
134 | + <artifactId>itextpdf</artifactId> | |
135 | + <version>5.5.13</version> | |
136 | + </dependency> | |
137 | + <dependency> | |
138 | + <groupId>com.itextpdf</groupId> | |
139 | + <artifactId>itext-asian</artifactId> | |
140 | + <version>5.2.0</version> | |
141 | + </dependency> | |
132 | 142 | <!--<dependency>--> |
133 | 143 | <!--<groupId>com.fasterxml.jackson.core</groupId>--> |
134 | 144 | <!--<artifactId>jackson-core</artifactId>--> |
platform-operate-api/src/main/java/com/lyms/hospitalapi/lcdcf/LcdcfHisService.java
View file @
7f8a546
1 | 1 | package com.lyms.hospitalapi.lcdcf; |
2 | 2 | |
3 | -import com.lyms.hospitalapi.pojo.PregPatientinfo; | |
4 | -import com.lyms.platform.common.utils.DateUtil; | |
3 | +import java.sql.Connection; | |
4 | +import java.sql.SQLException; | |
5 | +import java.text.SimpleDateFormat; | |
6 | +import java.util.ArrayList; | |
7 | +import java.util.Date; | |
8 | +import java.util.HashMap; | |
9 | +import java.util.List; | |
10 | +import java.util.Map; | |
11 | + | |
12 | +import javax.servlet.http.HttpServletResponse; | |
13 | + | |
5 | 14 | import org.apache.commons.collections.CollectionUtils; |
6 | 15 | import org.apache.commons.dbutils.DbUtils; |
7 | 16 | import org.apache.commons.dbutils.QueryRunner; |
8 | 17 | import org.apache.commons.dbutils.handlers.BeanListHandler; |
9 | 18 | import org.springframework.stereotype.Service; |
10 | 19 | |
11 | -import java.sql.Connection; | |
12 | -import java.sql.SQLException; | |
13 | -import java.text.SimpleDateFormat; | |
14 | -import java.util.*; | |
20 | +import com.alibaba.fastjson.JSONObject; | |
21 | +import com.itextpdf.text.BaseColor; | |
22 | +import com.itextpdf.text.Document; | |
23 | +import com.itextpdf.text.DocumentException; | |
24 | +import com.itextpdf.text.Element; | |
25 | +import com.itextpdf.text.Font; | |
26 | +import com.itextpdf.text.FontFactory; | |
27 | +import com.itextpdf.text.PageSize; | |
28 | +import com.itextpdf.text.Paragraph; | |
29 | +import com.itextpdf.text.Phrase; | |
30 | +import com.itextpdf.text.Rectangle; | |
31 | +import com.itextpdf.text.pdf.BaseFont; | |
32 | +import com.itextpdf.text.pdf.PdfPCell; | |
33 | +import com.itextpdf.text.pdf.PdfPTable; | |
34 | +import com.itextpdf.text.pdf.PdfWriter; | |
35 | +import com.lyms.hospitalapi.pojo.PregPatientinfo; | |
36 | +import com.lyms.platform.common.utils.DateUtil; | |
15 | 37 | |
16 | 38 | /** |
17 | 39 | * Created by Administrator on 2017-12-05. |
... | ... | @@ -156,5 +178,184 @@ |
156 | 178 | return ""; |
157 | 179 | } |
158 | 180 | |
181 | + public void exportPdf(JSONObject jsonObject, HttpServletResponse response) throws Exception { | |
182 | + response.setContentType("application/pdf"); | |
183 | + response.setHeader("Expires","0"); | |
184 | + response.setHeader("Cache-Control","must-revalidate, post-check=0, pre-check=0"); | |
185 | + response.setHeader("Pragma","public"); | |
186 | + | |
187 | + Rectangle rect = new Rectangle(PageSize.A4);//文档竖方向 | |
188 | + Document doc = new Document(rect); | |
189 | + PdfWriter writer = PdfWriter.getInstance(doc, response.getOutputStream()); | |
190 | + //PDF版本(默认1.4) | |
191 | + writer.setPdfVersion(PdfWriter.PDF_VERSION_1_2); | |
192 | + //文档属性 | |
193 | + //页边空白 | |
194 | + doc.setMargins(20, 20, 40, 40); | |
195 | + //打开文档 | |
196 | + doc.open(); | |
197 | + try { | |
198 | + createFile(doc, jsonObject); | |
199 | + } catch (DocumentException e) { | |
200 | + e.printStackTrace(); | |
201 | + } | |
202 | + doc.close(); | |
203 | + } | |
204 | + | |
205 | + public void createFile(Document doc, JSONObject jsonObject) throws DocumentException { | |
206 | + String reportName = jsonObject.getString("title"); | |
207 | + String hospitalName = jsonObject.get("hospitalName").toString(); | |
208 | + String age = jsonObject.get("age").toString(); | |
209 | + String sexCode = jsonObject.get("sex").toString(); | |
210 | + String sex = sexCode.equals("1") ? "男" : sexCode.equals("2") ? "女" : "未知"; | |
211 | + String name = jsonObject.get("name").toString(); | |
212 | + String checkDept = jsonObject.get("checkDept").toString(); | |
213 | + String doctor = jsonObject.get("doctor").toString(); | |
214 | + | |
215 | + doc.add(getFont(1, reportName + "报告单")); | |
216 | + doc.add(getFont(7, hospitalName)); | |
217 | + | |
218 | + PdfPTable table = new PdfPTable(35); | |
219 | + table.getDefaultCell().setBorder(PdfPCell.NO_BORDER); | |
220 | + | |
221 | + setTitle(age, sex, name, checkDept, doctor, table); | |
222 | + | |
223 | + PdfPCell blankRow = new PdfPCell(new Phrase("\n")); | |
224 | + blankRow.setFixedHeight(20f); | |
225 | + blankRow.setColspan(35); | |
226 | + blankRow.setBorder(Rectangle.NO_BORDER); | |
227 | + table.addCell(blankRow); | |
228 | + doc.add(table); | |
229 | + | |
230 | + PdfPCell cell; | |
231 | + | |
232 | + PdfPTable pdfPTable = new PdfPTable(7); | |
233 | + pdfPTable.setHeaderRows(1); | |
234 | + cell = new PdfPCell(new Phrase(getFont(5, "项目"))); | |
235 | + cell.setColspan(2); | |
236 | + pdfPTable.addCell(cell); | |
237 | + pdfPTable.addCell(new PdfPCell(new Phrase(getFont(5, "代码")))); | |
238 | + pdfPTable.addCell(new PdfPCell(new Phrase(getFont(5, "结果")))); | |
239 | + pdfPTable.addCell(new PdfPCell(new Phrase(getFont(5, "异常")))); | |
240 | + pdfPTable.addCell(new PdfPCell(new Phrase(getFont(5, "参考值")))); | |
241 | + pdfPTable.addCell(new PdfPCell(new Phrase(getFont(5, "单位")))); | |
242 | + List<HashMap> items = (List<HashMap>) jsonObject.get("itemList"); | |
243 | + for (HashMap map : items) { | |
244 | + cell = new PdfPCell(new Phrase(getFont(5, map.get("name").toString()))); | |
245 | + cell.setColspan(2); | |
246 | + pdfPTable.addCell(cell); | |
247 | + cell = new PdfPCell(new Phrase(getFont(5, map.get("code").toString()))); | |
248 | + pdfPTable.addCell(cell); | |
249 | + cell = new PdfPCell(new Phrase(getFont(5, map.get("result").toString()))); | |
250 | + pdfPTable.addCell(cell); | |
251 | + cell = new PdfPCell(new Phrase(getFont(5, map.get("special").toString()))); | |
252 | + pdfPTable.addCell(cell); | |
253 | + cell = new PdfPCell(new Phrase(getFont(5, map.get("refer").toString()))); | |
254 | + pdfPTable.addCell(cell); | |
255 | + cell = new PdfPCell(new Phrase(getFont(5, map.get("unit").toString()))); | |
256 | + pdfPTable.addCell(cell); | |
257 | + } | |
258 | + doc.add(pdfPTable); | |
259 | + doc.close(); | |
260 | + | |
261 | + | |
262 | + } | |
263 | + | |
264 | + private static void setTitle( String age, String sex, String name, String checkDept, String doctor, PdfPTable table) throws DocumentException { | |
265 | + PdfPCell cell; | |
266 | + cell = new PdfPCell(new Phrase(getFont(5, "年龄:" + age))); | |
267 | + cell.setColspan(6); | |
268 | + cell.setBorder(0); | |
269 | + table.addCell(cell); | |
270 | + cell = new PdfPCell(new Phrase(getFont(5, "性别:" + sex))); | |
271 | + cell.setColspan(5); | |
272 | + cell.setBorder(0); | |
273 | + table.addCell(cell); | |
274 | + cell = new PdfPCell(new Phrase(getFont(5, "姓名:" + name))); | |
275 | + cell.setColspan(6); | |
276 | + cell.setBorder(0); | |
277 | + table.addCell(cell); | |
278 | + cell = new PdfPCell(new Phrase(getFont(5, "申请科室:" + checkDept))); | |
279 | + cell.setColspan(10); | |
280 | + cell.setBorder(0); | |
281 | + table.addCell(cell); | |
282 | + cell = new PdfPCell(new Phrase(getFont(5, "申请医生:" + doctor))); | |
283 | + cell.setColspan(8); | |
284 | + cell.setBorder(0); | |
285 | + table.addCell(cell); | |
286 | + } | |
287 | + | |
288 | + public static PdfPTable createTable(float[] widths) { | |
289 | + PdfPTable table = new PdfPTable(widths); | |
290 | + try { | |
291 | + table.setLockedWidth(true); | |
292 | + table.setHorizontalAlignment(Element.ALIGN_CENTER); | |
293 | + table.getDefaultCell().setBorder(1); | |
294 | + } catch (Exception e) { | |
295 | + e.printStackTrace(); | |
296 | + } | |
297 | + return table; | |
298 | + } | |
299 | + | |
300 | + /** | |
301 | + * 文档超级 排版 | |
302 | + * @param type 1-标题 2-标题一 3-标题二 4-标题三 5-正文 6-左对齐 | |
303 | + */ | |
304 | + public static Paragraph getFont(int type, String text){ | |
305 | + Font font = FontFactory.getFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED,10f, Font.NORMAL, BaseColor.BLACK); | |
306 | + if(1 == type){//1-标题 | |
307 | + font.setSize(16f); | |
308 | + font.setStyle(Font.BOLD); | |
309 | + } else if(2 == type){//2-标题一 | |
310 | + font.setSize(16f); | |
311 | + font.setStyle(Font.BOLD); | |
312 | + } else if(3 == type){//3-标题二 | |
313 | + font.setSize(14f); | |
314 | + font.setStyle(Font.BOLD); | |
315 | + } else if(4 == type){//4-标题三 | |
316 | + font.setSize(14f); | |
317 | + } else if(5 == type){//5-正文 | |
318 | + font.setSize(10.5f); | |
319 | + } else if(6 == type){//6-左对齐 | |
320 | + font.setSize(10.5f); | |
321 | + } else if (7 == type) { | |
322 | + font.setSize(12f); | |
323 | + font.setStyle(Font.BOLD); | |
324 | + } else { | |
325 | + font.setSize(10.5f);//默认大小 | |
326 | + } | |
327 | + //注: 字体必须和 文字一起new | |
328 | + Paragraph paragraph = new Paragraph(text, font); | |
329 | + if(1 == type){ | |
330 | + paragraph.setAlignment(Paragraph.ALIGN_CENTER);//居中 | |
331 | + paragraph.setSpacingBefore(10f);//上间距 | |
332 | + paragraph.setSpacingAfter(10f);//下间距 | |
333 | + } else if(2 == type){//2-标题一 | |
334 | + paragraph.setAlignment(Element.ALIGN_JUSTIFIED); //默认 | |
335 | + paragraph.setSpacingBefore(2f);//上间距 | |
336 | + paragraph.setSpacingAfter(2f);//下间距 | |
337 | + } else if(3 == type){ | |
338 | + paragraph.setSpacingBefore(2f);//上间距 | |
339 | + paragraph.setSpacingAfter(1f);//下间距 | |
340 | + } else if(4 == type){//4-标题三 | |
341 | + //paragraph.setAlignment(Element.ALIGN_RIGHT);//右对齐 | |
342 | + paragraph.setSpacingBefore(2f);//上间距 | |
343 | + paragraph.setSpacingAfter(2f);//下间距 | |
344 | + } else if(5 == type){ | |
345 | + paragraph.setAlignment(Element.ALIGN_JUSTIFIED); | |
346 | + paragraph.setFirstLineIndent(24);//首行缩进 | |
347 | + paragraph.setSpacingBefore(1f);//上间距 | |
348 | + paragraph.setSpacingAfter(1f);//下间距 | |
349 | + } else if(6 == type){//左对齐 | |
350 | + paragraph.setAlignment(Element.ALIGN_LEFT); | |
351 | + paragraph.setSpacingBefore(1f);//上间距 | |
352 | + paragraph.setSpacingAfter(1f);//下间距 | |
353 | + } else if (7 == type) { | |
354 | + paragraph.setAlignment(Paragraph.ALIGN_CENTER);//居中 | |
355 | + paragraph.setSpacingBefore(8f);//上间距 | |
356 | + paragraph.setSpacingAfter(30f);//下间距 | |
357 | + } | |
358 | + return paragraph; | |
359 | + } | |
159 | 360 | } |
platform-operate-api/src/main/java/com/lyms/platform/operate/web/controller/LisController.java
View file @
7f8a546
1 | 1 | package com.lyms.platform.operate.web.controller; |
2 | 2 | |
3 | +import java.util.ArrayList; | |
4 | +import java.util.HashMap; | |
5 | +import java.util.List; | |
6 | +import java.util.Map; | |
7 | + | |
8 | +import javax.servlet.http.HttpServletRequest; | |
9 | +import javax.servlet.http.HttpServletResponse; | |
10 | + | |
11 | +import org.apache.commons.collections.CollectionUtils; | |
12 | +import org.springframework.beans.factory.annotation.Autowired; | |
13 | +import org.springframework.beans.factory.annotation.Qualifier; | |
14 | +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | |
15 | +import org.springframework.stereotype.Controller; | |
16 | +import org.springframework.web.bind.annotation.RequestBody; | |
17 | +import org.springframework.web.bind.annotation.RequestHeader; | |
18 | +import org.springframework.web.bind.annotation.RequestMapping; | |
19 | +import org.springframework.web.bind.annotation.RequestMethod; | |
20 | +import org.springframework.web.bind.annotation.RequestParam; | |
21 | +import org.springframework.web.bind.annotation.ResponseBody; | |
22 | + | |
23 | +import com.alibaba.fastjson.JSONObject; | |
3 | 24 | import com.lyms.hospitalapi.lcdcf.LcdcfHisService; |
4 | 25 | import com.lyms.platform.common.annotation.TokenRequired; |
5 | 26 | import com.lyms.platform.common.base.BaseController; |
6 | 27 | |
... | ... | @@ -15,19 +36,7 @@ |
15 | 36 | import com.lyms.platform.permission.model.LisReportItemModel; |
16 | 37 | import com.lyms.platform.permission.model.LisReportModel; |
17 | 38 | import com.lyms.platform.permission.service.LisService; |
18 | -import org.apache.commons.collections.CollectionUtils; | |
19 | -import org.springframework.beans.factory.annotation.Autowired; | |
20 | -import org.springframework.beans.factory.annotation.Qualifier; | |
21 | -import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | |
22 | -import org.springframework.stereotype.Controller; | |
23 | -import org.springframework.web.bind.annotation.*; | |
24 | 39 | |
25 | -import javax.servlet.http.HttpServletRequest; | |
26 | -import java.util.ArrayList; | |
27 | -import java.util.HashMap; | |
28 | -import java.util.List; | |
29 | -import java.util.Map; | |
30 | - | |
31 | 40 | /** |
32 | 41 | * Created by lqy on 2017-04-11. |
33 | 42 | */ |
... | ... | @@ -262,6 +271,19 @@ |
262 | 271 | HttpServletRequest request) { |
263 | 272 | return new BaseObjectResponse().setErrorcode(ErrorCodeConstants.SUCCESS). |
264 | 273 | setErrormsg(ErrorCodeConstants.SUCCESS_DESCRIPTION).setData(lcdcfHisService.getPatientCheckLisItems(vcCardNo)); |
274 | + } | |
275 | + | |
276 | + /** | |
277 | + * 导出pdf | |
278 | + * | |
279 | + * @param jsonObject | |
280 | + * @param response | |
281 | + * @return | |
282 | + */ | |
283 | + @RequestMapping(method = RequestMethod.POST, value = "/exportReportPdf") | |
284 | + @TokenRequired | |
285 | + public void exportPdf(@RequestBody JSONObject jsonObject, HttpServletResponse response) throws Exception { | |
286 | + lcdcfHisService.exportPdf(jsonObject, response); | |
265 | 287 | } |
266 | 288 | } |