npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@researchdatabox/sails-hook-redbox-pdfgen

v1.3.1

Published

Adds PDF generation functionality for records to ReDBox

Downloads

156

Readme

ReDBox Portal Record PDF Generation

An installable hook for the ReDBox Portal to add PDF generation functionality for records using Puppeteer.

Installation

There are a few steps to install the pieces needed for generating PDF files.

Install package

In your redbox portal root folder run the command:

npm i @researchdatabox/sails-hook-redbox-pdfgen

Install browser that puppeteer will control

Modify your Dockerfile to install the puppeteer dependencies and install the browser that puppeteer will control.

The dependency of this package puppeteer will download known working versions of Chrome for Testing and chrome-headless-shell.

Add to your Dockerfile:

# As the 'root' user.
# USER root

# Install dependencies for Chrome for Testing.
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 | gpg --dearmor -o /usr/share/keyrings/googlechrome-linux-keyring.gpg \
    && sh -c 'echo "deb [arch=amd64 signed-by=/usr/share/keyrings/googlechrome-linux-keyring.gpg] https://dl-ssl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' \
    && apt-get update \
    && apt-get install -y --no-install-recommends google-chrome-stable fonts-freefont-ttf libxss1 \
    && rm -rf /var/lib/apt/lists/* \
    && mkdir -p /opt/browsers \
    && chown node:node /opt/browsers

# As the 'node' user.
# USER node

# Copy the source files.
# COPY --chown=node:node . <source-path-in-container>

# Set the download path to a place that is stored in the container.
ENV PUPPETEER_CACHE_DIR=/opt/browsers
RUN cd <source-path-in-container> \
    && npm install

Configuration

The service is designed to run using the record post-save trigger functionality. It has the following options:

waitForSelector

Required: yes

A css selector that puppeteer will wait for before generating the PDF. Usually this is when the angular app has finished initialising and the selector "div#loading.hidden" will be satisfactory. If you have components that make AJAX calls after initialisation that you need to wait on then you may need to use a different selector.

pdf-prefix

Required: yes

The prefix of the filename that will be generated. PDFs will be stored as datastreams (attachments) to the record with this parameter as a prefix followed by an ISO8601 datestring. e.g. <type>-pdf-201901021000.pdf

token

Required: yes

An API access token that puppeteer will use to access the record. This needs to be generated for a user on the User Management page of the system. The user must also have appropriate roles set so that it has appropriate permissions to view the record.

sourceUrlBase

Required: no

Default: /default/rdmp/record/view

Set the base source url.

PDFOptions

Required: no

Default: {format: 'A4', printBackground: true}

Change the pdf attributes.

Example configuration

Inject the API token via the hook's index.js.

In index.js:

module.exports = function (sails) {
  return {
    initialize: function (cb) {
      if (!_.isUndefined(sails.config.auth.default.local.default.token) && !_.isEmpty(sails.config.auth.default.local.default.token)) {
        const enabledTypes = ["<type>"];
        for (let enabledType of enabledTypes) {
          sails.log.verbose(`PDFService::Adding token for recordtype ${enabledType}`)
          sails.config.recordtype[enabledType].hooks.onCreate.post[0].options.triggerConfiguration.options.token = sails.config.auth.default.local.default.token;
          sails.config.recordtype[enabledType].hooks.onUpdate.post[0].options.triggerConfiguration.options.token = sails.config.auth.default.local.default.token;
        }
      }
    }
  };
};

Set up the pdf generation task.

In config/agendaQueue.js:

module.exports.agendaQueue = {
    jobs: [
        {
            name: 'PDFService-CreatePDF',
            fnName: 'rdmpservice.queuedTriggerSubscriptionHandler',
            options: {
                lockLifetime: 120 * 1000, // 120 seconds max runtime
                lockLimit: 1,
                concurrency: 1
            }
        }
    ]
};

Configure when to run the pdf generation.

In config/<type>-recordtype.js

module.exports.recordtype = {
  '<type>': {
    "packageType": "<type>",
    hooks: {
      onCreate: {
        post: [
          {
            function: 'sails.services.rdmpservice.queueTriggerCall',
            options: {
              jobName: 'PDFService-CreatePDF',
              triggerConfiguration: {
                function: 'sails.services.pdfservice.createPDF',
                options: {
                  waitForSelector: 'div#loading.hidden',
                  pdfPrefix: '<type>-pdf',
                  token: ''
                }
              }
            }
          },
        ]
      },
      onUpdate: {
        post: [
          {
            function: 'sails.services.rdmpservice.queueTriggerCall',
            options: {
              jobName: 'PDFService-CreatePDF',
              triggerConfiguration: {
                function: 'sails.services.pdfservice.createPDF',
                options: {
                  waitForSelector: 'div#loading.hidden',
                  pdfPrefix: '<type>-pdf',
                  token: ''
                }
              }
            }
          }
        ]
      }
    }
  }
}

Development

# This will build the npm package for `sails-hook-redbox-pdfgen` and install it into the `rbportal`.
# To start a redbox instance for local development:
./runForDev.sh

# After making code changes:
npm run dev:docker:clean


# To run tests, then clean up the test docker compose resources:
npm install
npm run dev:host

npm run test:bruno:docker
npm run test:bruno:docker:clean

# npm run test:mocha:docker
# npm run test:mocha:docker:clean

npm run test:docker:clean


# To remove the database contents and all generated and cached files:
npm run dev:host:clean