-
Notifications
You must be signed in to change notification settings - Fork 3
/
pdf-to-base64.js
78 lines (66 loc) · 2.28 KB
/
pdf-to-base64.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
/*
* @Author: shubhambansal
* @Date: 2018-10-27 06:30:04
* @Last Modified by: shubhambansal
* @Last Modified time: 2018-10-27 06:59:59
*/
(function(escope){
"use strict";
function validUrl (url) {
return /(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/gi.test(url);
}
function validTypePdf (pdf) {
return /(\.(pdf))/gi.test(pdf);
}
function base64ToBrowser (buffer) {
return window.btoa([].slice.call(new Uint8Array(buffer)).map(function(bin) { return String.fromCharCode(bin) }).join(""));
}
function base64ToNode (buffer) {
return buffer.toString("base64");
}
function readFileAndConvert (fileName) {
var fileSystem = require("fs");
var path = require("path");
if (fileSystem.statSync(fileName).isFile()) {
return base64ToNode(fileSystem.readFileSync(path.resolve(fileName)).toString("base64"));
}
return null;
}
function isPdf (urlOrPdf) {
if (validTypePdf(urlOrPdf)) {
return Promise.resolve(readFileAndConvert(urlOrPdf));
} else {
return Promise.reject("[*] Occurent some error... [validTypePdf] == false");
}
}
function isBrowser (urlOrPdf, param) {
if (!("fetch" in window && "Promise" in window)) {
return Promise.reject("[*] It's pdf2base64 not compatible with your browser.");
}
return fetch(urlOrPdf, param || {}).then(function(response){
return response.arrayBuffer();
}).then(base64ToBrowser);
}
function isNodeJs (urlOrPdf) {
if (validUrl(urlOrPdf)) {
var fetch = require("node-fetch");
return fetch(urlOrPdf).then(function(response){
return response.buffer();
}).then(base64ToNode);
} else {
return isPdf(urlOrPdf);
}
}
function pdfToBase64(urlOrPdf, param) {
if (typeof window !== "undefined" && ("document" in window && "navigator" in window)) {
return isBrowser(urlOrPdf, param);
} else {
return isNodeJs(urlOrPdf);
}
}
if (typeof module !== "undefined") {
module.exports = pdfToBase64;
} else {
escope.pdfToBase64 = pdfToBase64;
}
})(this);