-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathindex.js
125 lines (110 loc) · 3.11 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import html2canvas from 'html2canvas';
import JsPDF from 'jspdf';
import ReactDOM from 'react-dom';
const fileType = {
PNG: 'image/png',
JPEG: 'image/jpeg',
PDF: 'application/pdf'
};
const DEFAULT_PNG = {
fileName: 'component.png',
type: fileType.PNG,
html2CanvasOptions: {}
};
const DEFAULT_JPEG = {
fileName:'component.jpg',
type: fileType.JPEG,
html2CanvasOptions: {}
};
const DEFAULT_PDF = {
fileName: 'component.pdf',
type: fileType.PDF,
html2CanvasOptions: {},
pdfOptions: {}
};
/**
* @param {string} uri
* @param {string} filename
*/
const saveAs = (uri, filename) => {
const link = document.createElement('a');
if (typeof link.download === 'string') {
link.href = uri;
link.download = filename;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
} else {
window.open(uri);
}
};
/**
* @param {React.RefObject} node
* @param {string} fileName
* @param {string} type
* @param {object} html2CanvasOptions={}
*/
const getPDF = (canvas, {w, h, orientation, unit = 'mm', pdfFormat}) => {
const width = w || canvas.width
const height = h || canvas.height
const o = orientation || width > height ? 'l' : 'p'
const format = pdfFormat || 'a4'
return new JsPDF(o, unit, format)
}
const exportComponent = (node, {
fileName,
type,
html2CanvasOptions,
pdfOptions
}) => {
if(!node.current) {
throw new Error("'node' must be a RefObject")
}
const element = ReactDOM.findDOMNode(node.current);
return html2canvas(element, {
scrollY: -window.scrollY,
useCORS: true,
...html2CanvasOptions
}).then(canvas => {
if (type === fileType.PDF) {
const pdf = getPDF(canvas, pdfOptions)
pdf.addImage(
canvas.toDataURL(fileType.PNG, 1.0),
'PNG',
pdfOptions.x || 0,
pdfOptions.y || 0,
pdfOptions.w || canvas.width,
pdfOptions.h || canvas.height
);
pdf.save(fileName);
} else {
saveAs(canvas.toDataURL(type, 1.0), fileName);
}
});
};
/**
* @param {React.RefObject} node
* @param {string} fileName='component.png'
* @param {object} html2CanvasOptions={}
*/
const exportComponentAsPNG = (node, parameters = {}) => exportComponent(node, {...DEFAULT_PNG, ...parameters});
/**
* @param {React.RefObject} node
* @param {string} fileName='component.jpeg'
* @param {string} type=fileType.JPEG
* @param {object} html2CanvasOptions={}
*/
const exportComponentAsJPEG = (node, parameters = {}) => exportComponent(node, {...DEFAULT_JPEG, ...parameters});
/**
* @param {React.RefObject} node
* @param {string} fileName='component.pdf'
* @param {string} type=fileType.PDF
* @param {object} html2CanvasOptions={}
* @param {string} pdfOptions={}
*/
const exportComponentAsPDF = (node, parameters = {}) => exportComponent(node, {...DEFAULT_PDF, ...parameters});
export {
exportComponentAsJPEG,
exportComponentAsPDF,
exportComponentAsPNG
};