将HTML模板转成pdf,excel,word

由于在做这个项目的时候,用的是react,所以就将这篇文章放在这里吧
  • 将HTML模板转成pdf

  • 2个关键点,
    1,将数据插入html模板中mustache
    2,将插入数据后的模板转成pdfjsPDF

  • 根据pdf,编写html模板,也可以使用工具转,不过转出来的效果不是很理想,将要替换的数据使用mustache的写法

Image1.png
  • 调后台接口,成功的得到数据
            const url = '/api/invoices/show_edit_invoice.json'
            const params = {
                user_session_key: localStorage.getItem('user_session_key'),
                id: this.invoice_id
            }
            this.http.REQUEST(url, params, method).then(res => {
                if (res.data.status.code === '20000') {
                    const data = res.data.data
                    const obj = {}
                    // 将得到的数据传过去,渲染HTML模板
                    this.exportFile(obj)
                    this.setState({ export_visible: false })
                }
            })
  • 解析数据,使用mustache渲染html模板,得到的output会是一个html格式的字符串,在require html的时候,会碰到无法找到module的报错,因为缺少html-loader,nam install html-loader —save-dev,解决,同时
       {
             test: /\.html$/,
             loader: 'html'
       }
        let output
        // 根据输出不同的类型文件,使用不用HTML模板
        if (this.state.exportInvoice.value === 'pdf') {
            output = mustache.render(template_pdf, obj)
        } else if (this.state.exportInvoice.value === 'excel') {
            output = mustache.render(template_excel, obj)
        } else if (this.state.exportInvoice.value === 'word') {
            output = mustache.render(template_word, obj)
        }
  • 引入JSPDf
<script src="https://cdn.bootcss.com/jspdf/1.3.2/jspdf.min.js"></script>
  • 这个时候得到了输出后的html字符串,现在进行处理
        // 使用iframe将其渲染到页面中,不然会出现奇葩的报错信息
        const parser = new DOMParser()
        const htmldoc = parser.parseFromString(output, 'text/html')
        const body_str = htmldoc.documentElement.getElementsByTagName('body')[0].innerHTML
        const head_str = htmldoc.documentElement.getElementsByTagName('head')[0].innerHTML

        const myiframe = document.createElement('iframe')
        myiframe.style.width = '1400px'
        myiframe.style.height = '900px'
        document.body.appendChild(myiframe)

        myiframe.contentWindow.document.body.style.backgroundColor = '#ffffff'
        if (navigator.userAgent.indexOf('Firefox') >= 0) {
            myiframe.contentWindow.document.body.innerHTML = body_str
            myiframe.contentWindow.document.head.innerHTML = head_str
        } else {
            myiframe.contentWindow.document.body.innerHTML = body_str
            myiframe.contentWindow.document.head.innerHTML = head_str
        }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 单例模式 适用场景:可能会在场景中使用到对象,但只有一个实例,加载时并不主动创建,需要时才创建 最常见的单例模式,...
    Obeing阅读 2,109评论 1 10
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,992评论 19 139
  • 工厂模式类似于现实生活中的工厂可以产生大量相似的商品,去做同样的事情,实现同样的效果;这时候需要使用工厂模式。简单...
    舟渔行舟阅读 7,854评论 2 17
  • 一个人,一段漆黑的路,一直走。 没有颜色,没有光亮,没有疏影,连呼吸都是黑色的。 在这条昏暗的路上,我与孤独为伍。...
    有脾气的小饭团阅读 220评论 0 0
  • 天越来越冷,夜越来越长,又到了单身狗们“空虚寂寞冷”的季节。 上个月,小瑶去相亲,对方无意间讲起了自己的不开心情到...
    冫小于姑娘阅读 177评论 0 1