0

I am trying to use jsPdf (in conjunction with html2canvas and canvg) to get the content of a HTML dom container into a PDF file. I have a "View" and "Download" link in my example below where "View" is like a preview and ideally "Download" should create a document that looks like the preview

http://plnkr.co/edit/NsYkwMZeGbpNGlp8y4P0?p=preview

The contents of report1.html are essentially what goes into making the PDF

<div ng-controller="TemplateCtrl" class="popovertemplate">
<b>Hello world</b>
<div> 
<hr>  
Generating a nice document
<hr> 
<div>
  <table style="width:100%">
    <tr>
      <th>Firstname</th>
      <th>Lastname</th> 
      <th>Points</th>
    </tr>
    <tr>
      <td>Eve</td>
      <td>Jackson</td> 
      <td>94</td>
    </tr>
  </table>
</div>

<div ng-controller="ChordCtrl">
    <chord-layout chord-matrix="{{matrix}}">
    </chord-layout> 
</div>

<div>
  <svg height="100" width="100">
    <circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
  </svg> 
</div>

 <ul>
  <li ng-repeat="i in getNumber(number) track by $index">
    <div>{{$index+1}}</div>
  </li>
</ul>

<hr>


</div>
</div>

I am having trouble with the layout of the PDF since the whole thing is being coerced into an image because jsPdf could not handle tables, styles and svg charts which are essential for our reports. Any suggestions to improve the following

  • Styling within the pdf
  • Better ways to give the DOM element as an input to the pdf generator so that this whole pdf generation can be made better

For jsPdf experts who are not too aware of angularjs or other things in this example, the following code to generate the PDF is where I am not doing too well

app.service('PdfGenService', function(){
this.generatePdf = function(jQueryElement) {
    jQueryElement.show();
    jQueryElement.css('background-color','white');
    canvg();
    canvg();
    var pdf = new jsPDF();
    html2canvas(jQueryElement, {
      allowTaint: true,
      taintTest: false,
      onrendered: function(canvas) { 

        var imgData = canvas.toDataURL("image/jpeg");
        pdf.addImage(imgData, 'JPEG', 10,10,180,300);
        pdf.save('Test.pdf');
      }
    });


};
}); 

Any help is appreciated.

user2789284
  • 751
  • 1
  • 11
  • 28
  • http://www.cloudformatter.com/CSS2Pdf uses the DOM, supports all of what you request including SVG charts as vector based in the output. See the many charting demos on that page. – Kevin Brown Sep 25 '14 at 00:27
  • This looks really promising but it looks like it ships off data to their server to prepare the download for pdf. Do you know if this is something that can be done all client side? If you know of any plunkr's that have employed this, that would be great..Thanks for this. – user2789284 Sep 25 '14 at 01:05
  • It uses a commercial formatter (RenderX) in the Cloud. They provide free use of that formatting server farm. There are commercial offerings for those that do not want the free service. – Kevin Brown Sep 25 '14 at 01:28

1 Answers1

1

I have already encountered these problems

I used this for drawing tables :

https://github.com/Prashanth-Nelli/jsPdfTablePlugin

Another option would be to use screenshots of the HTML with HTML2Canvas or Casper.js.

After such jspdf is open source you can easily change it to improve the design of your pdf. After I not think you can make this really nice, I've never found the ideal solution.

Hope this can help you ;)

EDIT :

How to properly use jsPDF library

jsPDF can't get any styling to work

Community
  • 1
  • 1
DeadCalimero
  • 260
  • 1
  • 9
  • Yeah I found all of these links but none that can really do a decent job across all kinds of DOM nodes. But thanks anyway. – user2789284 Sep 25 '14 at 01:06