导出网页将使用jsPDf PDF(Exporting web page into PDF using

2019-09-28 03:20发布

我想在网页导出为PDF文件时,用户点击该按钮,但下面的代码是不是为我工作。 任何一个可以请帮我在哪里出了错。 我使用jsPDF到Web页面导出为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>

Answer 1:

有脚本错误。 请试试这个代码

<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>


文章来源: Exporting web page into PDF using jsPDf