replaced phantomjs with puppeteer

started to replace phantomjs with puppeteer
This commit is contained in:
bckmnn
2021-02-14 17:18:50 +01:00
parent 20bbb5aa08
commit 26329b000b
3 changed files with 45 additions and 33 deletions

View File

@@ -21,6 +21,20 @@ RUN cd audiowaveform/build/ && cmake -D ENABLE_TESTS=0 -D BUILD_STATIC=1 ..
RUN cd audiowaveform/build/ && make RUN cd audiowaveform/build/ && make
RUN cd audiowaveform/build/ && make install RUN cd audiowaveform/build/ && make install
# install chromium
RUN apk add --no-cache \
chromium \
nss \
freetype \
freetype-dev \
harfbuzz \
ca-certificates \
ttf-freefont
# Tell Puppeteer to skip installing Chrome. We'll be using the installed package.
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true \
PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium-browser
# install other requirements # install other requirements
RUN apk add graphicsmagick ffmpeg ffmpeg-dev ghostscript RUN apk add graphicsmagick ffmpeg ffmpeg-dev ghostscript

View File

@@ -2,7 +2,7 @@
const db = require('../models/db'); const db = require('../models/db');
const config = require('config'); const config = require('config');
const phantom = require('node-phantom-simple'); const puppeteer = require('puppeteer');
const os = require('os'); const os = require('os');
module.exports = { module.exports = {
@@ -25,46 +25,44 @@ module.exports = {
var on_exit = function(exit_code) { var on_exit = function(exit_code) {
if (exit_code>0) { if (exit_code>0) {
console.error("phantom abnormal exit for url "+space_url); console.error(exit_code);
console.error("puppeteer abnormal exit for url "+space_url);
if (!on_success_called && on_error) { if (!on_success_called && on_error) {
on_error(); on_error();
} }
} }
}; };
phantom.create({ path: require('phantomjs-prebuilt').path }, function (err, browser) { (async () => {
if (err) { let browser;
console.error(err); let page;
} else { try {
return browser.createPage(function (err, page) { browser = await puppeteer.launch(
{
headless: true,
args: ['--disable-dev-shm-usage', '--no-sandbox']
}
);
page = await browser.newPage();
page.setDefaultTimeout(timeout);
await page.setJavaScriptEnabled(false);
console.log("page created, opening ",space_url); console.log("page created, opening ",space_url);
await page.goto(space_url, {waitUntil: 'networkidle0'});
if (type=="pdf") { if (type=="pdf") {
var psz = { await page.pdf({path: export_path, width: space.width+'px', height: space.height+'px' });
width: space.width+"px", }else{
height: space.height+"px" await page.screenshot({path: export_path});
};
page.set('paperSize', psz);
} }
page.set('settings.resourceTimeout',timeout); await browser.close();
page.set('settings.javascriptEnabled',false);
return page.open(space_url, function (err,status) {
page.render(export_path, function() {
on_success_called = true;
if (on_success) {
on_success(export_path); on_success(export_path);
} } catch (error) {
page.close(); on_error();
browser.exit();
});
});
});
} }
}, { })();
onExit: on_exit
});
} }
}; };

View File

@@ -33,7 +33,7 @@
"node-phantom-simple": "2.2.4", "node-phantom-simple": "2.2.4",
"node-server-screenshot": "^0.2.1", "node-server-screenshot": "^0.2.1",
"nodemailer": "^4.6.7", "nodemailer": "^4.6.7",
"phantomjs-prebuilt": "^2.1.16", "puppeteer": "3.0.0",
"read-chunk": "^2.1.0", "read-chunk": "^2.1.0",
"request": "^2.88.0", "request": "^2.88.0",
"sanitize-html": "^1.11.1", "sanitize-html": "^1.11.1",