Java生成PDF------前端自定义下载地址

/**

  • 生成PDF------前端自定义下载地址
    * @throws Exception
    */
@GetMapping("pdf2")
public  void pdf2(HttpServletRequest request, HttpServletResponse response) throws Exception {
   // 第一步,实例化一个document对象
   Document document = new Document(PageSize.A4);
   // 第二步,设置要到出的路径
   response.setContentType("application/msexcel");
   response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("电子回单.pdf", "UTF-8"));
   response.setCharacterEncoding("utf-8");

   // 如果是浏览器通过request请求需要在浏览器中输出则使用下面方式;
   ServletOutputStream out = response.getOutputStream();

   // 第四步,将pdf文件输出到磁盘
   PdfWriter writer = PdfWriter.getInstance(document, out);
   // 第五步,打开生成的pdf文件
   document.open();
   // 第六步,设置内容
   BisBankReceiptPdf pdf111 = bisBankReceiptPdfDao.get("AD5F79CE2D30A50B4DBC8553C4003665");
   BytesToImage(pdf111.getEbillimg(),"D:\\xhw\\aa1.jpg");
   Image image1 = Image.getInstance("D:\\xhw\\aaa.jpg");
   document.add(image1);
   // 创建table,注意这里的2是两列的意思,下面通过table.addCell添加的时候必须添加整行内容的所有列
   document.newPage();
   Image image2 = Image.getInstance("D:\\xhw\\aaa.jpg");
   document.add(image2);
   // 第七步,关闭document
   document.close();

}
/**
 * 批量打印
 * @param e
 * @param dt
 * @param node
 * @param config
 */
function batchPrint1(e, dt, node, config) {
    parent.layer.open({
        type: 2,
        zIndex: -100,
        shade: 0,
        content: basePath + "/poi/pdf2",
        success:function (layero, index) {
            layer && layer.close(loadIndex);
        }
    });
}
/**
 *  将图片字节数组转化为图片,存放到指定路径
 *
 * @param bytes 图片字节数组
 * @param url 存放的路径
 */
public static void BytesToImage(byte[] bytes, String url){
   FileImageOutputStream imageOutput = null;//打开输入流
   try {
      imageOutput = new FileImageOutputStream(new File(url));
      imageOutput.write(bytes, 0, bytes.length);//将byte写入硬盘
   } catch (IOException e) {
      e.printStackTrace();
   }finally {
      try {
         if (imageOutput != null)
            imageOutput.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值