java调用没有嵌套table的jasper文件生成PDF返回到页面

@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();
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值