关于点击生成pdf文件并下载到本地的功能

后端

引入模块

async =  require 'async'

fs = require 'fs'

写入文件

writeFile = (address, str, cbf) ->
  write = fs.writeFileSync(address,str)
  return cbf null, write

生成html文件

htmlFile = (address, option, cbf) ->
  html = fs.readFileSync(address,option)
  return cbf null, html

生成pdf文件

pdfFile = (html, address, cbf) ->
  # console.log "html",html
  options = { format: 'A3' }
  pdf.create html, options
    .toFile address, (err, res) ->
      if (err) 
        return console.log err
      return cbf null,"success"

删除html文件

deteleHtml = (address, cbf) ->
  fs.unlink address, (err,res) ->
    if (err) 
      return console.log err
    return cbf null, 'detele success'

调用

funcs = [
  # 写入文件
  (cbf) ->
    return writeFile htmlAdr, params.str, cbf
  # 生产html文件
  (result1,cbf) ->
    return htmlFile htmlAdr, 'utf-8', cbf
  # 生产pdf文件
  (result2,cbf) ->
    return pdfFile result2, pdfAdr, cbf
  # 删除html文件
  (result3,cbf) ->
    return deteleHtml htmlAdr, cbf
]
async.waterfall funcs, (error, result) ->
  if error
    return cbf error, null
  # 返回用户信息
  res.download pdfAdr, fileName

前端

a(href="javascript:;" v-on:click.stop.prevent="onDownEsignatureLh(esignature_lh);") 下载
a = document.createElement "a"
url = window.URL.createObjectURL res.body
a.href = url
a.download = esignature_lh.ciphertext + ".pdf"
a.click()
window.URL.revokeObjectURL res.body
    原文作者:CcChan
    原文地址: https://segmentfault.com/a/1190000017380476
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞