Skip to content

Commit a20b78b

Browse files
Rajeev RanjanRajeev Ranjan
Rajeev Ranjan
authored and
Rajeev Ranjan
committed
Initial commit
1 parent 797c9a2 commit a20b78b

10 files changed

+7426
-0
lines changed

.babelrc

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"presets": [
3+
"@babel/preset-env"
4+
],
5+
"plugins": [
6+
"@babel/plugin-syntax-dynamic-import",
7+
["@babel/plugin-transform-runtime", {
8+
"corejs": false
9+
}],
10+
"@babel/plugin-proposal-class-properties"
11+
]
12+
}

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.DS_Store
2+
node_modules
3+
/dist

.npmignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
index.html
2+
test.png
3+
content.jpg
4+
webpack.config.js
5+
/app/
6+
.babelrc

README.md

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# print-html-to-pdf
2+
3+
Print HTML to pdf using jsPDF & dom-to-image.
4+
5+
## Install
6+
7+
```
8+
npm i print-html-to-pdf
9+
```
10+
11+
## print html to pdf example
12+
13+
```html
14+
<button id="print-button">Print</button>
15+
<div id="print-me">
16+
<p>
17+
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse sit
18+
amet laoreet urna, eu convallis arcu. Etiam eget risus nec justo ultricies
19+
lobortis. Sed vehicula quam tellus, non porttitor felis pulvinar eget.
20+
Integer ac porttitor diam. Donec ultrices vel ex et scelerisque. Ut
21+
vulputate dolor nulla, vitae viverra tortor eleifend ut. Suspendisse
22+
potenti. In sagittis est non lectus blandit, non tempus erat maximus.
23+
Vestibulum id enim dignissim, viverra purus sed, finibus ex. Praesent quis
24+
consectetur est. Cras ac erat auctor, egestas magna et, gravida tellus.
25+
Phasellus non posuere tortor.
26+
</p>
27+
</div>
28+
```
29+
30+
```js
31+
import printHtmlToPDF from "print-html-to-pdf";
32+
33+
const printButton = document.getElementById("print-button");
34+
printButton.addEventListener("click", async (event) => {
35+
const node = document.getElementById("print-me");
36+
const jsPDFOption = {
37+
jsPDF: {
38+
format: "a4",
39+
},
40+
};
41+
await printHtmlToPDF.print(node, jsPDFOption);
42+
});
43+
```
44+
45+
### - jsPDF
46+
47+
please ref to [JSPDF Documentation](http://raw.githack.com/MrRio/jsPDF/master/docs/) for more option

app/print-html-to-pdf.js

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
'use strict';
2+
3+
import { jsPDF } from 'jspdf';
4+
import domtoimage from 'dom-to-image';
5+
import _ from 'lodash';
6+
import spinner from './spinner';
7+
class PrintHtmlToPDF {
8+
9+
// Constructor
10+
constructor() {
11+
this.__init();
12+
};
13+
14+
/*
15+
Print give html node to pdf
16+
@params:
17+
: node is dom element
18+
: default option is defaultOption = {
19+
jsPDF: {
20+
unit: 'px',
21+
format: 'a4',
22+
}
23+
};
24+
*/
25+
print = async (node, option = {}) => {
26+
option = _.merge(this.defaultOption, option);
27+
const fileName = "default";
28+
if (fileName === 'closed') {
29+
return;
30+
}
31+
spinner.spin();
32+
33+
const dataUrl = await this.__htmlToImageDataUrl(node, spinner);
34+
const pdf = new jsPDF(option.jsPDF);
35+
const pdfWidth = pdf.internal.pageSize.getWidth();
36+
const pdfHeight = pdf.internal.pageSize.getHeight();
37+
const imgProps = pdf.getImageProperties(dataUrl);
38+
const imgHeight = pdfWidth / imgProps.width * imgProps.height;
39+
pdf.addImage(dataUrl, option.imageType, 0, 0, pdfWidth, imgHeight);
40+
await pdf.save(fileName, { returnPromise: true });
41+
spinner.stop();
42+
43+
}
44+
45+
// Initialize the basic needs
46+
__init = () => {
47+
this.defaultOption = {
48+
jsPDF: {
49+
unit: 'px',
50+
format: 'a4',
51+
},
52+
imageType: 'image/png',
53+
filename: 'print-html-to-pdf.pdf',
54+
};
55+
};
56+
57+
// Initialize the modal
58+
__initializePrintModalPopup = async () => {
59+
60+
};
61+
62+
// Convert html to png image
63+
__htmlToImageDataUrl = async (node, spinner) => {
64+
return new Promise((resolve, reject) => {
65+
domtoimage.toPng(node)
66+
.then(function (dataUrl) {
67+
resolve(dataUrl);
68+
})
69+
.catch(function (error) {
70+
console.error('printHtmlToPDF', error);
71+
spinner.stop();
72+
reject(error);
73+
});
74+
});
75+
}
76+
77+
};
78+
79+
export default new PrintHtmlToPDF();

app/spinner.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict';
2+
3+
class Spinner {
4+
constructor() {
5+
this.__init();
6+
}
7+
8+
// Spin the spinner
9+
spin = () => {
10+
if (document.getElementById(this.spinnerId) === null || document.getElementById(this.spinnerId) === undefined) {
11+
document.body.appendChild(this.spinnerDiv);
12+
};
13+
const spinner = document.getElementById(this.spinnerId);
14+
this.spinnerDiv.style.height = `${document.body.scrollHeight}px`;
15+
this.spinnerDiv.style.display = "block";
16+
};
17+
18+
// Spin the spinner
19+
stop = () => {
20+
if (document.getElementById(this.spinnerId) !== null) {
21+
const spinner = document.getElementById(this.spinnerId);
22+
this.spinnerDiv.style.display = "none";
23+
}
24+
};
25+
26+
// Initialize the basic needs
27+
__init = () => {
28+
this.spinnerId = 'print-html-to-pdf-spinner-div';
29+
this.__initSpinner();
30+
};
31+
32+
// Initialize the spinner
33+
__initSpinner = () => {
34+
this.spinnerDiv = document.createElement('div');
35+
this.spinnerDiv.id = this.spinnerId;
36+
37+
this.spinnerDiv.style.position = "absolute";
38+
this.spinnerDiv.style.zIndex = "99999999";
39+
this.spinnerDiv.style.top = "0";
40+
this.spinnerDiv.style.left = "0";
41+
this.spinnerDiv.style.width = "100%";
42+
this.spinnerDiv.style.backgroundColor = "rgba(255, 255, 255, 0.8)";
43+
this.spinnerDiv.style.display = "none";
44+
45+
const spinnerImage = document.createElement('img');
46+
spinnerImage.style = "position: absolute; margin: 100px auto auto auto; inset: 0px;";
47+
spinnerImage.alt = "Printing pdf......";
48+
spinnerImage.src = "data:image/svg+xml,%3C%3Fxml version='1.0' encoding='UTF-8' standalone='no'%3F%3E%3Csvg xmlns:svg='http://www.w3.org/2000/svg' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' version='1.0' width='64px' height='64px' viewBox='0 0 128 128' xml:space='preserve'%3E%3Crect x='0' y='0' width='100%25' height='100%25' fill='%23FFFFFF' /%3E%3Cg%3E%3Cpath d='M59.6 0h8v40h-8V0z' fill='%23000000'/%3E%3Cpath d='M59.6 0h8v40h-8V0z' fill='%23cccccc' transform='rotate(30 64 64)'/%3E%3Cpath d='M59.6 0h8v40h-8V0z' fill='%23cccccc' transform='rotate(60 64 64)'/%3E%3Cpath d='M59.6 0h8v40h-8V0z' fill='%23cccccc' transform='rotate(90 64 64)'/%3E%3Cpath d='M59.6 0h8v40h-8V0z' fill='%23cccccc' transform='rotate(120 64 64)'/%3E%3Cpath d='M59.6 0h8v40h-8V0z' fill='%23b2b2b2' transform='rotate(150 64 64)'/%3E%3Cpath d='M59.6 0h8v40h-8V0z' fill='%23999999' transform='rotate(180 64 64)'/%3E%3Cpath d='M59.6 0h8v40h-8V0z' fill='%237f7f7f' transform='rotate(210 64 64)'/%3E%3Cpath d='M59.6 0h8v40h-8V0z' fill='%23666666' transform='rotate(240 64 64)'/%3E%3Cpath d='M59.6 0h8v40h-8V0z' fill='%234c4c4c' transform='rotate(270 64 64)'/%3E%3Cpath d='M59.6 0h8v40h-8V0z' fill='%23333333' transform='rotate(300 64 64)'/%3E%3Cpath d='M59.6 0h8v40h-8V0z' fill='%23191919' transform='rotate(330 64 64)'/%3E%3CanimateTransform attributeName='transform' type='rotate' values='0 64 64;30 64 64;60 64 64;90 64 64;120 64 64;150 64 64;180 64 64;210 64 64;240 64 64;270 64 64;300 64 64;330 64 64' calcMode='discrete' dur='1080ms' repeatCount='indefinite'%3E%3C/animateTransform%3E%3C/g%3E%3C/svg%3E";
49+
this.spinnerDiv.appendChild(spinnerImage);
50+
};
51+
};
52+
53+
export default new Spinner();

index.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const printHtmlToPDF = require('./dist/print-html-to-pdf.min');
2+
module.exports = printHtmlToPDF;

0 commit comments

Comments
 (0)