Skip to content

Commit d47f1d9

Browse files
committed
initial commit
0 parents  commit d47f1d9

File tree

9 files changed

+575
-0
lines changed

9 files changed

+575
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

Dockerfile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
FROM node:6
2+
3+
# chrome deps
4+
RUN apt-get update && apt-get upgrade -y && apt-get install -y -q \
5+
gconf-service libasound2 libatk1.0-0 libcups2 libdbus-1-3 libgconf-2-4 libgtk-3-0 \
6+
libnspr4 libnss3 libx11-xcb1 libxss1 fonts-liberation libappindicator1 xdg-utils
7+
8+
RUN wget https://dl.google.com/linux/direct/google-chrome-beta_current_amd64.deb && \
9+
dpkg -i google-chrome-beta_current_amd64.deb && \
10+
rm google-chrome-beta_current_amd64.deb
11+
12+
ADD . /server
13+
WORKDIR /server
14+
15+
RUN npm i
16+
17+
#CMD ["./start.sh"]
18+
ENTRYPOINT ["./start.sh"]

download-chromium.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
CHROMIUM_ROOT="http://commondatastorage.googleapis.com/chromium-browser-snapshots/Linux_x64"
3+
CHROMIUM_LATEST=`wget -q -O - "$CHROMIUM_ROOT/LAST_CHANGE"`
4+
wget $CHROMIUM_ROOT/$CHROMIUM_LATEST/chrome-linux.zip

package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "chrome-print",
3+
"version": "1.0.0",
4+
"description": "simple headless chrome-based print server",
5+
"main": "server.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1",
8+
"start": "node server.js"
9+
},
10+
"author": "Lyle Underwood <[email protected]>",
11+
"license": "UNLICENSED",
12+
"dependencies": {
13+
"body-parser": "^1.17.1",
14+
"chrome-remote-interface": "^0.22.0",
15+
"express": "^4.15.2",
16+
"express-fileupload": "^0.1.3",
17+
"tempy": "^0.1.0"
18+
}
19+
}

server.js

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
const express = require('express');
2+
const bodyParser = require('body-parser');
3+
const fileUpload = require('express-fileupload');
4+
const fs = require('fs');
5+
const tempy = require('tempy');
6+
const CDP = require('chrome-remote-interface');
7+
8+
function getScreenshot({
9+
url,
10+
format = 'png',
11+
width = 1920,
12+
height = 1080,
13+
delay = 300,
14+
userAgent = null,
15+
full = false
16+
}) {
17+
return new Promise((resolve, reject) => {
18+
19+
// Start the Chrome Debugging Protocol
20+
CDP(function(client) {
21+
// Extract used DevTools domains.
22+
const {DOM, Emulation, Network, Page, Runtime} = client;
23+
24+
// Set up viewport resolution, etc.
25+
const deviceMetrics = {
26+
width,
27+
height,
28+
deviceScaleFactor: 0,
29+
mobile: false,
30+
fitWindow: false,
31+
};
32+
33+
// Enable events on domains we are interested in.
34+
Promise.all([
35+
Page.enable(),
36+
DOM.enable(),
37+
Network.enable(),
38+
]).then(() => {
39+
Emulation.setDeviceMetricsOverride(deviceMetrics).then(() => {
40+
Emulation.setVisibleSize({width, height}).then(() => {
41+
// Navigate to target page
42+
Page.navigate({url}).then(() => {
43+
});
44+
});
45+
}).catch((e) => reject(e));
46+
}).catch((e) => reject(e));
47+
48+
49+
// Wait for page load event to take screenshot
50+
Page.loadEventFired(() => {
51+
setTimeout(() => {
52+
Page.captureScreenshot({format}).then((screenshot) => {
53+
const buffer = new Buffer(screenshot.data, 'base64');
54+
client.close();
55+
resolve(buffer);
56+
}).catch((e) => reject(e));
57+
}, delay);
58+
});
59+
}).on('error', err => {
60+
reject(err);
61+
});
62+
63+
});
64+
}
65+
66+
const app = express();
67+
68+
app.use(bodyParser.urlencoded({extended: true}));
69+
app.use(bodyParser.json());
70+
app.use(fileUpload());
71+
72+
app.get('/', (req, res) => {
73+
res.type('text/plain').send(`POST to this URI in this format:
74+
{html: "<html></html>", width: 100, height: 100}`);
75+
});
76+
77+
app.post('/', (req, res) => {
78+
const file = req.files.htmlFile;
79+
const width = req.body.width ? parseInt(req.body.width, 10) : undefined;
80+
const height = req.body.height ? parseInt(req.body.height, 10) : undefined;
81+
const delay = req.body.delay ? parseInt(req.body.delay, 10) : undefined;
82+
83+
if (!file) {
84+
return res.status(422).send('No htmlFile sent.');
85+
}
86+
87+
const tmp = tempy.file({extension: 'html'});
88+
89+
file.mv(tmp, (err) => {
90+
if (err) {
91+
res.status(500).send('There was an error.');
92+
throw err;
93+
}
94+
95+
getScreenshot({
96+
width,
97+
height,
98+
delay,
99+
url: 'file://' + tmp
100+
}).then((data) => {
101+
res.status(200).type('image/png').send(data);
102+
}).catch((e) => {
103+
console.log(e);
104+
res.status(500).send('some kind of failure');
105+
});
106+
})
107+
});
108+
109+
app.listen(process.env.NODE_PORT || 8888);

start.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
google-chrome-beta --headless --hide-scrollbars --remote-debugging-port=9222 --disable-gpu --no-sandbox &
3+
npm run start

test/page.html

Lines changed: 208 additions & 0 deletions
Large diffs are not rendered by default.

test/test.html

Lines changed: 208 additions & 0 deletions
Large diffs are not rendered by default.

test/test.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
port=`docker ps |grep chrome-print |sed 's/.*:\([0-9]*\)-.*/\1/'`
3+
echo $port
4+
#curl -d "html=%3Chtml%3E%0A%20%20%3Cbody%3E%0A%20%20%20%20hello%20world%0A%20%20%3C%2Fbody%3E%0A%3C%2Fhtml%3E%0A" -X POST -o test.png http://localhost:$port/
5+
curl -F "[email protected]" -F "width=800" -F "height=1200" -X POST -H "Content-Type: multipart/form-data" -o test.png http://localhost:$port/

0 commit comments

Comments
 (0)