使用.fromHTML方法生成PDF时,文本对齐不正确

时间:2018-06-04 05:27:04

标签: javascript html jspdf text-alignment

我创建了JavaScript代码以从HTML后端获取数据并将该HTML转换为PDF。

为了生成PDF,我使用了JSPDF。

我的HTML字符串如下所示:

"<p><span style="font-family:calibri;font-size:17.3333px;">Dear [@Client Name],<br></span></p><p><span style="font-family:calibri;font-size:17.3333px;">This is a sample letter that I need filled out for [@Client Name 2], who lives in [@City],[@Province]. On this date, the [@Number Date], of ​[@Month],[@Year]. It occurred to me that [@Client Name] was talking to [@Client Name 3].<br></span></p><p><span style="font-family:calibri;font-size:17.3333px;">The end.</span></p><p><span style="font-family:calibri;font-size:17.3333px;">Appriciate your time.<br></span></p><p><span style="font-family:calibri;font-size:17.3333px;">[@Date]</span><br></p>"

使用JSPDF生成的PDF如下所示:

enter image description here

生成HTML的代码是:

    function GeneratePDF(content) { 


        var doc = new jsPDF();  
        var specialElementHandlers = {
            '#editor': function (element, renderer) {
                return true;
            }
        };

        doc.fromHTML(content, 15, 15, {
            'width': 250,
                'elementHandlers': specialElementHandlers
        });
        doc.save('sample-file.pdf');
}

您能否指导我如何管理对齐以管理内容。另外,如何管理字体以及如何删除þÿ字符?

1 个答案:

答案 0 :(得分:0)

请检查 http://jsfiddle.net/2rhsvgkk/

灵感来自 How to properly use jsPDF library

HTML

<div id="customers">
   <p>Dear [@Client Name],</p>
   <p>This is a sample letter that I need filled out for [@Client Name 2], who lives in [@City], [@Province].  On this date, the [@Number Date], of [@Month],[@Year]. It occurred to me that [@Client Name] was talking to [@Client Name 3].</p>
   <p>The end.</p>
   <p>Appriciate your time.</p>
   <p>[@Date]</p>
</div>
<button onclick="javascript:demoFromHTML();">PDF</button>

您正在使用一些不必要的<span><br>标记。

希望这能帮到你