PDF技术之-jasperreports的使用
使用过程中会出现中文没有显示的问题。
解决方法:
在设置jasper文件的过程中设置其中的中文的字体。我选择的是华文宋体。
设置好字段的中文的字体样式。就把这个jasper文件放到我的springboot项目中。
我项目的格式为:
这里有从网上找到的stsong.TTF为华文宋体的样式。自己配置的fonts.xml字体配置。
其中
fonts.xml配置为:
<?xml version="1.0" encoding="UTF-8"?> <fontFamilies> <!--<fontFamily name="Lobster Two">--> <!--<normal>lobstertwo/LobsterTwo-Regular.otf</normal>--> <!--<bold>lobstertwo/LobsterTwo-Bold.otf</bold>--> <!--<italic>lobstertwo/LobsterTwo-Italic.otf</italic>--> <!--<boldItalic>lobstertwo/LobsterTwo-BoldItalic.otf</boldItalic>--> <!--<pdfEncoding>Identity-H</pdfEncoding>--> <!--<pdfEmbedded>true</pdfEmbedded>--> <!--<!–--> <!--<exportFonts>--> <!--<export key="net.sf.jasperreports.html">'Lobster Two', 'Times New Roman', Times, serif</export>--> <!--</exportFonts>--> <!--–>--> <!--</fontFamily>--> <fontFamily name="华文宋体"> <normal>stsong/stsong.TTF</normal> <bold>stsong/stsong.TTF</bold> <italic>stsong/stsong.TTF</italic> <boldItalic>stsong/stsong.TTF</boldItalic> <pdfEncoding>Identity-H</pdfEncoding> <pdfEmbedded>true</pdfEmbedded> <exportFonts> <export key="net.sf.jasperreports.html">'华文宋体', Arial, Helvetica, sans-serif</export> <export key="net.sf.jasperreports.xhtml">'华文宋体', Arial, Helvetica, sans-serif</export> </exportFonts> <!--<locales> <locale>en_US</locale> <locale>de_DE</locale> </locales> --> </fontFamily> </fontFamilies>
jasperreports_extension.properties配置文件为:
net.sf.jasperreports.extension.registry.factory.simple.font.families=net.sf.jasperreports.engine.fonts.SimpleFontExtensionsRegistryFactory
net.sf.jasperreports.extension.simple.font.families.lobstertwo=stsong/fonts.xml
java的代码:
传入parameters值的时候,在设置jasper文件的parameters的值就是使用map传入对应的值的。这里不是filed的,第三个参数才是表格字段filed的。
@RequestMapping(value = "/testJaper01", method = RequestMethod.GET) public void createHtml(HttpServletResponse response, HttpServletRequest request) throws IOException { //引入jasper文件 ClassPathResource resource = new ClassPathResource("templates/parameters.jasper"); FileInputStream fis = new FileInputStream(resource.getFile()); ServletOutputStream outputStream = response.getOutputStream(); //模板的参数名称和Map的key一致的 HashMap<String, Object> map = new HashMap<String, Object>(); map.put("id", "111"); map.put("username", "zs张三"); map.put("companyName", "caicai公司"); map.put("mobile", "15222222222"); try { //这个map设置的就是parameters的值,并非是filed的 JasperPrint print = JasperFillManager.fillReport(fis, map, new JREmptyDataSource()); /*导出PDF*/ JasperExportManager.exportReportToPdfStream(print, response.getOutputStream()); } catch (Exception e) { e.printStackTrace(); } finally { outputStream.flush(); outputStream.close(); fis.close(); } }
使用jdbc连接数据库拿到数组渲染到表格
@GetMapping("/testJaper02") public void createPDF(HttpServletResponse response, HttpServletRequest request) throws IOException { //1.引入文件 ClassPathResource resource = new ClassPathResource("templates/testjdbc.jasper"); FileInputStream fileInputStream = new FileInputStream(resource.getFile()); //2.创建jasperPrint,向jasper文件中填充数据 ServletOutputStream outputStream = response.getOutputStream(); try { //这里由于是使用到了jdbc直接连接上数据库,所有这个map不存入任何值。 HashMap<String, Object> parameters = new HashMap<String, Object>(); JasperPrint print = JasperFillManager.fillReport(fileInputStream, parameters, getConnection()); //3.将jasperPrint以PDF的形式输出 JasperExportManager.exportReportToPdfStream(print, outputStream); // response.setContentType("application/pdf"); } catch (Exception e) { e.printStackTrace(); } finally { outputStream.flush(); outputStream.close(); fileInputStream.close(); } } public Connection getConnection() { Connection connection = null; try { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/ssm"; connection = DriverManager.getConnection(url, "root", "root"); } catch (Exception e) { e.printStackTrace(); } return connection; }
使用javabean来渲染数据
@GetMapping("/testjavaBean") public void createPDF2(HttpServletRequest request, HttpServletResponse response) throws IOException { ClassPathResource resource = new ClassPathResource("templates/testJavaBean.jasper"); FileInputStream fis = new FileInputStream(resource.getFile()); ServletOutputStream os = response.getOutputStream(); try { HashMap<String, Object> map = new HashMap<String, Object>(); /* ArrayList<User> arrayList = new ArrayList<User>(10); for (int i = 0; i < 10; i++) { User user = new User(i, "m00" + i, "小" + i, "别名" + i, "pic" + i, "状态" + i); arrayList.add(user); }*/ List<User> arrayList = userService.findAllUser(); JRBeanCollectionDataSource ds= new JRBeanCollectionDataSource(arrayList); /**1.jasper文件流 * 2.参数列表 * 3.JRBeanCollectionDataSource*/ JasperPrint jasperPrint = JasperFillManager.fillReport(fis, map, ds); JasperExportManager.exportReportToPdfStream(jasperPrint,os); response.setContentType("application/pdf"); } catch (Exception e) { e.printStackTrace(); }finally { os.flush(); } }
在返回给前端的问题:
一种是以pdf文件显示或者导出
在浏览器显示的输出配置;
/** * 参数一:封装好模板好数据的JasperPrint对象 * 参数二:输出的位置 */ JasperExportManager.exportReportToPdfStream(print,response.getOutputStream());
下载的配置
/** 参数:封装好模板好数据的JasperPrint对象 * 参数二:输出的位置*/ //设置下载框响应头 response.setHeader("Content-Disposition","attachment;filename=export.pdf"); //参数一是响应头,参数而,是附件和下载后的文件名 JasperExportManager.exportReportToPdfStream(print,response.getOutputStream()); //这一步同上
https://wenku.baidu.com/link?url=HrWPdhA-kf-dCDnoak9kfYaB7PzoYj1foufQ0kYPfVEcte6s5qkTh5ENhqDZhnknzVKwMqdzRMKZyagPYlnwdXgTt7fS_aSmPA4FLiZoqEC
posted on 2021-03-12 04:39 fyccaishao 阅读(577) 评论(0) 编辑 收藏 举报