eKoopmans / html2pdf.js

Client-side HTML-to-PDF rendering using pure JS.
MIT License
4.11k stars 1.39k forks source link

Importing html2pdf ionic3/Angular4 #113

Open smart548 opened 6 years ago

smart548 commented 6 years ago

Hi, I've downloaded the library through the npm install command as suggested and everything installed in the node_modules folder as expected. The problem is that I don't know how to import it in my components; I've tried to:

Can you please elaborate on how can I use your lib in my project (or basically in any typescript project)?

Thank you.

cscanlin commented 6 years ago

I was able to get it working with: import html2pdf from 'html2pdf.js'

jainvishal520 commented 6 years ago

@cscanlin I installed the library using npm commands and imported with import html2pdf from 'html2pdf.js' as mentioned by you. But I'm getting error that html2pdf is not a function. Can you please help me with this?

sameerpallav commented 6 years ago

You'll have to inlcude it angular-cli.json under scripts like "scripts": [ "assets/js/html2pdf.bundle.js", ]

In your component: declare var html2pdf: any;

Now you can use html2pdf variable

const element = ele.nativeElement;
        const opt = {
            margin:       0,
            filename:     'myfile.pdf',
            image:        { type: 'jpeg', quality: 0.98 },
            html2canvas:  { scale: 2 , dpi: 300,
                letterRendering: true,
                useCORS: true},
            jsPDF:        { unit: 'in', format: 'a4', orientation: 'portrait' }
        };

        // New Promise-based usage:
        html2pdf().from(element).set(opt).save();