3

I am working on a web application and I have to convert a HTML page (have multiple tables and datatables) to PDF. I am using pdfmake to convert the content of the page to PDF.

This is the script I am using for it

<script type="text/javascript" >
var docDefinition = { content: "This is a sample PDF" };
alert("Will this work ");
pdfMake.createPdf(docDefinition).download('test.pdf');      
</script>

Now, I want to convert full HTML page to PDF but I have na idea how to do this in pdfmake.

In jsPDF there is something like this

<script>
     function demoFromHTML() {
         var pdf = new jsPDF('p','pt','a4');
         pdf.addHTML(document.body,{pagesplit:true},function() {
                  pdf.save('Test.pdf');
              }); 
           document.getElementById('buttons').style.visibility = 'hidden';

    }
</script>

i.e., document.body to convert full web page to PDF. Is there any similar code available in pdfmake as well.

Is there any other way to convert whole web page to pdf apart from jsPdf and which uses javascript?

Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
Chirag Kawariya
  • 205
  • 2
  • 4
  • 18
  • Do you want to convert only the content or complete page with images styles and text effects also? Check the first answer in this SO thread [link]http://stackoverflow.com/questions/31610129/pdfmake-html-table-to-pdfmake-table – Satyaki Chatterjee Jan 08 '16 at 05:54
  • @SatyakiChatterjee - Complete page because that HTML page contains Tables, DataTables and icons. I know there is a way through which tables can be converted to PDF but in my case, it's dataTables which are obviously dynamic. . – Chirag Kawariya Jan 08 '16 at 06:06
  • did you checked the link in my comment? – Satyaki Chatterjee Jan 08 '16 at 06:09

1 Answers1

1

AFAIK at this moment there is no html parser engine on the original pdfmake project or plugin that fits this need of rendering html content to pdf. I think you should go for jspdf for this. I personally use both libraries depending on the things I am trying to achieve. They doesn't conflict on each other. JSPDF at the moment is more complete and have more features and plugins than pdfmake. jspdf is great for making any kind of purpose pdf, you can even make graphs and draw objects. pdfmake is better if you only want a simple document with text, images and tables. I know it's a little painful on jspdf to always play with X and Y when you just want some text and images.

UPDATE: There seems to be plans to support this. Follow this thread for this feature. https://github.com/bpampuch/pdfmake/issues/205

There is also a plugin for this (I haven't tested it): https://github.com/ymkins/html2pdfmake/blob/master/html2pdfmake.js

revobtz
  • 606
  • 10
  • 12