Last active
June 5, 2020 14:31
-
-
Save clakech/ec3dfabcac5cb3e321f4637d27e4d86d to your computer and use it in GitHub Desktop.
Revisions
-
clakech revised this gist
Jun 5, 2020 . 1 changed file with 7 additions and 6 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -12,19 +12,20 @@ const streamImage = async (path, imageId, bucketName, res) => { await getImage(path, bucketName).pipe(res); } const store = (inspectionId, pdf, bucketName) => storage.bucket(bucketName).file(`inspection/${inspectionId}/report.pdf`).save(pdf); const generate = async ({ data }) => { # get the report in json const report = JSON.parse(Buffer.from(data, "base64").toString()); # configure express const app = express(); app.use('/api/inspections/:inspectionId/images/:imageId', ({ params: { inspectionId, imageId } }, res) => streamImage(`inspection/${inspectionId}/${imageId}`, imageId, process.env.IMAGE_BUCKET, res)); app.use('/api/inspections/:inspectionId/report', (req, res) => res.json(report)); app.use(express.static('www')); # find an available port const port = await portfinder.getPortPromise(); return new Promise((resolve) => { const server = app.listen(port, async () => { @@ -33,8 +34,8 @@ const generate = async ({ data }) => { const pdf = await render({ url: `http://localhost:${port}#/report/${id}`, goto: { timeout: 0 }, # I don't remember why I needed this :P emulateScreenMedia: false # use @media print to display the web page }); await store(id, pdf, process.env.REPORT_BUCKET); -
clakech revised this gist
Jun 3, 2020 . 1 changed file with 15 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,15 @@ { "name": "pdf-report-generator", "main": "index.js", "version": "1.°.0", "private": true, "keywords": [], "author": "", "license": "UNLICENSED", "dependencies": { "@google-cloud/storage": "^4.7.0", "express": "^4.17.1", "portfinder": "^1.0.26", "url-to-pdf-api": "git+https://github.com/alvarcarto/url-to-pdf-api.git" } } -
clakech created this gist
Jun 3, 2020 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,53 @@ const express = require('express'); const portfinder = require('portfinder'); const { render } = require('url-to-pdf-api/src/core/render-core.js'); const { Storage } = require("@google-cloud/storage"); const storage = new Storage({}); const getImage = async (path, bucketName) => storage.bucket(bucketName).file(path).createReadStream(); const streamImage = async (path, imageId, bucketName, res) => { res.set('Content-Type', 'image/jpg'); res.set('Content-Disposition', 'attachment;filename=' + imageId); await getImage(path, bucketName).pipe(res); } const store = (inspectionId, pdf, bucketName) => { const pdfFilePath = `inspection/${inspectionId}/report.pdf`; return storage.bucket(bucketName).file(pdfFilePath).save(pdf); }; const generate = async ({ data }) => { const report = JSON.parse(Buffer.from(data, "base64").toString()); const app = express(); app.use('/api/inspections/:inspectionId/images/:imageId', ({ params: { inspectionId, imageId } }, res) => streamImage(`inspection/${inspectionId}/${imageId}`, imageId, process.env.IMAGE_BUCKET, res)); app.use('/api/inspections/:inspectionId/report', (req, res) => res.json(report)); app.use(express.static('www')); const port = await portfinder.getPortPromise(); return new Promise((resolve) => { const server = app.listen(port, async () => { try { const id = report.id; const pdf = await render({ url: `http://localhost:${port}#/report/${id}`, goto: { timeout: 0 }, emulateScreenMedia: false }); await store(id, pdf, process.env.REPORT_BUCKET); resolve(pdf); } catch (e) { console.error(e); throw e; } finally { server.close(); } }); }); }; exports.run = arg => generate(arg).catch(e => console.error(e));