Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

html2canvas to render document PDF with css styling using angular2/typescript

How to generate an output file PDF using html2canvas with angular2

I tried to import the file html2canvas typescript and made a declaration like this to use it

declare let html2canvas: Html2CanvasStatic;

but I get html2canvas is not defined

html2canvas(document.body, {
  onrendered: function(canvas) {
    document.body.appendChild(canvas);
  }
});

I found this file typescript on github html2canvas typescript

how can use this for my application angular2

like image 700
khalil _diouri Avatar asked Aug 17 '16 14:08

khalil _diouri


People also ask

How to download HTML page from html2canvas using jspdf?

Add a download button to app.component.html which calls the dowload () function, Using jspdf in app.component.ts html2canvas enables you to grab the html page or just an element in it and place it in a pdf.

How to include html2canvas in Angular 4?

If you are using Angular 4 you can include html2canvas under scripts list in .angular-cli.json as below "scripts": [ "../node_modules/html2canvas/dist/html2canvas.min.js" ]

What is html2canvas and how to use it?

html2canvas enables you to grab the html page or just an element in it and place it in a pdf. First install html2canvas and its typings, Then import this at the top of app.component.ts,

How to generate PDF files in angular2+ using jspdf?

To generate pdf files in Angular2+ (specifically angular4), first install jspdf Import jspdf into app.component.ts Add a download button to app.component.html which calls the dowload () function, Using jspdf in app.component.ts html2canvas enables you to grab the html page or just an element in it and place it in a pdf.


4 Answers

I could use html2canvas with the followings changes:

package.json:

 "dependencies": {
     "html2canvas": "0.5.0-beta4",
     "@types/html2canvas": "0.5.32",
 }

After using npm install I could use html2canvas in my component.ts files like this:

import * as html2canvas from "html2canvas"

test() {
    html2canvas(document.body, {
        onrendered: function(canvas) {
        var img = canvas.toDataURL()
        window.open(img);
     }
    });

}

Without installing @types/html2canvasI could use the lib via require but not via the import:

html2canvas = require('hmtl2canvas');
like image 114
mszalbach Avatar answered Oct 18 '22 20:10

mszalbach


In 2018:

html2canvas(document.body).then((canvas) => {
    window.open().document.write('<img src="' + canvas.toDataURL() + '" />');
  });
like image 36
ismaestro Avatar answered Oct 18 '22 19:10

ismaestro


If you are using Angular 4 you can include html2canvas under scripts list in .angular-cli.json as below

"scripts": [
    "../node_modules/html2canvas/dist/html2canvas.min.js"
]

After that import it in your class as below

import * as html2canvas from "html2canvas"

and then use it in your functions as below

html2canvas(parameters);
like image 28
Anil Agrawal Avatar answered Oct 18 '22 20:10

Anil Agrawal


This is in addition to above answer, i.e. add @types/html2canvas to dependencies and add import statement in your code.

However, using the above sample code, I am getting error in VisualStudioCode i.e.

'onrendered' does not exist in type 'Html2CanvasOptions'.

To resolve that, I used "then" as below:

html2canvas(document.body).then((canvas) => {
  document.body.appendChild(canvas);
});
like image 1
pradeep Avatar answered Oct 18 '22 20:10

pradeep