0

Task: To convert a document[visualforce generated one] into pdf.

Solution I am using now : making use of salesforce renderAs="pdf" method

Problem Faced : RenderAs pdf method is not working as expected in all cases. So we have opted for alternate method.

Alternate Solution I am trying now : Canvas & Jspdf

Steps I have done

  1. html2canvas to convert the document into image.
  2. jsPdf method to convert the image into pdf.

My code

   <script>
     $(function() {

     $("#finalPdf").click(function() {
     html2canvas($("#widget"), {
     onrendered: function(canvas) {
     var img =canvas.toDataURL("image/jpeg,1.0");  
    //           console.dir(canvas);
      var pdf = new jsPDF('p', 'pt', 'letter');


      pdf.addImage(img, 'JPEG', 15, 15);

      pdf.output('datauri');

    }
  });
})
});

</script>

Here the jsPdf method is working for small sized document alone. For documents with more content its not responding. Do anyone know about this??

  • 3
    When you say "RenderAs pdf method is not working as expected in all cases". Can you add more details on when its not working? – RedDevil Jul 31 '17 at 7:52
  • we are facing alignment issues with it. In certain cases the alignment is collapsing when we run the pdf in batch. So we thought of taking a picture[ screenshot] of the generated document and then to convert it into pdf, So that the alignment won't collapse anywhere. – Aruna Jul 31 '17 at 7:55
  • Just a thought, but i think Jspdf expects valid html5 while visualforce doesn't create that. That may be part of your problem. – Kasper Jul 31 '17 at 11:53
  • 2
    But I am not giving the html part to jsPdf, I am converting the visualforce page into jpeg image and then giving that jpeg to jsPdf. Will it still cause problem? – Aruna Jul 31 '17 at 12:05
  • Sorry, you're right! To be honest, i'd be careful with building your solution unless it is a very simple page that you are after. You are now trying to combine visualforce, with all it's uniqueness with htmltocanvas which has its own oddities and jsPdf which may or may not be-able to handle everything htmltocanvas does. It could work really well, or it could turn into an incomprehensible mess. – Kasper Aug 1 '17 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.