尝试将HTML转换为PDF时出现错误

时间:2019-05-15 14:11:59

标签: html ckeditor jspdf

我在之前的页面中使用HTML将HTML转换为PDF java-script可以正常工作,但是当我尝试在使用CKeditor的页面上尝试将其转换为PDF时使用它,但是以某种方式我得到了错误specialelementhandler在控制台中未定义

当我尝试将HTML转换为PDF时出现错误

当我尝试将HTML转换为PDF时出现错误 收到此错误

Uncaught ReferenceError: specialElementHandlers is not defined
    at HTMLButtonElement.<anonymous> (MailMergeCKEditor:71)
    at HTMLButtonElement.dispatch (jquery-1.12.3.min.js:3)
    at HTMLButtonElement.r.handle

我想将CKeditor中的所有文本转换为pdf格式

<head>

    <title>Using placeholders</title>
    <script src="https://code.jquery.com/jquery-1.12.3.min.js"></script>
    <script src="https://cdn.ckeditor.com/4.11.4/standard-all/ckeditor.js"></script>
    <button id="cmd">Generate PDF</button>


    <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/0.9.0rc1/jspdf.min.js"></script>
</head>

<body id="bodyclass">
    <textarea cols="10" id="editor1" name="editor1" rows="10" data-sample-short>&lt;p&gt;This is some &lt;strong&gt;sample text&lt;/strong&gt;. You are using &lt;a href=&quot;https://ckeditor.com/&quot;&gt;CKEditor&lt;/a&gt;.&lt;/p&gt;
<p>
  User Can modify this 
</p>

<m class="a">
  User Can't modify this
</m>


<p>
    User Can modify this
</p>

</textarea>


    <div id="editor"></div>
    <script>
        CKEDITOR.replace('editor1', {
            extraPlugins: 'placeholder',
            height: 220,
            allowedContent: 'm'






        });
        CKEDITOR.config.allowedContent = true;




        CKEDITOR.on('dialogDefinition', function (event) {
            if ('placeholder' == event.data.name) {
                var input = event.data.definition.getContents('info').get('name');
                input.type = 'select';
                input.items = [['Company'], ['Email'], ['First Name'], ['Last Name']];
                input.setup = function () {
                    this.setValue('Company');
                };
            }
        });


 var doc = new jsPDF();

$('#cmd').click(function () {   
    doc.fromHTML($('#bodyclass').html(), 15, 15, {
        'width': 170,
            'elementHandlers': specialElementHandlers
    });
    doc.save('sample-file.pdf');
});



    </script>
</body>

</html>

1 个答案:

答案 0 :(得分:1)

基本上,您没有为specialElementHandlers变量分配任何内容。

在您的var doc = new jsPDF();语句之后,您需要添加以下代码段,

var specialElementHandlers = {
    '#editor1': function (element, renderer) {
        return true;
    }
};

添加此内容后,请尝试刷新页面,希望您的问题得到解决。

希望这会有所帮助!