Skip to content

Instantly share code, notes, and snippets.

@clakech
Last active June 5, 2020 14:31
Show Gist options
  • Select an option

  • Save clakech/ec3dfabcac5cb3e321f4637d27e4d86d to your computer and use it in GitHub Desktop.

Select an option

Save clakech/ec3dfabcac5cb3e321f4637d27e4d86d to your computer and use it in GitHub Desktop.

Revisions

  1. clakech revised this gist Jun 5, 2020. 1 changed file with 7 additions and 6 deletions.
    13 changes: 7 additions & 6 deletions index.js
    Original 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) => {
    const pdfFilePath = `inspection/${inspectionId}/report.pdf`;
    return storage.bucket(bucketName).file(pdfFilePath).save(pdf);
    };
    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 },
    emulateScreenMedia: false
    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);
  2. clakech revised this gist Jun 3, 2020. 1 changed file with 15 additions and 0 deletions.
    15 changes: 15 additions & 0 deletions package.json
    Original 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"
    }
    }
  3. clakech created this gist Jun 3, 2020.
    53 changes: 53 additions & 0 deletions index.js
    Original 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));