polotno-node 0.3.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +30 -2
- package/instance.js +35 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# Polotno-node
|
|
2
2
|
|
|
3
3
|
Export Polotno JSON into images and pdf files. NodeJS package to work with [Polotno Store API](https://polotno.dev/).
|
|
4
|
+
Should work fine with lambda functions as well.
|
|
4
5
|
|
|
5
6
|
## Usage
|
|
6
7
|
|
|
@@ -68,7 +69,7 @@ const url = await instance.jsonToDataURL(json);
|
|
|
68
69
|
res.json({ url });
|
|
69
70
|
```
|
|
70
71
|
|
|
71
|
-
### `instance.jsonToImageBase64(json)`
|
|
72
|
+
### `instance.jsonToImageBase64(json, attrs)`
|
|
72
73
|
|
|
73
74
|
Export json into base64 string of image.
|
|
74
75
|
|
|
@@ -81,7 +82,7 @@ const imageBase64 = await instance.jsonToImageBase64(json, {
|
|
|
81
82
|
fs.writeFileSync('out.png', imageBase64, 'base64');
|
|
82
83
|
```
|
|
83
84
|
|
|
84
|
-
### `instance.jsonToPDFBase64(json)`
|
|
85
|
+
### `instance.jsonToPDFBase64(json, attrs)`
|
|
85
86
|
|
|
86
87
|
Export json into base64 string of pdf file.
|
|
87
88
|
|
|
@@ -91,3 +92,30 @@ const json = JSON.parse(fs.readFileSync('polotno.json'));
|
|
|
91
92
|
const pdfBase64 = await instance.jsonToPDFBase64(json);
|
|
92
93
|
fs.writeFileSync('out.pdf', pdfBase64, 'base64');
|
|
93
94
|
```
|
|
95
|
+
|
|
96
|
+
### `instance.jsonToPDFDataURL(json, attrs)`
|
|
97
|
+
|
|
98
|
+
Export json into data url of pdf file.
|
|
99
|
+
|
|
100
|
+
```js
|
|
101
|
+
const json = JSON.parse(fs.readFileSync('polotno.json'));
|
|
102
|
+
|
|
103
|
+
const url = await instance.jsonToPDFDataURL(json);
|
|
104
|
+
res.json({ url });
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## Browserless usage
|
|
108
|
+
|
|
109
|
+
By default `polotno-node` will import browser bundle using `chrome-aws-lambda`. But you can use `browserless` instead, to keep your cloud function smaller.
|
|
110
|
+
|
|
111
|
+
```js
|
|
112
|
+
const { createInstance } = require('polotno-node/instance');
|
|
113
|
+
const puppeteer = require('puppeteer');
|
|
114
|
+
|
|
115
|
+
const instance = await createInstance({
|
|
116
|
+
key: 'nFA5H9elEytDyPyvKL7T',
|
|
117
|
+
browser: await puppeteer.connect({
|
|
118
|
+
browserWSEndpoint: 'wss://chrome.browserless.io?token=API_KEY',
|
|
119
|
+
}),
|
|
120
|
+
});
|
|
121
|
+
```
|
package/instance.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const path = require('path');
|
|
2
2
|
const DEFAULT_CLIENT = `file:${path.join(__dirname, 'dist', 'client.html')}`;
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
module.exports.createPage = async (browser, url) => {
|
|
5
5
|
const page = await browser.newPage();
|
|
6
6
|
|
|
7
7
|
page.on('console', (msg) => {
|
|
@@ -23,6 +23,36 @@ const getNewPage = async (browser, url) => {
|
|
|
23
23
|
return page;
|
|
24
24
|
};
|
|
25
25
|
|
|
26
|
+
module.exports.run = async (page, func, args) => {
|
|
27
|
+
return await page.evaluate(func, ...args);
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
module.exports.jsonToDataURL = async (page, json, attrs) => {
|
|
31
|
+
return await page.evaluate(
|
|
32
|
+
async (json, attrs) => {
|
|
33
|
+
const pixelRatio = attrs.pixelRatio || 1;
|
|
34
|
+
store.loadJSON(json);
|
|
35
|
+
store.setElementsPixelRatio(pixelRatio);
|
|
36
|
+
await store.waitLoading();
|
|
37
|
+
return store.toDataURL({ ...attrs, pixelRatio });
|
|
38
|
+
},
|
|
39
|
+
json,
|
|
40
|
+
attrs || {}
|
|
41
|
+
);
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
module.exports.jsonToPDFDataURL = async (page, json, attrs) => {
|
|
45
|
+
return page.evaluate(
|
|
46
|
+
async (json, attrs) => {
|
|
47
|
+
store.loadJSON(json);
|
|
48
|
+
await store.waitLoading();
|
|
49
|
+
return await store.toPDFDataURL(attrs);
|
|
50
|
+
},
|
|
51
|
+
json,
|
|
52
|
+
attrs || {}
|
|
53
|
+
);
|
|
54
|
+
};
|
|
55
|
+
|
|
26
56
|
module.exports.createInstance = async ({
|
|
27
57
|
key,
|
|
28
58
|
url,
|
|
@@ -30,12 +60,13 @@ module.exports.createInstance = async ({
|
|
|
30
60
|
browser,
|
|
31
61
|
} = {}) => {
|
|
32
62
|
const visitPage = url || `${DEFAULT_CLIENT}?key=${key}`;
|
|
33
|
-
|
|
34
|
-
|
|
63
|
+
const firstPage = useParallelPages
|
|
64
|
+
? null
|
|
65
|
+
: await module.exports.createPage(browser, visitPage);
|
|
35
66
|
|
|
36
67
|
const run = async (func, ...args) => {
|
|
37
68
|
const page = useParallelPages
|
|
38
|
-
? await
|
|
69
|
+
? await module.exports.createPage(browser, visitPage)
|
|
39
70
|
: firstPage;
|
|
40
71
|
|
|
41
72
|
const result = await page.evaluate(func, ...args);
|