无法通过jsPDF从Html div制作完整的html内容到PDF

时间:2015-09-07 09:47:34

标签: javascript jquery jspdf

我需要你的帮助。 我尝试在博客中的每个帖子上制作可下载的pdf文件格式博客。我使用了以下代码和插件:

插件:

jQuery v1.11.3
jspdf.js

HTML结构

<div id='printpdf'>
..................
Blog Post Content here
.....................
</div>
<div id="editor">
</div>
<button id="btn-pdf" type="button" name="btn-pdf">generate PDF</button>

HTML Live网址: http://ecwebzone.blogspot.in/2015/09/this-is-test-post-of-pdf-creation.html

代码:

<script>
  $(document).ready(function(){
     var specialElementHandlers = {
      &#39;#editor&#39;: function (element, renderer) {
        return true;
      }   
    };

    $(&#39;#btn-pdf&#39;).click(function () {

            var doc = new jsPDF('p', 'mm', [297, 210]);
            var source = $('#printpdf').html();
            var specialElementHandlers = {
                '#editor': function (element, renderer) {
                    return true;
                }
            };
            doc.fromHTML(source, 5, 5, {
                'width': 297,'elementHandlers': specialElementHandlers
            });
            doc.output("dataurlnewwindow");

    });


  });
</script>

单击按钮后,它会转换此帖子的几行,例如,您通过我的网址检查:

Answer the following questions by selecting the most appropriate option.
1. A creative learner refers to one who is

请帮助我任何人。我想将这个选项添加到我的每个帖子中,即“以pdf格式下载帖子”的4个纸张大小。

我也尝试过:

<script>
     function downPDF() {
         var doc = new jsPDF(&#39;p&#39;, &#39;in&#39;, &#39;letter&#39;);
         var source = $(&#39;#printpdf&#39;).first();
         var specialElementHandlers = {
             &#39;#editor&#39;: function(element, renderer) {
                 return true;
             }
         };

         doc.fromHTML(
             source, // HTML string or DOM elem ref.
             0.5,    // x coord
             0.5,    // y coord
             {
                 &#39;width&#39;: 7.5, // max width of content on PDF
                 &#39;elementHandlers&#39;: specialElementHandlers
             });

         doc.save('ECNote-files.pdf');
    }
    </script>

此处仅输出空白。创建的pdf中没有文字。

1 个答案:

答案 0 :(得分:0)

在HTML源代码中,我看到带有span的第一行没有&lt; br&gt;在结束标记之后但是&lt; / div&gt;。所以#printpdf-div在第一个跨度后关闭。重写&lt; / div&gt;在这个位置用&lt; br /&gt;它应该工作。

代码:

... 1. A creative learner refers to one who is</span> --></div><--
<span style="background-color: white; color: #141823; font-family: helvetica, arial, sans-serif; font-size: 14px; line-height: 19.32px;">(1) very talented in drawing and painting</span><br /> ....