-
Notifications
You must be signed in to change notification settings - Fork 830
Expand file tree
/
Copy pathgeneratePdf.js
More file actions
52 lines (46 loc) · 1.44 KB
/
generatePdf.js
File metadata and controls
52 lines (46 loc) · 1.44 KB
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
const { mdToPdf } = require("md-to-pdf");
const os = require("os");
const { execSync } = require("child_process");
// Detect platform and architecture
const platform = os.platform();
const arch = os.arch();
console.log(`Detected: ${platform} ${arch}`);
// Install correct Chrome binary if not already installed
const chromePath = `${os.homedir()}/.cache/puppeteer/chrome/mac_${arch === "arm64" ? "arm" : "x86"}-*`;
try {
const chromeExists = execSync(
`ls ${chromePath} 2>/dev/null || echo "notfound"`,
)
.toString()
.trim();
if (chromeExists === "notfound" || chromeExists === "") {
console.log("Installing Chrome binary for current architecture...");
execSync(
`PUPPETEER_CACHE_DIR=${os.homedir()}/.cache/puppeteer npx puppeteer browsers install chrome`,
{
stdio: "inherit",
},
);
}
} catch (error) {
console.log("Chrome binary check/install completed");
}
// Generate PDF
(async () => {
await mdToPdf(
{ path: "./notes/lectures.md" },
{
dest: "./dist/namaste-javascript-notes.pdf",
launch_options: {
args: ["--no-sandbox", "--disable-setuid-sandbox"],
},
pdf_options: {
format: "A4",
margin: { top: "20mm", right: "20mm", bottom: "20mm", left: "20mm" },
printBackground: true,
},
basedir: process.cwd(), // Use current working directory as base for relative paths
},
);
console.log("PDF generated successfully!");
})();