Search code examples
javascriptjspdf

Create Pdf with Div border using jspdf


I am trying to use the JsPdf library to create a pdf based on html elements. I am wanting to know if it is possible to do with a div border or if i have to use the doc.line property and essentially draw each line around my div. I.E.

var doc = new jsPDF()

doc.line(20, 20, 60, 20)

I would much rather use <div style="border: solid; width: 300px ">

Has anyone had any luck with this?

Here is my Fiddle


Solution

  • How about using jsPdf in conjunction with Html2Canvas? Render the html to canvas, then add the canvas to the pdf as an image like so:

    var img = canvas.toDataURL("image/png");
    doc.addImage(img, 'JPEG', 300, 200);
    doc.save('test.pdf');
    

    See fiddle for full example: http://jsfiddle.net/nLLuvnwL/