This repository has been archived by the owner on Sep 13, 2024. It is now read-only.
forked from bpmn-io/bpmn-to-image
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
162 lines (124 loc) · 3.09 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
const puppeteer = require('puppeteer');
const fs = require('fs');
const {
basename,
relative
} = require('path');
const {
readFileSync
} = require('fs');
async function printDiagram(page, options) {
const {
input,
outputs,
minDimensions,
footer,
title = true,
deviceScaleFactor,
zoomToElement,
diagramZoom,
size
} = options;
const diagramXML = readFileSync(input, 'utf8');
const diagramTitle = title === false ? false : (
title.length ? title : basename(input)
);
await page.goto(`file://${__dirname}/skeleton.html`);
const viewerScript = relative(__dirname, require.resolve('bpmn-js/dist/bpmn-viewer.production.min.js'));
const desiredViewport = await page.evaluate(async function(diagramXML, options) {
const {
viewerScript,
...openOptions
} = options;
await loadScript(viewerScript);
// returns desired viewport
return openDiagram(diagramXML, openOptions);
}, diagramXML, {
minDimensions,
title: diagramTitle,
viewerScript,
footer,
size
});
page.setViewport({
width: size != null ? desiredViewport.width : Math.round(desiredViewport.width),
height: size != null ? desiredViewport.height : Math.round(desiredViewport.height),
deviceScaleFactor: deviceScaleFactor
});
await page.evaluate(() => resize());
if (zoomToElement != null) {
await page.evaluate((zoomToElement) => {
zoomToElementById(zoomToElement);
}, zoomToElement);
}
if (diagramZoom !== 1) {
await page.evaluate((diagramZoom) => {
zoomDiagram(diagramZoom);
}, diagramZoom)
}
for (const output of outputs) {
console.log(`writing ${output}`);
if (output.endsWith('.pdf')) {
await page.pdf({
path: output,
width: desiredViewport.width,
height: desiredViewport.diagramHeight
});
} else
if (output.endsWith('.png')) {
await page.screenshot({
path: output,
clip: {
x: 0,
y: 0,
width: desiredViewport.width,
height: desiredViewport.diagramHeight
}
});
} else
if (output.endsWith('.svg')) {
const svg = await page.evaluate(() => toSVG());
fs.writeFileSync(output, svg, 'utf8');
} else {
console.error(`Unknown output file format: ${output}`);
}
}
}
async function withPage(fn) {
let browser;
try {
browser = await puppeteer.launch({
args: ['--no-sandbox', '--disable-setuid-sandbox'],
});
await fn(await browser.newPage());
} finally {
if (browser) {
await browser.close();
}
}
}
async function convertAll(conversions, options={}) {
await withPage(async function(page) {
for (const conversion of conversions) {
const {
input,
outputs
} = conversion;
await printDiagram(page, {
input,
outputs,
...options
});
}
});
}
module.exports.convertAll = convertAll;
async function convert(input, output) {
return await convertAll([
{
input,
outputs: [ output ]
}
]);
}
module.exports.convert = convert;