polotno-node 0.2.2 → 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.
Files changed (4) hide show
  1. package/README.md +30 -2
  2. package/index.js +19 -84
  3. package/instance.js +123 -0
  4. package/package.json +2 -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/index.js CHANGED
@@ -1,32 +1,14 @@
1
1
  const chrome = require('chrome-aws-lambda');
2
2
  const puppeteer = require('puppeteer');
3
- const path = require('path');
4
3
 
5
- const DEFAULT_CLIENT = `file:${path.join(__dirname, 'dist', 'client.html')}`;
4
+ const { createInstance } = require('./instance');
6
5
 
7
- const getNewPage = async (browser, url) => {
8
- const page = await browser.newPage();
9
-
10
- page.on('console', (msg) => {
11
- msg.args().forEach((message) => {
12
- // skip style information
13
- if (message.toString().indexOf('margin') >= 0) {
14
- return;
15
- }
16
- const text = message
17
- .toString()
18
- .replace('JSHandle:', '')
19
- .replace('%c', '');
20
- console.log(text);
21
- });
22
- });
23
-
24
- await page.goto(url);
25
-
26
- return page;
27
- };
28
-
29
- module.exports.createInstance = async ({ key, url, useParallelPages } = {}) => {
6
+ module.exports.createInstance = async ({
7
+ key,
8
+ url,
9
+ useParallelPages,
10
+ browserArgs = [],
11
+ } = {}) => {
30
12
  const browser = await puppeteer.launch({
31
13
  args: [
32
14
  ...chrome.args,
@@ -34,70 +16,23 @@ module.exports.createInstance = async ({ key, url, useParallelPages } = {}) => {
34
16
  '--hide-scrollbars',
35
17
  '--disable-web-security',
36
18
  '--allow-file-access-from-files',
19
+ // more info about --disable-dev-shm-usage
20
+ // https://github.com/puppeteer/puppeteer/issues/1175#issuecomment-369728215
21
+ // https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md#tips
22
+ '--disable-dev-shm-usage',
23
+ ...browserArgs,
37
24
  ],
38
25
  defaultViewport: chrome.defaultViewport,
39
26
  executablePath: await chrome.executablePath,
40
27
  headless: true,
41
28
  ignoreHTTPSErrors: true,
42
29
  });
43
- const visitPage = url || `${DEFAULT_CLIENT}?key=${key}`;
44
-
45
- const firstPage = await getNewPage(browser, visitPage);
46
30
 
47
- const run = async (func, ...args) => {
48
- const page = useParallelPages
49
- ? await getNewPage(browser, visitPage)
50
- : firstPage;
51
-
52
- const result = await page.evaluate(func, ...args);
53
- if (useParallelPages) {
54
- page.close();
55
- }
56
- return result;
57
- };
58
-
59
- const jsonToDataURL = async (json, attrs) => {
60
- return await run(
61
- async (json, attrs) => {
62
- const pixelRatio = attrs.pixelRatio || 1;
63
- store.loadJSON(json);
64
- store.setElementsPixelRatio(pixelRatio);
65
- await store.waitLoading();
66
- return store.toDataURL({ ...attrs, pixelRatio });
67
- },
68
- json,
69
- attrs || {}
70
- );
71
- };
72
-
73
- const jsonToImageBase64 = async (json, attrs) => {
74
- const url = await jsonToDataURL(json, attrs);
75
- return url.split('base64,')[1];
76
- };
77
-
78
- const jsonToPDFDataURL = async (json, attrs) => {
79
- return await run(
80
- async (json, attrs) => {
81
- store.loadJSON(json);
82
- await store.waitLoading();
83
- return await store.toPDFDataURL(attrs);
84
- },
85
- json,
86
- attrs || {}
87
- );
88
- };
89
-
90
- const jsonToPDFBase64 = async (json, attrs) => {
91
- const url = await jsonToPDFDataURL(json, attrs);
92
- return url.split('base64,')[1];
93
- };
94
-
95
- return {
96
- close: async () => await browser.close(),
97
- run,
98
- jsonToDataURL,
99
- jsonToImageBase64,
100
- jsonToPDFDataURL,
101
- jsonToPDFBase64,
102
- };
31
+ return await createInstance({
32
+ browser,
33
+ key,
34
+ url,
35
+ useParallelPages,
36
+ browserArgs,
37
+ });
103
38
  };
package/instance.js ADDED
@@ -0,0 +1,123 @@
1
+ const path = require('path');
2
+ const DEFAULT_CLIENT = `file:${path.join(__dirname, 'dist', 'client.html')}`;
3
+
4
+ module.exports.createPage = async (browser, url) => {
5
+ const page = await browser.newPage();
6
+
7
+ page.on('console', (msg) => {
8
+ msg.args().forEach((message) => {
9
+ // skip style information
10
+ if (message.toString().indexOf('margin') >= 0) {
11
+ return;
12
+ }
13
+ const text = message
14
+ .toString()
15
+ .replace('JSHandle:', '')
16
+ .replace('%c', '');
17
+ console.log(text);
18
+ });
19
+ });
20
+
21
+ await page.goto(url);
22
+
23
+ return page;
24
+ };
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
+
56
+ module.exports.createInstance = async ({
57
+ key,
58
+ url,
59
+ useParallelPages,
60
+ browser,
61
+ } = {}) => {
62
+ const visitPage = url || `${DEFAULT_CLIENT}?key=${key}`;
63
+ const firstPage = useParallelPages
64
+ ? null
65
+ : await module.exports.createPage(browser, visitPage);
66
+
67
+ const run = async (func, ...args) => {
68
+ const page = useParallelPages
69
+ ? await module.exports.createPage(browser, visitPage)
70
+ : firstPage;
71
+
72
+ const result = await page.evaluate(func, ...args);
73
+ if (useParallelPages) {
74
+ page.close();
75
+ }
76
+ return result;
77
+ };
78
+
79
+ const jsonToDataURL = async (json, attrs) => {
80
+ return await run(
81
+ async (json, attrs) => {
82
+ const pixelRatio = attrs.pixelRatio || 1;
83
+ store.loadJSON(json);
84
+ store.setElementsPixelRatio(pixelRatio);
85
+ await store.waitLoading();
86
+ return store.toDataURL({ ...attrs, pixelRatio });
87
+ },
88
+ json,
89
+ attrs || {}
90
+ );
91
+ };
92
+
93
+ const jsonToImageBase64 = async (json, attrs) => {
94
+ const url = await jsonToDataURL(json, attrs);
95
+ return url.split('base64,')[1];
96
+ };
97
+
98
+ const jsonToPDFDataURL = async (json, attrs) => {
99
+ return await run(
100
+ async (json, attrs) => {
101
+ store.loadJSON(json);
102
+ await store.waitLoading();
103
+ return await store.toPDFDataURL(attrs);
104
+ },
105
+ json,
106
+ attrs || {}
107
+ );
108
+ };
109
+
110
+ const jsonToPDFBase64 = async (json, attrs) => {
111
+ const url = await jsonToPDFDataURL(json, attrs);
112
+ return url.split('base64,')[1];
113
+ };
114
+
115
+ return {
116
+ close: async () => await browser.close(),
117
+ run,
118
+ jsonToDataURL,
119
+ jsonToImageBase64,
120
+ jsonToPDFDataURL,
121
+ jsonToPDFBase64,
122
+ };
123
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "polotno-node",
3
- "version": "0.2.2",
3
+ "version": "0.3.1",
4
4
  "description": "Polotno workflow from NodeJS",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -10,6 +10,7 @@
10
10
  "files": [
11
11
  "dist",
12
12
  "index.js",
13
+ "instance.js",
13
14
  "README.md"
14
15
  ],
15
16
  "devDependencies": {