-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathPDFDownloadLink.js
41 lines (33 loc) · 914 Bytes
/
PDFDownloadLink.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
import { useEffect } from 'react';
import usePDF from './usePDF';
export const PDFDownloadLink = ({
fileName = 'document.pdf',
document: doc,
children,
onClick,
href,
...rest
}) => {
const [instance, updateInstance] = usePDF();
useEffect(() => updateInstance(doc), [doc]);
if (!doc) {
console.warn('You should pass a valid document to PDFDownloadLink');
return null;
}
const handleDownloadIE = () => {
if (instance && window.navigator.msSaveBlob) {
// IE
window.navigator.msSaveBlob(instance.blob, fileName);
}
};
const handleClick = (event) => {
handleDownloadIE();
if (typeof onClick === 'function') onClick(event, instance);
};
return (
<a href={instance.url} download={fileName} onClick={handleClick} {...rest}>
{typeof children === 'function' ? children(instance) : children}
</a>
);
};
export default PDFDownloadLink;