-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
128 lines (120 loc) · 3.62 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
126
127
128
#!/usr/bin/env node
// build command cant be run with npm run build. Use npx and copy paste it from the package.json.
import chalk from "chalk";
import inquirer from "inquirer";
import figlet from "figlet";
import { createSpinner } from "nanospinner";
import puppeteer from "puppeteer";
import path from "path";
function welcome() {
console.clear();
console.log(
chalk.yellowBright(
figlet.textSync("Webpage\nto PDF\nConverter", {
font: "Calvin S",
})
) + "\n"
);
}
async function askURL() {
const data = await inquirer.prompt([
{
name: "URL",
type: "input",
message:
"Enter the URL of the page to be converted (should be contain http(s)): ",
default() {
return null;
},
validate(value) {
if (value === null)
return "No input. Enter a URL or press Ctrl+C to exit";
const pattern = new RegExp(
/^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,}))\.?)(?::\d{2,5})?(?:[/?#]\S*)?$/i
);
if (!pattern.test(value))
return `Entry not valid. Enter a valid URL or press Ctrl+C to exit ${pattern.test(
value
)}`;
return true;
},
},
{
name: "filename",
type: "input",
message:
"Enter the filename to be saved (a-z, A-Z, 0-9, -, _, no spaces or other special characters): ",
default() {
return "page";
},
validate(value) {
const pattern = new RegExp(/^[a-zA-Z0-9_-]+$/g);
if (!pattern.test(value)) return "Enter a valid filename";
return true;
},
},
]);
return data;
}
// function setHttp(link) {
// if (link.search(/^http[s]?\:\/\//) == -1) {
// link = "http://" + link;
// }
// return link;
// }
async function generatePDF(data) {
const { URL, filename } = data;
let spinner = createSpinner("Launching browser...").start();
let browser;
try {
browser = await puppeteer.launch({
headless: true,
});
spinner.success({ text: "Browser Opened" });
} catch (error) {
spinner.error({ text: "Failed to open browser. Try again." });
process.exit(0);
}
spinner = createSpinner("Opening tab...").start();
let webPage;
try {
webPage = await browser.newPage();
spinner.success({ text: "New tab opened" });
} catch (error) {
spinner.error({ text: "Failed to open a new tab. Try again." });
process.exit(0);
}
spinner = createSpinner("Opening URL...").start();
try {
await webPage.goto(URL, {
waitUntil: "networkidle0",
});
spinner.success({ text: "URL loaded" });
} catch (error) {
spinner.error({ text: "Failed to open the URL. Try again." });
process.exit(0);
}
spinner = createSpinner(
"Converting page to PDF and saving to disk..."
).start();
try {
await webPage.pdf({
path: path.join(process.cwd(), `${filename}.pdf`),
printBackground: true,
format: "A4",
});
spinner.success({
text: `PDF saved at ${path.join(process.cwd(), `${filename}.pdf`)}`,
});
} catch (error) {
spinner.error({
text: "Failed to covert and save the PDF. Try again." + error,
});
process.exit(0);
}
await browser.close();
}
welcome();
const data = await askURL();
await generatePDF(data);
process.exit(0);