Generate PDF from HTML Using PDFmake in Angularjs

Generate PDF from HTML using pdfMake in Angularjs

Okay, I figured this out.

  1. You will need html2canvas and pdfmake. You do NOT need to do any injection in your app.js to either, just include in your script tags

  2. On the div that you want to create the PDF of, add an ID name like below:

     <div id="exportthis">
  3. In your Angular controller use the id of the div in your call to html2canvas:

  4. change the canvas to an image using toDataURL()

  5. Then in your docDefinition for pdfmake assign the image to the content.

  6. The completed code in your controller will look like this:

           html2canvas(document.getElementById('exportthis'), {
    onrendered: function (canvas) {
    var data = canvas.toDataURL();
    var docDefinition = {
    content: [{
    image: data,
    width: 500,
    }]
    };
    pdfMake.createPdf(docDefinition).download("Score_Details.pdf");
    }
    });

I hope this helps someone else. Happy coding!

Using pdfmake - generate pdf tool in AngularJS

found it!
I missed to import of the pdfMake plugin in my home html file.

how to export html data to pdf in angularjs

You can use pdfmake, to export the pdf

DEMO

var app = angular.module("app", []);
app.controller("listController", ["$scope", function($scope) { $scope.data= [{"agence":"CTM","secteur":"Safi","statutImp":"operationnel"}]; $scope.export = function(){ html2canvas(document.getElementById('exportthis'), { onrendered: function (canvas) { var data = canvas.toDataURL(); var docDefinition = { content: [{ image: data, width: 500, }] }; pdfMake.createPdf(docDefinition).download("test.pdf"); } }); } } ]);
<!doctype html><html ng-app="app">
<head> <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.4/angular.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.22/pdfmake.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.min.js"></script> <script src="script.js"></script></head>
<body> <div ng-controller="listController"> <div id="exportthis"> {{data}} </div> <button ng-click="export()">export</button> </div></body>
</html>

How to create PDF document from HTML table?

I managed to create PDF reports that I needed.

Plugin I used is PDFmake and is definitely best I found, works so flawless.

Only problem I had is that I wrongly included it and that's why it didn't worked first time.



Related Topics



Leave a reply



Submit