1

当用户单击按钮时,我正在尝试将网页导出为 pdf 文件,但以下代码对我不起作用。任何人都可以帮助我哪里出错了。我正在使用 jsPDF 将网页导出为 pdf。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>html2canvas example</title>
   <script type="text/javascript" src="js/jquery/jquery-1.7.1.min.js"></script>
    <script type="text/javascript" src="js/jquery/jquery-ui-1.8.17.custom.min.js"></script>
    <script type="text/javascript" src="js/jspdf.js"></script>
    <script type="text/javascript" src="libs/Deflate/adler32cs.js"></script>
    <script type="text/javascript" src="libs/FileSaver.js/FileSaver.js"></script>
    <script type="text/javascript" src="libs/Blob.js/BlobBuilder.js"></script>
    <script type="text/javascript" src="jspdf.plugin.addimage.js"></script>
    <script type="text/javascript" src="jspdf.plugin.standard_fonts_metrics.js"></script>
    <script type="text/javascript" src="jspdf.plugin.split_text_to_size.js"></script>
    <script type="text/javascript" src="jspdf.plugin.from_html.js"></script>
    <script type="text/javascript" src="js/basic.js"></script>
 <script type="text/javascript">
 $(function () 
{
        var doc = new jsPDF();
        var specialElementHandlers = {
            '#editor': function (element, renderer) {
                return true;
            }
        };
        $('#cmd').click(function () {
            doc.fromHTML($('#content').html(), 15, 15, {
                'width': 170,
                    'elementHandlers': specialElementHandlers
            });
            doc.save('sample-file.pdf');
        });
    });
 </script>
</head>
<body id="target" >
     <div id="content">
         <h3>Hello, this is a H3 tag</h3>
        <p>a pararaph</p>
     </div>
  <div id="editor"></div>
  <button id="cmd">generate PDF</button>
</body>
</html>
4

1 回答 1

4

脚本中有错误。请尝试此代码

<script>
$(document).ready(function (){
                            $('#cmd').click(function () {
                            var specialElementHandlers = 
                            function (element,renderer) {
                            return true;
                            }
                            var doc = new jsPDF();
                            doc.fromHTML($('#content').html(), 15, 15, {
                            'width': 170,
                            'elementHandlers': specialElementHandlers
                            });
                            doc.output('datauri'); 
                        });
                        });
</script>
于 2013-11-19T07:07:47.243 回答