@ResponseBody
@RequestMapping(value = "/printing")
public void printing(@RequestParam Map<String, String> queryParams, HttpServletResponse response) throws IOException, JRException {
String ClassPath=fileBasePath+"/ireport/template/";
// File file=new File(this.getClass().getResource("/").getPath());
//截取絕對路徑,不能把項目放到絕對路徑下
// String classPath=file.toString().substring(0,file.toString().length()-30)+"src/main/resources/META-INF/public/ymsweb/waybill/dispatchPrint/";
// URL url = Thread.currentThread().getContextClassLoader().getResource("META-INF/public/ymsweb/waybill/dispatchPrint/zhuancangdan.jasper");
// URL url2 = Thread.currentThread().getContextClassLoader().getResource("META-INF/public/ymsweb/waybill/dispatchPrint/chuhuodan.jasper");
String path =ClassPath+"zhuancangdan.jasper";
String path2 =ClassPath+"chuhuodan.jasper";
Map<String, Object> parameters = new HashMap<>();
parameters.put(JRParameter.REPORT_LOCALE, new Locale("zh_cn"));//設置打印簡體或者繁體
ServletOutputStream outputStream = null;
List jasperPrintList = new ArrayList();
UserDetails userDetails = UserContextHolder.getCurrentUser();
String removeAllWorking = queryParams.get("vrdoNos").replace("[", "").replace("]", "");
String[] vrdoNos=null;
if (!removeAllWorking.isEmpty()) {
String[] arr = removeAllWorking.split(",");
vrdoNos = new String[arr.length];
for (int i = 0; i < arr.length; i++) {
String id = String.valueOf(arr[i]);
vrdoNos[i] = id;
}
}
//判斷是託運單還是轉倉單,true爲轉倉單,false爲出貨單
if(vrDispatchOrderService.orderType(vrdoNos[i])){
//用託運單去查詢該託運單下的所有訂單
List<PrintingZhuanCangDanBean> printingZhuanCangDanBeans =vrDispatchOrderService.printingZhuanCangDan(vrdoNos[i]);
if(EmptyUtils.isEmpty(printingZhuanCangDanBeans)){
response.setCharacterEncoding("UTF-8");
response.sendError(404,"該託運單號"+vrdoNos[i]+"沒有數據");
}
for (int j = 0; j< printingZhuanCangDanBeans.size(); j++){
printingZhuanCangDanBeans.get(j).setPrintUser(userDetails.getUsername());
}
JRDataSource source=new JRBeanCollectionDataSource(printingZhuanCangDanBeans);
JasperPrint jasperPrint = JasperFillManager.fillReport(path, parameters, source);
jasperPrintList.add(jasperPrint);
}else{
List<PrintingChuHuoDanBean> printingChuHuoDanBeans=vrDispatchOrderService.printingChuHuoDan(vrdoNos[i]);
if(EmptyUtils.isEmpty(printingChuHuoDanBeans)){
response.setCharacterEncoding("UTF-8");
response.sendError(404,"該託運單號"+vrdoNos[i]+"沒有數據");
}
for (int j = 0; j< printingChuHuoDanBeans.size(); j++){
printingChuHuoDanBeans.get(j).setPrintUser(userDetails.getUsername());
}
JRDataSource source=new JRBeanCollectionDataSource(printingChuHuoDanBeans);
JasperPrint jasperPrint = JasperFillManager.fillReport(path2, parameters, source);
jasperPrintList.add(jasperPrint);
// JRDataSource source=new JRBeanCollectionDataSource(printingDispatchOrderBeans);
// JasperPrint jasperPrint = JasperFillManager.fillReport(path, parameters, source);
// jasperPrintList.add(jasperPrint);
}
}
//獲取輸出字節流
outputStream = response.getOutputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
JRPdfExporter exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST,jasperPrintList);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
exporter.exportReport();
byte[] bytes= baos.toByteArray();
response.setContentType("application/pdf;charset=UTF-8");
outputStream.write(bytes);
outputStream.flush();
outputStream.close();
}