Installations
npm install jspdf-html2canvas
Developer Guide
Typescript
Yes
Module System
CommonJS
Node Version
16.20.2
NPM Version
8.19.4
Score
48.8
Supply Chain
96
Quality
76.3
Maintenance
100
Vulnerability
98.9
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (67.55%)
HTML (26.96%)
JavaScript (5.49%)
Developer
johnnywang1994
Download Statistics
Total Downloads
851,204
Last Day
486
Last Week
9,512
Last Month
55,970
Last Year
440,648
GitHub Statistics
78 Stars
65 Commits
9 Forks
3 Watching
5 Branches
1 Contributors
Package Meta Information
Latest Version
1.5.2
Package Id
jspdf-html2canvas@1.5.2
Unpacked Size
17.44 kB
Size
6.95 kB
File Count
16
NPM Version
8.19.4
Node Version
16.20.2
Publised On
27 Feb 2024
Total Downloads
Cumulative downloads
Total Downloads
851,204
Last day
-83.3%
486
Compared to previous day
Last week
-31.5%
9,512
Compared to previous week
Last month
5.3%
55,970
Compared to previous month
Last year
158.3%
440,648
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
2
Dev Dependencies
5
jsPDF-html2canvas
A combine usage with jsPDF & html2canvas, which translating html content to PDF file. Written in Typescript.
html2PDF function will auto fit the target dom width into PDF size. So no need to worry about the overflow part. And if the content height is over 1 pdf, it'll auto seperate it into another pdf page.
Install
npm i jspdf-html2canvas
1import html2PDF from 'jspdf-html2canvas'; 2 3html2PDF(node, options);
since this plugin is an umd module, you can also use by cdn with /dist/jspdf-html2canvas.min.js
, just remember to include both jspdf
& html2canvas
cdn before this plugin.
1<script src="https://unpkg.com/jspdf@latest/dist/jspdf.umd.min.js"></script> 2<script src="https://cdn.jsdelivr.net/npm/html2canvas@1.4.1/dist/html2canvas.min.js"></script> 3<script src="https://cdn.jsdelivr.net/npm/jspdf-html2canvas@latest/dist/jspdf-html2canvas.min.js"></script>
html2PDF([Node, NodeList], options)
convert specific DOM target to print it into PDF file.
Automatically, it'll save the file, or you can define the success
callback to do with the jsPDF instance.
1<!-- default a4's width is 595.28px --> 2<div id="page" style="width: 595.28px;color: black;background: white;"> 3 <h3>PDF for Test</h3> 4 <p>Here is some content for testing!!</p> 5</div> 6 7<button id="btn">Generate</button>
1let btn = document.getElementById('btn');
2let page = document.getElementById('page');
3
4btn.addEventListener('click', function(){
5 html2PDF(page, {
6 jsPDF: {
7 format: 'a4',
8 },
9 imageType: 'image/jpeg',
10 output: './pdf/generate.pdf'
11 });
12});
you can easily await
the method to wait for pdf generated.
1async function printPdf() { 2 const pdf = await html2PDF(page, { 3 // ... 4 }); 5 // do something with pdf(jsPdf instance) 6}
If there's some white space on top of the outputed PDF file, it might caused by the scroll problem, just add some settings for
html2canvas
plugin as following. see the reference
1html2PDF(page, {
2 // ... other settings
3 html2canvas: {
4 scrollX: 0,
5 scrollY: -window.scrollY,
6 },
7});
Custom multiple page supported
There might be some situation you want to print DOM seperately, just easily give the nodeList with length
in it, will adjust every nodes inside seperately into a new page in the same PDF output.
for example:
1<div id="page" style="width: 595.28px;color: black;background: white;"> 2 <div class="page page-1"> 3 <h3>Test page 1</h3> 4 <p>This is an page for testing 1</p> 5 </div> 6 <div class="page page-2"> 7 <h3>Test page 2</h3> 8 <p>This is an page for testing 1</p> 9 </div> 10 <div class="page page-3"> 11 <h3>Test page 3</h3> 12 <p>This is an page for testing 1</p> 13 </div> 14</div>
1const pages = document.getElementsByClassName('page');
2
3btn.addEventListener('click', function(){
4 html2PDF(pages, {
5 jsPDF: {
6 format: 'a4',
7 },
8 imageType: 'image/jpeg',
9 output: './pdf/generate.pdf'
10 });
11});
Options
- jsPDF
- type:
Object
- default:
1{ 2 unit: 'pt', 3 format: 'a4' 4}
setting for creating jsPDF's instance, please ref to JSPDF Documentation
- html2canvas
- type:
Object
- default:
1{ 2 imageTimeout: 15000, 3 logging: true, 4 useCORS: false 5}
setting for html2canvas
configs, please ref to html2canvas Documentation
- watermark
- type:
String
|Function
|Object
- optional
setting for watermark in pdf, will add watermark into each pages of your outputed pdf file.
each data type has different usage as following:
datatype: String
=> image url
create image watermark in the center of each page with default image scale size 1
, please use .png
file for watermark.
1html2PDF(page, {
2 watermark: './test.png',
3});
datatype: Function
=> custom handler
define custom handler to do things for each page of pdf file.
1html2PDF(page, { 2 watermark({ pdf, pageNumber, totalPageNumber }) { 3 // pdf: jsPDF instance 4 pdf.setTextColor('#ddd'); 5 pdf.text(50, pdf.internal.pageSize.height - 30, `Watermark, page: ${pageNumber}/${totalPageNumber}`); 6 }, 7});
datatype: Object
=> custom handler or resize image watermark
define image watermark with change ratio
, or use custom handler
to do with the image position.
1html2PDF(page, {
2 watermark: {
3 src: './test.png',
4 scale: 0.5
5 },
6});
7// or
8html2PDF(page, {
9 watermark: {
10 src: './test.png',
11 handler({ pdf, imgNode, pageNumber, totalPageNumber }) {
12 const props = pdf.getImageProperties(imgNode);
13 // do something...
14 pdf.addImage(imgNode, 'PNG', 0, 0, 40, 40);
15 },
16 },
17});
- imageType
- type:
String
- allowed:
image/jpeg
,image/png
,image/webp
- default:
image/jpeg
define the target imageType, now only support for jpeg, png, webp
1// will be used like
2let pageData = canvas.toDataURL(opts.imageType, opts.imageQuality);
- imageQuality
- type:
Number
- allowed:
0 - 1
- default:
1
define the image quality transfered from canvas
- margin
- type:
Object{key => number}
- allowed key:
top
,right
,bottom
,left
- default:
0
define the margin of each page
- autoResize
- type:
Boolean
- default:
true
define whether to auto resize the snapshot image to fit PDF layout size
- output
- type:
String
- default:
jspdf-generate.pdf
define name of the output PDF file
1pdf.save(opts.output);
- init
- type:
Function
1function init(pdf) { 2 pdf.setFont('Myfont'); 3 pdf.setFontSize(10); 4}
define some init for jspdf initiating before printing
- success
- type:
Function
- default:
1function success(pdf) { 2 pdf.save(this.output); 3}
callback function to do after all code, default will save the file with the output name setting.
Defaults options
1const defaultOptions = { 2 jsPDF: { 3 unit: 'pt', 4 format: 'a4', 5 }, 6 html2canvas: { 7 imageTimeout: 15000, 8 logging: true, 9 useCORS: false, 10 }, 11 imageType: 'image/jpeg', 12 imageQuality: 1, 13 margin: { 14 top: 0, 15 right: 0, 16 bottom: 0, 17 left: 0, 18 }, 19 watermark: undefined, 20 autoResize: true, 21 output: 'jspdf-generate.pdf', 22 init: function() {}, 23 success: function(pdf) { 24 pdf.save(this.output); 25 } 26}
Recommend
if you want more custom & widing solutions, you can use this npm package
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
packaging workflow detected
Details
- Info: Project packages its releases by way of GitHub Actions.: .github/workflows/npm-publish.yml:7
Reason
6 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-gx9m-whjm-85jf
- Warn: Project is vulnerable to: GHSA-mmhx-hmjr-r674
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-4vvj-4cpr-p986
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/npm-publish.yml:1
- Info: no jobLevel write permissions found
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/npm-publish.yml:11: update your workflow using https://app.stepsecurity.io/secureworkflow/johnnywang1994/jsPDF-html2canvas/npm-publish.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/npm-publish.yml:13: update your workflow using https://app.stepsecurity.io/secureworkflow/johnnywang1994/jsPDF-html2canvas/npm-publish.yml/master?enable=pin
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
Reason
no SAST tool detected
Details
- Warn: no pull requests merged into dev branch
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Score
3.3
/10
Last Scanned on 2024-12-23
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to jspdf-html2canvas
jspdf-pro
jspdf+html2canvas生成pdf并解决过长导致的canvas空白问题并支持自动分页、跨页处理
jspdf-with-html2canvas
PDF Document creation from JavaScript
@zendostrike/jspdf-html2canvas-pro
jsPDF fork but using html2canvas-pro. This is a temporary workaround.
@colinng/jspdf-html2canvas-pro
jsPDF fork but using html2canvas-pro. This is a temporary workaround.