Skip to content

Commit 822a353

Browse files
Sanskriti LalSanskriti Lal
Sanskriti Lal
authored and
Sanskriti Lal
committed
Added an option in the db-drawer tool that allows users to export the visualized database schema as a PDF or PNG file
1 parent a302324 commit 822a353

File tree

1 file changed

+67
-9
lines changed

1 file changed

+67
-9
lines changed

index.js

Lines changed: 67 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
const { spawn } = require("child_process");
44
const fs = require('fs');
55
const path = require("path");
6+
const express = require('express');
7+
const puppeteer = require('puppeteer');
8+
const { PDFDocument } = require('pdf-lib');
69

710
// Define variables
811
let nodeProcess = null;
@@ -21,17 +24,52 @@ if (process.argv.length === 3) {
2124

2225
// Initialization function
2326
function init() {
24-
//check if /model is present or not
25-
modelDirPath = path.join(process.cwd(), '/models')
26-
if (!fs.existsSync(modelDirPath)){
27-
console.log("Error : models folder not found, create a models folder to visualize schemas")
28-
process.exit()
27+
// Check if /model is present or not
28+
modelDirPath = path.join(process.cwd(), '/models');
29+
if (!fs.existsSync(modelDirPath)) {
30+
console.log("Error: models folder not found, create a models folder to visualize schemas");
31+
process.exit();
2932
}
3033

34+
const app = express();
35+
const port = 3000;
36+
37+
// Serve static files (your visualizations)
38+
app.use(express.static(path.join(__dirname, 'public')));
39+
40+
// Add routes for export functionality
41+
app.get('/export/pdf', async (req, res) => {
42+
try {
43+
const pdf = await generatePDF();
44+
res.setHeader('Content-Type', 'application/pdf');
45+
res.setHeader('Content-Disposition', 'attachment; filename=schema.pdf');
46+
res.send(pdf);
47+
} catch (err) {
48+
console.error(err);
49+
res.status(500).send('Error generating PDF');
50+
}
51+
});
52+
53+
app.get('/export/png', async (req, res) => {
54+
try {
55+
const png = await generatePNG();
56+
res.setHeader('Content-Type', 'image/png');
57+
res.setHeader('Content-Disposition', 'attachment; filename=schema.png');
58+
res.send(png);
59+
} catch (err) {
60+
console.error(err);
61+
res.status(500).send('Error generating PNG');
62+
}
63+
});
64+
65+
app.listen(port, () => {
66+
console.log(`Server is running at http://localhost:${port}`);
67+
});
68+
3169
nodeProcess = startProcess();
3270
watchFiles();
33-
process.on('SIGINT', async () => { await exitHandler() });
34-
process.on('SIGTERM', async () => { await exitHandler() });
71+
process.on('SIGINT', async () => { await exitHandler(); });
72+
process.on('SIGTERM', async () => { await exitHandler(); });
3573
}
3674

3775
// Start the child process
@@ -61,7 +99,7 @@ function startProcess() {
6199
processExited = false;
62100
childProcess.on('close', () => {
63101
processExited = true;
64-
console.log('server restarting');
102+
console.log('Server restarting');
65103
});
66104
childProcess.on('error', () => {
67105
processExited = true;
@@ -78,7 +116,7 @@ function watchFiles() {
78116
fs.watchFile(dir, { interval: 1000 }, (curr, prev) => {
79117
// Check if file content has changed
80118
if (curr.mtime !== prev.mtime) {
81-
console.log("files modified...");
119+
console.log("Files modified...");
82120
clearTimeout(previousReloadTimer);
83121
previousReloadTimer = setTimeout(async () => {
84122
await reload();
@@ -112,3 +150,23 @@ async function exitHandler() {
112150
await stopProcess();
113151
process.exit();
114152
}
153+
154+
// Generate PDF function
155+
async function generatePDF() {
156+
const browser = await puppeteer.launch();
157+
const page = await browser.newPage();
158+
await page.goto(`http://localhost:${3000}`); // URL of your visualization
159+
const pdf = await page.pdf({ format: 'A4' });
160+
await browser.close();
161+
return pdf;
162+
}
163+
164+
// Generate PNG function
165+
async function generatePNG() {
166+
const browser = await puppeteer.launch();
167+
const page = await browser.newPage();
168+
await page.goto(`http://localhost:${3000}`); // URL of your visualization
169+
const screenshot = await page.screenshot({ fullPage: true });
170+
await browser.close();
171+
return screenshot;
172+
}

0 commit comments

Comments
 (0)