TinyMce使用具有格式的jsPDF生成pdf

问题描述 投票:0回答:1

我想使用jspdf将所有格式的tinymce编辑器的html内容转换为pdf。当我将文本格式设置为居中时,pdf不会像在编辑器中一样显示。我猜问题出在doc.fromHTML()中。我曾尝试更改宽度,但没有得到理想的结果。如果可能,我希望jspdf从tinymce设置动态页面宽度。怎么做?

width |result
--------------
no    |text align to to left
50    |indented to right which is increasing from line to line (pic attached) 
100   |The whole text move to the left about half. Half of page at right is empty. 

宽度50tmcejspdf

代码:

<!DOCTYPE html>
<html lang="en">
    <head>
        <script src="../testing/plugin/jquery/jquery-3.4.1.min.js"></script>
        <script src="../testing/plugin/tinymce_v5/tinymce/js/tinymce/jquery.tinymce.min.js"></script>
        <script src="../testing/plugin/tinymce_v5/tinymce/js/tinymce/tinymce.min.js"></script>
        <script src="../testing/plugin/jsPDF-master/dist/jspdf.min.js"></script>
        <script src="../testing/plugin/jsPDF-master/dist/jspdf.debug.js"></script>
        <script>
            tinymce.init
            (
                {
                    selector: '#template_editor',

                }
            );

            function generate_pdf()
            {
                html_content=tinymce.activeEditor.getContent();
                tinymce.triggerSave(); // Add this line
                var doc = new jsPDF();
                //console.log(doc);
                var obj_g = $('#template_editor').val();
               var specialElementHandlers = 
               {
                  'DIV to be rendered out': function(element, renderer)
                  {
                    return true;
                   }
                };
                //var html=$("#template_editor").html();
                  doc.fromHTML(
                      html_content,
                      0.5,
                      0.5,
                      {
                        'width': 100,                     
                      }

                      );

                doc.save('projeto.pdf');

            }
        </script>
    </head>
    <body>

        <textarea id="template_editor" name="mytextarea"></textarea>
        <button type="button" id="generate_pdf" onclick="generate_pdf()">pdf!</button>
    </body>
</html>

提前感谢。

javascript tinymce jspdf tinymce-5
1个回答
0
投票

我建议您查看API2PDF的TinyMCE插件。它将为您带来更好的结果:https://www.api2pdf.com/tinymce-save-to-pdf-plugin/

© www.soinside.com 2019 - 2024. All rights reserved.