您的位置:首页 > 健康 > 美食 > 六安政府网_seo搜索优化软件_优化网站广告优化_百度一下主页官网

六安政府网_seo搜索优化软件_优化网站广告优化_百度一下主页官网

2024/12/23 22:30:36 来源:https://blog.csdn.net/fuyuan98520/article/details/144421980  浏览:    关键词:六安政府网_seo搜索优化软件_优化网站广告优化_百度一下主页官网
六安政府网_seo搜索优化软件_优化网站广告优化_百度一下主页官网

一、单个PDF导出到浏览器
 

    /*** * @param templatePath 模板路径* @param fileName 文件名称* @param data 填充文本* @param images 填充图片* @param response* @throws IOException*/public static void generateTempPDF(String templatePath, String fileName, Map<String, String> data , Map<String, String> images, HttpServletResponse response) throws IOException {fileName = URLEncoder.encode(fileName, "UTF-8");// 设置响应头response.setContentType("application/force-download");response.setHeader("Content-Disposition","attachment;fileName=" + fileName);PdfReader reader = null;PdfStamper ps = null;OutputStream fos = null;ByteArrayOutputStream bos = null;try {//模板路径templatePathreader = new PdfReader(templatePath);bos = new ByteArrayOutputStream();ps = new PdfStamper(reader, bos);// 使用中文字体BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);ArrayList<BaseFont> fontList = new ArrayList<>();fontList.add(bf);AcroFields fields = ps.getAcroFields();fields.setSubstitutionFonts(fontList);fillData(fields, data);//渲染//插入图片if (EmptyUtils.isNotEmpty(images)) {fillImage(fields, ps,images);}//必须要调用这个,否则文档不会生成的ps.setFormFlattening(true);if(ps != null){ps.close();}// 保存到本地// fos = new FileOutputStream(exportPath);// 输出到浏览器端fos = response.getOutputStream();fos.write(bos.toByteArray());}catch (Exception e){e.printStackTrace();}finally {if(fos!=null){fos.flush();fos.close();}if (bos != null){bos.close();}if(reader != null){reader.close();}}}

二、多个PDF文件压缩ZIP输出浏览器

 @Overridepublic void exportZip(List<Long> ids, HttpServletResponse response) throws IOException {response.setContentType("application/zip");response.setHeader("Content-Disposition", "attachment; filename=weighbridge_records.zip");try (ZipOutputStream zos = new ZipOutputStream(response.getOutputStream())) {for (Long id: ids) {// 业务代码String fileName = aaa.pdf;Map<String, String> data = new HashMap<>();// 生成 PDF 文件到 ByteArrayOutputStreamByteArrayOutputStream bos = new ByteArrayOutputStream();//模板路径String templatePath = templateProperties.getPath() + "\\weighbridge_record.pdf";generateTempPDF(templatePath, fileName, data, null, bos);// 将生成的 PDF 文件写入 ZIPZipEntry zipEntry = new ZipEntry(fileName);zos.putNextEntry(zipEntry);zos.write(bos.toByteArray());zos.closeEntry();bos.close();}} catch (IOException e) {e.printStackTrace();} catch (DocumentException e) {throw new RuntimeException(e);}}
public static void generateTempPDF(String templatePath, String fileName, Map<String, String> data, Map<String, String> images, ByteArrayOutputStream bos) throws IOException, DocumentException {PdfReader reader = null;PdfStamper ps = null;try {reader = new PdfReader(templatePath);ps = new PdfStamper(reader, bos);BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);ArrayList<BaseFont> fontList = new ArrayList<>();fontList.add(bf);AcroFields fields = ps.getAcroFields();fields.setSubstitutionFonts(fontList);fillData(fields, data); // 渲染// 插入图片if (EmptyUtils.isNotEmpty(images)) {fillImage(fields, ps, images);}ps.setFormFlattening(true);} catch (Exception e) {e.printStackTrace();} finally {if (ps != null) {ps.close();}if (reader != null) {reader.close();}}}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com