当前位置: 代码迷 >> 综合 >> PDF展示,下载,Java用 PDFObject 实现数据直接以流返回前台直接展示并提供下载(PDF文件)
  详细解决方案

PDF展示,下载,Java用 PDFObject 实现数据直接以流返回前台直接展示并提供下载(PDF文件)

热度:31   发布时间:2024-01-22 03:51:46.0

一:首先项目要添加maven依赖

        <!-- pdf start --><dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.5.10</version></dependency><dependency><groupId>com.itextpdf</groupId><artifactId>itext-asian</artifactId><version>5.2.0</version></dependency><!-- pdf end -->

二:需要在前台页面引入pdfobject.js


js的下载地址:
1:我已经打包下载好的下载地址:https://download.csdn.net/download/sgl520lxl/11246388
2:官方github下载地址:https://github.com/pipwerks/PDFObject/

 

 

三:下载好js源码请在你要用的前台页面引入pdfobject.js

<!--引入pdfobject.js-->
<script type="text/javascript" src="/assets/pdfobject/pdfobject.js"></script>
<!--这是设置浏览pdf页面的大小--> 
<style> 
.pdfobject-container { height: 800rem; border: 1rem solid rgba(0,0,0,.1); } 
</style>

 

<!--设置一个div用来存放展示页面-->

<div id="pdfId"></div>//生成pdf浏览(后台生成返回流的action接口地址)
$("#selectSeach").click(function(){var url = [[@{/print/createPdfStream}]];url = url +"?date="+s_payDate;//把流画到id为pdfId div中展示pdfPDFObject.embed(url, "#pdfId");
});

四:后台action接口,返回把数据以流返回

/**
* @Author Ni Klaus
* @Description //TODO 根据条件生成pdf流
* @Date 下午 14:58 2019/6/17 0017
* @Param [response, request, session]
* @return void
**/@RequestMapping("createPdfStream")public void getCloudAutQRCode(HttpServletResponse response, HttpServletRequest                          request, HttpSession session) throws IOException {//数据库查询出来的数据,我这里直接省略了查询的代码,直接生成对象StatisticsResult statisticsResult = new StatisticsResult();statisticsResult.setTime(date);statisticsResult.setWindowStr(windowName);List<ChargeResult> list = new ArrayList<>();try {Document document =new Document();ByteArrayOutputStream baos = new ByteArrayOutputStream();PdfWriter.getInstance(document,baos);//设置字体BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);com.itextpdf.text.Font FontChinese24 = new com.itextpdf.text.Font(bfChinese, 24, com.itextpdf.text.Font.BOLD);com.itextpdf.text.Font FontChinese18 = new com.itextpdf.text.Font(bfChinese, 18, com.itextpdf.text.Font.BOLD);com.itextpdf.text.Font FontChinese16 = new com.itextpdf.text.Font(bfChinese, 16, com.itextpdf.text.Font.BOLD);com.itextpdf.text.Font FontChinese12 = new com.itextpdf.text.Font(bfChinese, 12, com.itextpdf.text.Font.NORMAL);com.itextpdf.text.Font FontChinese9 = new com.itextpdf.text.Font(bfChinese, 9, com.itextpdf.text.Font.NORMAL);com.itextpdf.text.Font FontChinese8Bold = new com.itextpdf.text.Font(bfChinese, 8, com.itextpdf.text.Font.BOLD);com.itextpdf.text.Font FontChinese11 = new com.itextpdf.text.Font(bfChinese, 11, com.itextpdf.text.Font.ITALIC);com.itextpdf.text.Font FontChinese11Normal = new com.itextpdf.text.Font(bfChinese, 11, com.itextpdf.text.Font.NORMAL);document.open();//table1PdfPTable table1 = new PdfPTable(1);PdfPCell cell11 = new PdfPCell(new Paragraph("窗口收费日结单",FontChinese24));cell11.setVerticalAlignment(Element.ALIGN_MIDDLE);cell11.setHorizontalAlignment(Element.ALIGN_CENTER);cell11.setBorder(0);//设置每列宽度比例int width11[] = {100};table1.setWidths(width11);table1.getDefaultCell().setBorder(0);table1.addCell(cell11);document.add(table1);//加入空行Paragraph blankRow1 = new Paragraph(18f, " ", FontChinese18);document.add(blankRow1);//table2PdfPTable table2 = new PdfPTable(2);//设置每列宽度比例int width21[] = {2,98};table2.setWidths(width21);table2.getDefaultCell().setBorder(0);PdfPCell cell21 = new PdfPCell(new Paragraph("日结统计",FontChinese16));cell21.setBorder(0);table2.addCell(cell21);document.add(table2);//加入空行Paragraph blankRow2 = new Paragraph(18f, " ", FontChinese18);document.add(blankRow2);//table3PdfPTable table3 = new PdfPTable(2);int width3[] = {50,50};table3.setWidths(width3);PdfPCell cell31 = new PdfPCell(new Paragraph("窗口:"+statisticsResult.getWindowStr(),FontChinese11Normal));PdfPCell cell32 = new PdfPCell(new Paragraph("日期:"+statisticsResult.getTime(),FontChinese11Normal));cell31.setBorder(0);cell32.setBorder(0);table3.addCell(cell31);table3.addCell(cell32);document.add(table3);//加入空行Paragraph blankRow31 = new Paragraph(18f, " ", FontChinese11);document.add(blankRow31);//table4PdfPTable table4 = new PdfPTable(2);int width4[] = {50,50};table4.setWidths(width4);PdfPCell cell41 = new PdfPCell(new Paragraph("今日笔数:"+statisticsResult.getCounts(),FontChinese11Normal));PdfPCell cell42 = new PdfPCell(new Paragraph("总金额:"+statisticsResult.getAmount(),FontChinese11Normal));cell41.setBorder(0);cell42.setBorder(0);table4.addCell(cell41);table4.addCell(cell42);document.add(table4);//加入空行Paragraph blankRow41 = new Paragraph(18f, " ", FontChinese11);document.add(blankRow41);//加入空行Paragraph blankRow51 = new Paragraph(18f, " ", FontChinese11);document.add(blankRow51);PdfPTable table6 = new PdfPTable(1);int width22[] = {100};table6.getDefaultCell().setBorder(0);table6.setWidths(width22); PdfPCellcell61 = new PdfPCell(new Paragraph("收款明细:",FontChinese18));cell61.setBorder(0);table6.addCell(cell61);document.add(table6);//加入空行Paragraph blankRow52 = new Paragraph(18f, " ", FontChinese11);document.add(blankRow52);//画表格//table7PdfPTable table7 = new PdfPTable(6);BaseColor lightGrey = new BaseColor(0xCC,0xCC,0xCC);//20,10,13,15,17,25int width7[] = {20,15,13,15,25,25};table7.setWidths(width7);PdfPCell cell71 = new PdfPCell(new Paragraph("操作人",FontChinese8Bold));PdfPCell cell72 = new PdfPCell(new Paragraph("支付渠道",FontChinese8Bold));PdfPCell cell73 = new PdfPCell(new Paragraph("支付状态",FontChinese8Bold));PdfPCell cell74 = new PdfPCell(new Paragraph("金额",FontChinese8Bold));PdfPCell cell75 = new PdfPCell(new Paragraph("付款账号",FontChinese8Bold));PdfPCell cell76 = new PdfPCell(new Paragraph("创建时间",FontChinese8Bold));//表格高度cell71.setFixedHeight(25);cell72.setFixedHeight(25);cell73.setFixedHeight(25);cell74.setFixedHeight(25);cell75.setFixedHeight(25);cell76.setFixedHeight(25);//水平居中cell71.setHorizontalAlignment(Element.ALIGN_CENTER);cell72.setHorizontalAlignment(Element.ALIGN_CENTER);cell73.setHorizontalAlignment(Element.ALIGN_CENTER);cell74.setHorizontalAlignment(Element.ALIGN_CENTER);cell75.setHorizontalAlignment(Element.ALIGN_CENTER);cell76.setHorizontalAlignment(Element.ALIGN_CENTER);//垂直居中cell71.setVerticalAlignment(Element.ALIGN_MIDDLE);cell72.setVerticalAlignment(Element.ALIGN_MIDDLE);cell73.setVerticalAlignment(Element.ALIGN_MIDDLE);cell74.setVerticalAlignment(Element.ALIGN_MIDDLE);cell75.setVerticalAlignment(Element.ALIGN_MIDDLE);cell76.setVerticalAlignment(Element.ALIGN_MIDDLE);//边框颜色cell71.setBorderColor(lightGrey);cell72.setBorderColor(lightGrey);cell73.setBorderColor(lightGrey);cell74.setBorderColor(lightGrey);cell75.setBorderColor(lightGrey);cell76.setBorderColor(lightGrey);//去掉左右边框/*cell71.disableBorderSide(8);cell72.disableBorderSide(4);cell72.disableBorderSide(8);cell73.disableBorderSide(4);cell73.disableBorderSide(8);cell74.disableBorderSide(4);cell74.disableBorderSide(8);cell75.disableBorderSide(4);cell75.disableBorderSide(8);cell76.disableBorderSide(4);*/table7.addCell(cell71);table7.addCell(cell72);table7.addCell(cell73);table7.addCell(cell74);table7.addCell(cell75);table7.addCell(cell76);document.add(table7);if (!CollectionUtils.isEmpty(list)) {String payName = "未知";for (ChargeResult chargeResult : list) {if("wechat".equals(chargeResult.getPayCode())){payName ="微信";}if("ali".equals(chargeResult.getPayCode())){payName ="支付宝";}//table8PdfPTable table8 = new PdfPTable(6);int width8[] = {20,15,13,15,25,25};table8.setWidths(width8);PdfPCell cell81 = new PdfPCell(new Paragraph(chargeResult.getUserName(),FontChinese9));PdfPCell cell82 = new PdfPCell(new Paragraph(payName,FontChinese9));PdfPCell cell83 = new PdfPCell(new Paragraph(chargeResult.getStatusStr(),FontChinese9));PdfPCell cell84 = new PdfPCell(new Paragraph(chargeResult.getAmount().toString(),FontChinese9));PdfPCell cell85 = new PdfPCell(new Paragraph(chargeResult.getBuyerLogonId(),FontChinese9));PdfPCell cell86 = new PdfPCell(new Paragraph(TimeUtils.stringify(chargeResult.getCreateTime(), DateFormat.CT_S),FontChinese9));//表格高度cell81.setFixedHeight(25);cell82.setFixedHeight(25);cell83.setFixedHeight(25);cell84.setFixedHeight(25);cell85.setFixedHeight(25);cell86.setFixedHeight(25);//水平居中cell81.setHorizontalAlignment(Element.ALIGN_CENTER);cell82.setHorizontalAlignment(Element.ALIGN_CENTER);cell83.setHorizontalAlignment(Element.ALIGN_CENTER);cell84.setHorizontalAlignment(Element.ALIGN_CENTER);cell85.setHorizontalAlignment(Element.ALIGN_CENTER);cell86.setHorizontalAlignment(Element.ALIGN_CENTER);//垂直居中cell81.setVerticalAlignment(Element.ALIGN_MIDDLE);cell82.setVerticalAlignment(Element.ALIGN_MIDDLE);cell83.setVerticalAlignment(Element.ALIGN_MIDDLE);cell84.setVerticalAlignment(Element.ALIGN_MIDDLE);cell85.setVerticalAlignment(Element.ALIGN_MIDDLE);cell86.setVerticalAlignment(Element.ALIGN_MIDDLE);//边框颜色cell81.setBorderColor(lightGrey);cell82.setBorderColor(lightGrey);cell83.setBorderColor(lightGrey);cell84.setBorderColor(lightGrey);cell85.setBorderColor(lightGrey);cell86.setBorderColor(lightGrey);table8.addCell(cell81);table8.addCell(cell82);table8.addCell(cell83);table8.addCell(cell84);table8.addCell(cell85);table8.addCell(cell86);document.add(table8);}}//加入空行Paragraph blankRow5 = new Paragraph(18f, " ", FontChinese18);document.add(blankRow5);//设置属性//标题document.addTitle(date);//作者document.addAuthor("沈国亮");//主题document.addSubject("日结单pdf");document.close();OutputStream outStream = response.getOutputStream();outStream.write(baos.toByteArray());outStream.flush();outStream.close();} catch (Exception e) {e.printStackTrace();}}

 

五:最后效果:直接流的形式返回前台浏览,还可以下载

  相关解决方案