polotno-node
Advanced tools
Comparing version 0.1.3 to 0.1.4
26
index.js
@@ -17,9 +17,11 @@ const puppeteer = require('puppeteer'); | ||
const run = async (func, ...args) => { | ||
return await page.evaluate(func, ...args); | ||
}; | ||
return { | ||
close: async () => await browser.close(), | ||
run: async (func, ...args) => { | ||
return await page.evaluate(func, ...args); | ||
}, | ||
run, | ||
jsonToDataURL: async (json) => { | ||
return await instance.run(async (json) => { | ||
return await run(async (json) => { | ||
store.loadJSON(json); | ||
@@ -30,3 +32,19 @@ await store.waitLoading(); | ||
}, | ||
jsonToImageBase64: async (json) => { | ||
return await run(async (json) => { | ||
store.loadJSON(json); | ||
await store.waitLoading(); | ||
const url = store.toDataURL(); | ||
return url.split('base64,')[1]; | ||
}, json); | ||
}, | ||
jsonToPDFBase64: async (json) => { | ||
return await run(async (json) => { | ||
store.loadJSON(json); | ||
await store.waitLoading(); | ||
const url = await store.toPDFDataURL(); | ||
return url.split('base64,')[1]; | ||
}, json); | ||
}, | ||
}; | ||
}; |
{ | ||
"name": "polotno-node", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "Polotno workflow from NodeJS", | ||
@@ -17,3 +17,3 @@ "main": "index.js", | ||
"parcel-bundler": "^1.12.4", | ||
"polotno": "^0.17.0", | ||
"polotno": "^0.18.0", | ||
"react": "^17.0.1", | ||
@@ -20,0 +20,0 @@ "react-dom": "^17.0.1" |
@@ -27,19 +27,5 @@ # Polotno-node | ||
// execute loading of JSON and export to image | ||
// we can't directly use "json" variable inside the run function | ||
// we MUST pass it as the second argument | ||
const url = await instance.run(async (json) => { | ||
store.loadJSON(json); | ||
await store.waitLoading(); | ||
return store.toDataURL(); | ||
}, json); | ||
// or the same code, but much shorter: | ||
// const url = await instance.jsonToDataURL(json); | ||
const imageBase64 = await instance.jsonToImageBase64(json); | ||
fs.writeFileSync('out.png', imageBase64, 'base64'); | ||
// prepare base64 string to save | ||
var base64Data = url.replace(/^data:image\/png;base64,/, ''); | ||
// save it to local file | ||
require('fs').writeFileSync('out.png', base64Data, 'base64'); | ||
// close instance | ||
@@ -50,3 +36,40 @@ instance.close(); | ||
run(); | ||
``` | ||
``` | ||
## API | ||
### `instance.run()` | ||
Run any Polotno store API directly inside web-page context | ||
```js | ||
// we can't directly use "json" variable inside the run function | ||
// we MUST pass it as the second argument | ||
const url = await instance.run(async (json) => { | ||
store.loadJSON(json); | ||
await store.waitLoading(); | ||
return store.toDataURL(); | ||
}, json); | ||
``` | ||
### `instance.jsonToImageBase64(json)` | ||
Export json into base64 string of image. | ||
```js | ||
const json = JSON.parse(fs.readFileSync('polotno.json')); | ||
const imageBase64 = await instance.jsonToImageBase64(json); | ||
fs.writeFileSync('out.png', imageBase64, 'base64'); | ||
``` | ||
### `instance.jsonToImageBase64(json)` | ||
Export json into base64 string of pdf file. | ||
```js | ||
const json = JSON.parse(fs.readFileSync('polotno.json')); | ||
const pdfBase64 = await instance.jsonToPDFBase64(json); | ||
fs.writeFileSync('out.pdf', pdfBase64, 'base64'); | ||
``` |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
2802114
2848
74
948
1