0

I have created JavaScript code to get data from HTML back-end and convert that HTML into the PDF.

To generate PDF, I have used JSPDF.

My HTML string looks like below:

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

The PDF which is generated using JSPDF looks like below:

enter image description here

The code to generate HTML is:

    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');
}

Can you please guide me how the alignment can be managed to manage the content. Also, how can I manage the font and how to remove þÿ character?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Mohemmad K
  • 809
  • 7
  • 33
  • 74

1 Answers1

0

Please check http://jsfiddle.net/2rhsvgkk/

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

you are using some unnecessary <span> and <br> tags.

Hope this help you

Manjunath Siddappa
  • 2,126
  • 2
  • 21
  • 40
  • Thanks for the supporting. It is working fine for me. I had assumption that whatever HTML is there which provided to fromHTML function will be converted automatically. That's why I was keeping everything as it is. The code you provided is working fine. :-) – Mohemmad K Jun 04 '18 at 07:13