简体   繁体   English

无法使用 jsPDF 将 HTML 转换为 PDF

[英]Unable to convert HTML to PDF using jsPDF

I'm able to download a PDF using jsPDF but the downloaded PDF is plain text excluding the styles applied in the website.我可以使用 jsPDF 下载 PDF,但下载的 PDF 是纯文本,不包括网站中应用的样式。

I am unable to get the styles or formatting in the website to the downloadable PDF.我无法将网站中的样式或格式设置为可下载的 PDF。

Here's the jsPDF code I use:这是我使用的 jsPDF 代码:

       <button type="button" onclick="convert()" class="pda-button"><i class="save-icon sprite"></i> Save</button>

        <div  id="content">
          //content goes 
        </div>
        <div id="elementH"></div>

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="http://demos.codexworld.com/includes/js/bootstrap.js"></script>
    <script src="http://demos.codexworld.com/3rd-party/jsPDF/dist/jspdf.min.js"></script>  

        <script>
    function convert() {
        var doc = new jsPDF();
        var elementHTML = $('#content').html();
        var specialElementHandlers = {
            '#elementH': function (element, renderer) {
                return true;
            }
        };
        doc.fromHTML(elementHTML, 15, 15, {
            'width': 170,
            'elementHandlers': specialElementHandlers
        });

        // Save the PDF
        doc.save('sample-document.pdf');
    }
    </script>

Here's the link to website I integrated the above code and want to convert to PDF: Website's link这是我集成了上述代码并想转换为 PDF 的网站链接网站链接

As I click on save button on the top left of the site to download PDF, the PDF is generated and downloaded but as an plain text PDF with no styles.当我点击网站左上角的保存按钮下载 PDF 时,会生成并下载 PDF,但它是一个没有样式的纯文本 PDF。

And Please do not Down-Vote the post without explaining the reason for doing so..并且请不要在没有解释这样做的原因的情况下对帖子投反对票..

You need to give a callback function to the method doc.fromHTML你需要给方法doc.fromHTML一个回调函数

doc.fromHTML(elementHTML, 15, 15, {
        'width': 170,
        'elementHandlers': specialElementHandlers
    }, function cb() {} );

cb() will tell you that the pdf is ready. cb()会告诉你 pdf 已经准备好了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM