代码之家  ›  专栏  ›  技术社区  ›  Mohemmad K

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

  •  0
  • Mohemmad K  · 技术社区  · 7 年前

    我已经创建了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 回复  |  直到 7 年前
        1
  •  0
  •   Manjunath Siddappa    7 年前

    请检查一下 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> 标签。

    希望这对你有帮助