Javascript 无法通过jsPDF从Html div生成PDF

Javascript 无法通过jsPDF从Html div生成PDF,javascript,jquery,pdf,canvas,jspdf,Javascript,Jquery,Pdf,Canvas,Jspdf,我有这个js代码: var doc = new jsPDF(); $('#pdf_new').click(function(){ var html=$(".wrap_all").html(); doc.fromHTML(html,200,200, { 'width': 500, }); doc.save("Test.pdf"); }); 在html中,我有如下代码: <div class = 'wrap_all'> <

我有这个js代码:

var doc = new jsPDF();

$('#pdf_new').click(function(){
    var html=$(".wrap_all").html();
    doc.fromHTML(html,200,200, {
        'width': 500,
    });
    doc.save("Test.pdf");
});
在html中,我有如下代码:

<div class = 'wrap_all'>
    <div id = 'wrap_odd'> <div id = ''>....</div> </div>

    <div id = 'wrap_even'> <div id = ''>....</div> </div>
</div>

.... 
.... 
什么都不管用。。。控制台返回给我:

无法读取未定义的属性
#wrap_odd


(对不起,我的英文版)

当使用“fromHTML”从HTML中获取文本,并将其用于使用JSPDF创建的PDF时,您应该有一个elementHandler函数来呈现不需要的

尝试按照at中所示的相同模式进行操作

您的最终代码应该如下所示:

var doc = new jsPDF(); var specialElementHandlers = { 'DIV to be rendered out': function(element, renderer){ return true; } }; $('#pdf_new').click(function(){ var html=$(".wrap_all").html(); doc.fromHTML(html,200,200, { 'width': 500, 'elementHandlers': specialElementHandlers }); doc.save("Test.pdf"); }); var doc=new jsPDF(); 变量specialElementHandlers={ “要渲染的DIV”:函数(元素、渲染器){ 返回true; } }; $('#pdf_new')。单击(函数(){ var html=$(“.wrap_all”).html(); fromHTML文件(html,200200{ “宽度”:500, “elementHandlers”:specialElementHandlers }); doc.save(“Test.pdf”); });
选中此项..此项显示全部内容

     <head>
        <script src="https://code.jquery.com/jquery-git.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/0.9.0rc1/jspdf.min.js"></script>
<script type="text/javascript">
        $(window).on('load', function () {
            var doc = new jsPDF();
            var specialElementHandlers = {
                '#editor': function (element, renderer) {
                    return true;
                }
            };
            $('#pdfview').click(function () {
                doc.fromHTML($('#pdfdiv').html(), 15, 15, {
                    'width': 100,
                    'elementHandlers': specialElementHandlers
                });
                doc.save('file.pdf');
            });
        });
    </script>
    </head>
     <div id="pdfdiv">
                <h3 style="color:green;">WWW.SANWEBCORNER.COM</h3>
                <img src="404.png">
                <p>A newspaper acts an important medium to control corruption and scams. The chief topics of general interest in newspaper includes politics, social issues, sports, economy, movies, and share market.

    Newspaper is a mode of mass communication. It is very helpful in creating social awareness. Newspaper raises voices against social issues such as child labor, dowry system, etc. They urge the common people to act and behave in a rational manner.

    We get the information of the forthcoming movies and television shows through a newspaper. It also contains a list of multiplexes with time-schedule for the movies.

    A wide coverage of information is obtained at low cost though newspaper. It also influences the habit of thinking in men. It has also seen that illiterate adults are taking up education to read newspaper.

    There are such dailies that trade on such dirty tricks for survival. Being politically left or right, they misrepresent strikes and lockouts. Events like bank robbery and train accident or similar untoward events are distorted or exaggerated. They deliberately make their news sensational because it appeals to the less educated and less cultured more directly.

    The dignity and reputation of a newspaper rests on the degree of their fidelity to truth and fearless reporting. It is our cheapest and most powerful weapon in the last analysis.</p>
            </div>
            <div id="editor"></div>
            <button id="pdfview">Download PDF</button>

$(窗口).on('load',函数(){
var doc=new jsPDF();
变量specialElementHandlers={
“#编辑器”:函数(元素、渲染器){
返回true;
}
};
$('#pdfview')。单击(函数(){
doc.fromHTML($('#pdfdiv').html(),15,15{
“宽度”:100,
“elementHandlers”:specialElementHandlers
});
doc.save('file.pdf');
});
});
WWW.SANWEBCORNER.COM
报纸是控制腐败和诈骗的重要媒介。报纸的主要主题包括政治、社会问题、体育、经济、电影和股票市场。
报纸是大众传播的一种方式。它非常有助于创造社会意识。报纸对童工、嫁妆制度等社会问题发出了反对声音。它们敦促普通人理性行事。
我们通过报纸获得即将上映的电影和电视节目的信息。它还包含一份电影时间表的多路复用列表。
通过报纸可以以低成本获得广泛的信息报道。这也影响了男性的思维习惯。还可以看到,文盲成年人正在接受阅读报纸的教育。
有这样的日报为了生存而利用这些肮脏的伎俩。无论政治上是左派还是右派,他们都歪曲罢工和封锁。银行抢劫、火车事故或类似的不幸事件都被歪曲或夸大。他们故意让自己的新闻耸人听闻,因为它吸引了教育程度较低、文化程度较低的人你是直接的。
报纸的尊严和声誉取决于他们对真相的忠实程度和无畏的报道。归根结底,它是我们最廉价、最有力的武器

下载PDF
请注意:小心可打印元素容器中的元素放置


jsPDF似乎对恰到好处的元素非常敏感。这包括图像和链接。

如果我没有任何不需要的元素怎么办?