Created
August 19, 2020 08:43
-
-
Save matisiekpl/6beaf164d26c1b724e58795b630c93b8 to your computer and use it in GitHub Desktop.
Pdf Generator
This file contains hidden or 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
FROM node:11 | |
RUN echo 'kernel.unprivileged_userns_clone=1' > /etc/sysctl.d/userns.conf | |
# Install headless Chrome | |
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 \ | |
# We install Chrome to get all the OS level dependencies, but Chrome itself | |
# is not actually used as it's packaged in the node puppeteer library. | |
# Alternatively, we could could include the entire dep list ourselves | |
# (https://github.com/puppeteer/puppeteer/blob/master/docs/troubleshooting.md#chrome-headless-doesnt-launch-on-unix) | |
# but that seems too easy to get out of 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 | |
WORKDIR /app | |
# Install application | |
ADD package.json package.json | |
RUN yarn | |
ADD htmlToPdf.js htmlToPdf.js | |
EXPOSE 3000 3000 | |
CMD node htmlToPdf.js |
This file contains hidden or 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 express = require('express'); | |
const consola = require('consola'); | |
const puppeteer = require('puppeteer'); | |
let counter = 0; | |
let errors = 0; | |
async function main() { | |
const browser = await puppeteer.launch({ | |
headless: true, | |
args: [ | |
'--no-sandbox', | |
'disable-setuid-sandbox', | |
] | |
}); | |
const app = express(); | |
const port = process.env.PORT || 3000; | |
app.get('/health', (req, res) => { | |
res.json({ | |
size: counter, | |
errors, | |
}); | |
}) | |
app.get('/pdf/:url(*)', async (req, res) => { | |
try { | |
consola.info(`Processing site: ${req.params.url}`); | |
const page = await browser.newPage(); | |
await page.goto(req.params.url, { waitUntil: 'load', timeout: 0 }); | |
const pdf = await page.pdf({ | |
width: 1920, | |
height: 930, | |
printBackground: true | |
}); | |
counter++; | |
res.send(pdf); | |
} catch (err) { | |
consola.error(err); | |
errors++; | |
res.status(500).json({ | |
status: 'error' | |
}) | |
} | |
}); | |
await app.listen(port); | |
consola.success(`Server listening on port: ${port}`); | |
} | |
main(); |
This file contains hidden or 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": "lanyards", | |
"version": "1.0.0", | |
"main": "index.js", | |
"license": "MIT", | |
"dependencies": { | |
"consola": "^2.15.0", | |
"express": "^4.17.1", | |
"nodemon": "^2.0.4", | |
"puppeteer": "^5.2.1" | |
}, | |
"scripts": { | |
"start": "node htmlToPdf.js", | |
"dev": "nodemon htmlToPdf.js" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment