Last active
May 9, 2020 12:46
-
-
Save aherok/82fcfefe75f756e2bb458399dd2664af to your computer and use it in GitHub Desktop.
Puppetteer screenshooter PoC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://github.com/buildkite/docker-puppeteer/blob/master/Dockerfile | |
FROM node:12.16.3-slim | |
RUN apt-get update \ | |
&& apt-get install -y wget gnupg ca-certificates \ | |
&& wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \ | |
&& sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' \ | |
&& apt-get update \ | |
# Install Chrome to get all the dependencies just to keep deps up to date | |
&& apt-get install -y google-chrome-stable \ | |
&& rm -rf /var/lib/apt/lists/* \ | |
&& wget --quiet https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh -O /usr/sbin/wait-for-it.sh \ | |
&& chmod +x /usr/sbin/wait-for-it.sh | |
RUN mkdir -p /app | |
WORKDIR /app | |
ADD package.json package-lock.json index.js /app/ | |
RUN npm install | |
EXPOSE 3000 | |
CMD ["npm", "start"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const puppeteer = require('puppeteer'); | |
const express = require('express'); | |
const app = express(); | |
console.log("Running screenshooter..."); | |
app.get('/', async (req, resp) => { | |
const url = req.query.url; | |
if (!url) { | |
resp.status(400).send('Missing URL'); | |
return; | |
} | |
console.debug(`Printing PDF from URL: ${url}`); | |
try { | |
const pdf = await printPDF(url); | |
console.debug(`Got PDF, size: ${pdf.length} bytes`) | |
resp.set('Content-disposition', 'attachment; filename=screenshot.pdf') | |
.status(200) | |
.end(pdf); | |
} catch (e) { | |
resp.status(500).send(`<pre><code>${e.message}</code></pre>`); | |
} | |
}); | |
app.listen(3000, '0.0.0.0'); | |
async function printPDF(url) { | |
const browser = await puppeteer.launch({ | |
headless: true, | |
args: ['--disable-dev-shm-usage', '--no-sandbox'] | |
}); | |
const page = await browser.newPage(); | |
await page.goto(url, {waitUntil: 'networkidle2'}); | |
const pdf = await page.pdf({ | |
scale: 0.75, | |
format: 'A4', | |
printBackground: true, | |
margin: { | |
top: "1cm", | |
right: "1cm", | |
bottom: "1cm", | |
left: "1cm", | |
} | |
}); | |
await browser.close(); | |
return pdf | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "screenshooter", | |
"version": "1.0.0", | |
"dependencies": { | |
"express": "^4.17.1", | |
"nodemon": "^2.0.3", | |
"puppeteer": "3.0.4" | |
}, | |
"description": "A PDF website screenshot service", | |
"main": "index.js", | |
"scripts": { | |
"start": "node_modules/.bin/nodemon index.js" | |
}, | |
"author": "", | |
"license": "ISC", | |
"repository": "" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment