how to export html data to pdf in angularjs

angular-save-html-to-pdf example
pdfmake html to pdf
angularjs ng-table export to excel
pdfmake html to pdf angular 2
angular mat-table export to pdf
html form to pdf javascript
angular data table export to pdf
angular pdf

this is my html code where i rendered all json data from .js file but getting

TypeError: Cannot convert undefined or null to object at Function.keys () at DocMeasure.measureNode (pdfmake.js:15647) at DocMeasure.measureDocument (pdfmake.js:15635) at LayoutBuilder.tryLayoutDocument (pdfmake.js:15088) at LayoutBuilder.layoutDocument (pdfmake.js:15076) at PdfPrinter.createPdfKitDocument (pdfmake.js:2130) at Document._createDoc (pdfmake.js:82) at Document.getDataUrl (pdfmake.js:177) at Document.open (pdfmake.js:109) at l.$scope.openPdf (app.js:29)

<head>
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <script type="text/javascript" src="pdfmake.js"></script>
  <script type="text/javascript" src="vfs_fonts.js"></script>
  <script type="text/javascript" src="app.js"></script>
  <script type="text/javascript" src="jquery-3.1.1.min.js"></script>
  <script type="text/javascript" src="raj.js"></script>
    <script type="text/javascript" src="jspdf.js"></script>



</head>

<body ng-app="pdfDemo">
  <div ng-controller="pdfCtrl">
          <div id="pdfContent">
                  <table id="example-table">
                            <thead>
                            <th>firstName</th>
                            <th>lastName</th>
                            <th>Gender</th>
                            <th>Mobile</th>

                            </thead>
                            <tbody>
                            <tr ng-repeat="emp in employees">
                            <td>{{emp.firstName}}</td>
                            <td>{{emp.lastName}}</td>
                            <td>{{emp.gender}}</td>
                            <td>{{emp.mobile}}</td>
                            </tr>

                            </tbody>

                  </table>
          </div>
    <button ng-click="openPdf()">Open Pdf</button>
    <button ng-click="downloadPdf()">Download Pdf</button>
  </div>

</body>

</html>

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 export html data to pdf in angularjs and javascript, how to export html data to pdf in angularjs and javascript. HTML : <button onclick​="pdfdownload()" class="btn btn-success"><  this is my html code where i rendered all json data from .js file but getting TypeError: Cannot convert undefined or null to object at Function.keys () at DocMeasure.measureNode (pd

Here is the code to export HTML table to EXcel, CSV, Pdf, Doc

https://plnkr.co/edit/HmKBjYmJNjp8mPzIlg52?p=preview

  <body ng-controller="MainCtrl">
<p>Export HTML Table to Excel, Pdf, CSV and Doc</p>

<table class="export-table" style="width: 100%; margin-top: 20px">
      <thead>
          <tr>
              <th>Employee ID</th>
              <th>Last Name</th>
              <th>First Name</th>
              <th>Salary</th>
          </tr>
      </thead>
      <tbody>
          <tr ng-repeat="item in reportData">
              <td>{{item.EmployeeID}}</td>
              <td>{{item.LastName}}</td>
              <td>{{item.FirstName}}</td>
              <td>{{item.Salary}}</td>
          </tr>
      </tbody>
</table>
<hr>
<a href="#" data-ng-click="exportAction('csv')"> Export CSV</a><br/><br/>
<a href="#" data-ng-click="exportAction('excel')"> Export Excel</a><br/><br/>
<a href="#" data-ng-click="exportAction('doc')"> Export Doc</a><br/><br/>
<a href="#" data-ng-click="exportAction('pdf')"> Export Pdf</a><br/><br/>

Export HTML Table to PDF using AngularJS, Here Mudassar Ahmed Khan has explained with an example, how to export HTML Table data to PDF file using AngularJS. The HTML Table  In this blog I will show you how can we easily export html data in pdf on the click of only one button. We need html file which we want to export in pdf form and one angular js file that is used to control the html part.

I've used this:

https://github.com/MrRio/jsPDF

and then you can use in your controller like this:

 $scope.HTMLclick = function () {
                var pdf = new jsPDF();
                pdf.addHTML(($("#pdfContent")[0]), { pagesplit: true }, function () {
                    pdf.save('myfilename' + '.pdf');
                });

            };

How to Export Html Data to PDF in Angularjs, On the click of button execute a function which will mention in angular js file. To export the html part in pdf we need the pdfmake plugin. After  Here Mudassar Ahmed Khan has explained with an example, how to export HTML Table data to PDF file using AngularJS. The HTML Table will be populated from a JSON array using AngularJS. The HTML Table will be first converted into a HTML5 Canvas using html2canvas plugin and then the HTML5 Canvas will be exported to PDF using the pdfmake plugin.

FOR ANGULAR

STEP 1: npm install jspdf-autotable

or in index.html into <head></head> add: <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/2.3.2/jspdf.plugin.autotable.js">

STEP 2:in Angular-cli.json add:

"scripts": [
    "../node_modules/jspdf/dist/jspdf.min.js",
    "../node_modules/jspdf-autotable/dist/jspdf.plugin.autotable.js"
],

STEP 3: app.component.ts or any other component aff below imports

import { Component } from '@angular/core';

declare var jsPDF: any; 

STEP 4: For test put in your component:

export class AppComponent {
  title = 'app works!';

  public columns: string[] = ['Name', 'Phone', 'Whatsapp'];
  public data: string[] = ['Diego Venâncio', '79999565796', '79912345678'];

  constructor() {
    var doc = new jsPDF('p', 'pt');
    doc.autoTable(columns, data);
    doc.save("table.pdf");
  } 

}

more details?

AngularJS exporting to PDF utilising pdfmake.js library., js. bower install pdfmake. This can also be downloaded from github here. Now include the library into your angularJS index.html file. Sometimes, we need to provide the important data of an application in the form of a document like a PDF or an image. Those times we need to convert the HTML layout into the document, and here, we are going to do the same task to convert the HTML into PDF.

Best and feasible way is to use window.print() function. Which does not require any library

Pros

1.No external library require.

2.We can print only selected parts of body also.

3.No css conflicts and js issues.

4.Core html/js functionality

---Simple add below code

CSS to

@media print {
        body * {
            visibility: hidden; // part to hide at the time of print
            -webkit-print-color-adjust: exact !important; // not necessary use         
               if colors not visible
        }

        #printBtn {
            visibility: hidden !important; // To hide 
        }

        #page-wrapper * {
            visibility: visible; // Print only required part
            text-align: left;
            -webkit-print-color-adjust: exact !important;
        }
    }

JS code - Call below function on btn click

$scope.printWindow = function () {
  window.print()
}

Note: Use !important in every css object

Example -

.legend  {
  background: #9DD2E2 !important;
}

Is it possible to export HTML to PDF or generate pdf in AngularJs , I need to export any HTML (Div) to PDF using AngularJs only. or can say need to addImage(data, 'JPEG', 0, 0, canvas.width, canvas.height);. var blob  AngularJS: Export html table to PDF, Excel or Doc formats. When I finally understood what and how to use AngularJS directives, here is the custom directive I created to help export html table

Export to excel and PDF, html> <html lang="ar"> <head> <meta> <script data-require="jquery@*" src=​"https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>  Sometimes, we need to provide the important data of an application in the form of a document like a PDF or image. Those times we need to convert the HTML layout into the document, and here, we are going to do the same task to convert the HTML into PDF.

Convert HTML to PDF using angularjs, I am trying to convert HTML text to pdf using angularjs.But while converting HTML to pdf all text becomes disturb.Means it is not displayed  Generating PDF for reports, forms, invoices, and other data is a common use case for any web application. In a web application, We can generate pdf using various approaches: Using browser print function: It is an easy option when you want to print a complete web page as a pdf. You can also customize pdf up to some limits. limitation of this

Convert HTML to PDF with javascript and AngularJS, If you're looking to generate PDF files with either of those engines straight from your AngularJs code, you've come to the right place. Convert  T he pdfmake.js library can be installed using bower cd to your angular directory and run this code to get the latest version of pdfmake.js. bower install pdfmake. This can also be downloaded from github here. Now include the library into your angularJS index.html file.

Comments
  • yeah ! thank sajeetharan its working fine but when i replace code something like this its not getting
  • function($scope) { $scope.data= [{firstName:"rajeh",lastName:"gatla",gender:"mgale",mobile:"8143445171"}, {firstName:"sathi",lastName:"wesd",gender:"madfgle",mobile:"8143445171"}, {firstName:"gjds",lastName:"hs",gender:"madfhsdfle",mobile:"8143445171"}, {firstName:"dfdff",lastName:"gahgsftla",gender:"mdfsdsale",mobile:"8143445171"}, {firstName:"dsfsd",lastName:"gatWERWDla",gender:"madfshfle",mobile:"8143445171"}, {firstName:"dfagdsew",lastName:"gatlgfha",gender:"mdfgdfale",mobile:"8143445171"}];
  • <tbody> <tr ng-repeat="emp in employees"> <td>{{emp.firstName}}</td> <td>{{emp.lastName}}</td> <td>{{emp.gender}}</td> <td>{{emp.mobile}}</td> </tr> </tbody>
  • @Sajeetharan The only pdf exporter where more than 4 columns can be exported. Thanks for the link.
  • in the demo the exported pdf is not very clean
  • How to have more than 4 columns in pdf export?
  • i think this library exports only table, not even a single header or something, i was looking for invoicing.
  • I did same thing in my angular 5 project and I am getting ERROR ReferenceError: jsPDF is not defined at DashboardComponent.downloadPdf