0 votes
1 view
in Java by (13.1k points)

Can anyone help me print pdf from HTML. I am using jspdf but it contains only text.

<!DOCTYPE html>

<html>

    <body>

        <p>don't print this to pdf</p>

        <div id="pdf">

            <p><font size="3" color="red">print this to pdf</font></p>

        </div>

    </body>

</html>

Can anyone help me with this?

1 Answer

0 votes
by (26.7k points)

Basically, jsPDF comes with different plugins, in able to print HTML, you have to add some plugins into it. To do that:

- Download the latest version.

- Then, include these scripts:

jspdf.js

jspdf.plugin.from_html.js

jspdf.plugin.split_text_to_size.js

jspdf.plugin.standard_fonts_metrics.js

Then, your HTML code will look like this:

<!DOCTYPE html>

<html>

  <body>

    <p id="ignorePDF">don't print this to pdf</p>

    <div>

      <p><font size="3" color="red">print this to pdf</font></p>

    </div>

  </body>

</html>

After that, run the below file, that will help to convert to PDF:

var doc = new jsPDF();          

var elementHandler = {

  '#ignorePDF': function (element, renderer) {

    return true;

  }

};

var source = window.document.getElementsByTagName("body")[0];

doc.fromHTML(

    source,

    15,

    15,

    {

      'width': 180,'elementHandlers': elementHandler

    });

doc.output("dataurlnewwindow");

I hope this will help.

Want to become a Java expert? join Java Training now!!

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !

Categories

...