【问题标题】:How to render html and css to pdf with js如何使用js将html和css渲染成pdf
【发布时间】:2022-02-03 00:14:53
【问题描述】:

您好,我需要在 pdf 中创建自定义报价,我在 js 中检索数据并在 html 中发送,然后我检索 html 以在 pdf 中发送它,除了没有库支持 css,我找到了 HTML2CANVAS 的解决方案,除了它返回一个图像,我不想要那个

        document.getElementById('generationPdf').onclick = e =>{
            e.preventDefault()
            if (index + 1 === 1) {
                let folder_id = document.location.pathname.substring(8)
                let objectForDevis =  arrayObject[0]

                axios({
                    method: 'post',
                    url: folder_id+'/generate-pdf',
                    data: {
                    objectForDevis
                    }
                }).then(function (response) {
                    if (response) {
                       
                        
                    }
                });
            }
        }

【问题讨论】:

    标签: javascript ajax pdf jspdf html2canvas


    【解决方案1】:

    使用jsPDF 以下是我在this website 上找到的示例用法

    html2canvas(document.body, {
        onrendered:function(canvas) {
    
            //Returns the image data URL, parameter: image format and clarity (0-1)
            var pageData = canvas.toDataURL('image/jpeg', 1.0);
    
             //Default vertical direction, size ponits, format a4[595.28,841.89]
            var pdf = new jsPDF('', 'pt', 'a4');
    
            //Two parameters after addImage control the size of the added image, where the page height is compressed according to the width-height ratio column of a4 paper.
            pdf.addImage(pageData, 'JPEG', 0, 0, 595.28, 592.28/canvas.width * canvas.height );
    
            pdf.save('stone.pdf');
    
        }
    })
    

    【讨论】:

      猜你喜欢
      • 2014-10-23
      • 2019-02-13
      • 1970-01-01
      • 1970-01-01
      • 2020-04-12
      • 2010-09-30
      • 2021-05-26
      • 2021-05-13
      • 2015-05-19
      相关资源
      最近更新 更多