Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove handling of file upload, nginx proxy server will handle the fi… #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ RUN apt-get update -y && apt-get install -y -q libnss3 libfontconfig && rm -rf /

COPY --from=0 /chrome /chrome

WORKDIR /server
ADD package.json /server/package.json
RUN npm i

ADD . /server
WORKDIR /server

RUN npm i

EXPOSE 8888

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
"license": "MIT",
"repository": "ElasticSuite/chrome-print",
"dependencies": {
"bluebird": "^3.5.0",
"body-parser": "^1.17.1",
"chrome-remote-interface": "^0.22.0",
"express": "^4.15.2",
"express-fileupload": "^0.1.3",
"node-fs-extra": "^0.8.2",
"tempy": "^0.1.0"
"node-fs-extra": "^0.8.2"
}
}
159 changes: 59 additions & 100 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ const express = require('express');
const bodyParser = require('body-parser');
const fileUpload = require('express-fileupload');
const fs = require('fs-extra');
const tempy = require('tempy');
const CDP = require('chrome-remote-interface');
const Promise = require('bluebird');

const cdpHost = process.env.CHROME_HEADLESS_PORT_9222_TCP_ADDR || 'chrome-headless';
const cdpHost = process.env.CHROME_HEADLESS_PORT_9222_TCP_ADDR || 'localhost';
const cdpPort = process.env.CHROME_HEADLESS_PORT_9222_TCP_PORT || '9222';

function print({
Expand All @@ -17,72 +17,53 @@ function print({
userAgent = null,
full = false
}) {
return new Promise((resolve, reject) => {

// Start the Chrome Debugging Protocol
CDP.New({host: cdpHost, port: cdpPort})
.then(target => CDP({target, host: cdpHost, port: cdpPort}))
.then(client => {


// Extract used DevTools domains.
const {DOM, Emulation, Network, Page, Runtime} = client;

// Set up viewport resolution, etc.
const deviceMetrics = {
width,
height,
deviceScaleFactor: 0,
mobile: false,
fitWindow: false,
};
// Set up viewport resolution, etc.
const deviceMetrics = {
width,
height,
deviceScaleFactor: 0,
mobile: false,
fitWindow: false,
};

let client;
return CDP.New({host: cdpHost, port: cdpPort})
.then(target => CDP({target, host: cdpHost, port: cdpPort}))
.then(c => {
client = c;

// Enable events on domains we are interested in.
Promise.all([
Page.enable(),
DOM.enable(),
Network.enable(),
]).then(() => {
Emulation.setDeviceMetricsOverride(deviceMetrics).then(() => {
Emulation.setVisibleSize({width, height}).then(() => {
// Navigate to target page
Page.navigate({url}).then(() => {
});
});
}).catch((e) => reject(e));
}).catch((e) => reject(e));


// Wait for page load event to take screenshot
Page.loadEventFired(() => {
setTimeout(() => {
Page.printToPDF({
paperWidth: width,
paperHeight: height,

scale: 1,
// landscape: false,
displayHeaderFooter: false,
printBackground: true,
marginTop: 0,
marginBottom: 0,
marginLeft: 0,
marginRight: 0,
pageRanges: '1-1',
}).then((screenshot) => {
const buffer = new Buffer(screenshot.data, 'base64');
client.close();
CDP.Close({id: client.target.id, host: cdpHost, port: cdpPort})
.then(() => resolve(buffer))
.catch(e => reject(e));
}).catch((e) => reject(e));
}, delay);
});
}).catch(err => {
reject(err);
return Promise.all([
client.Page.enable(),
client.DOM.enable(),
client.Network.enable(),
]);
})
.then(() => client.Emulation.setDeviceMetricsOverride(deviceMetrics))
.then(() => client.Emulation.setVisibleSize({width, height}))
.then(() => client.Page.navigate({url}))
.then(() => client.Page.loadEventFired())
.then(() => Promise.delay(delay))
.then(() => client.Page.printToPDF({
paperWidth: width,
paperHeight: height,

scale: 1,
// landscape: false,
displayHeaderFooter: false,
printBackground: true,
marginTop: 0,
marginBottom: 0,
marginLeft: 0,
marginRight: 0,
pageRanges: '1-1',
}))
.then((screenshot) => {
const buffer = new Buffer(screenshot.data, 'base64');
client.close();
return buffer;
});

});
}

const app = express();
Expand All @@ -98,45 +79,23 @@ curl -F "htmlFile=@test.html" -F "width=8.5" -F "height=11" -X POST -H "Content-
});

app.post('/', (req, res) => {
const file = req.files.htmlFile;
const width = req.body.width ? parseInt(req.body.width, 10) : undefined;
const height = req.body.height ? parseInt(req.body.height, 10) : undefined;
const delay = req.body.delay ? parseInt(req.body.delay, 10) : undefined;

if (!file) {
return res.status(422).send('No htmlFile sent.');
}

const tmp = tempy.file({extension: 'html'});

file.mv(tmp, (err) => {
if (err) {
res.status(500).send('There was an error.');
throw err;
}

const newPath = `/printfiles/${tmp.replace(/^.*\/(.*)$/, '$1')}`;
fs.move(tmp, newPath, {overwrite: true}, err => {
if (err) {
console.log(err);
res.status(500).send('There was an error.');
}

print({
width,
height,
delay,
url: 'file://' + newPath
}).then((data) => {
res.status(200).type('application/pdf').send(data);
fs.remove(newPath);
}).catch((e) => {
console.log(e);
res.status(500).send('some kind of failure');
});
});

})
const filename = req.body.filename;

print({
width,
height,
delay,
url: `file:///printfiles/${filename}`
}).then((data) => {
res.status(200).type('application/pdf').send(data);
fs.remove(`/printfiles/${filename}`);
}).catch((e) => {
console.log(e);
res.status(500).send('some kind of failure');
});
});

app.listen(process.env.NODE_PORT || 8888);