Giter Site home page Giter Site logo

casey-williams-rh / pdf-generator Goto Github PK

View Code? Open in Web Editor NEW

This project forked from redhatinsights/pdf-generator

0.0 0.0 0.0 5.2 MB

Backed service to generate PDF reports for CRC

License: Apache License 2.0

Shell 0.94% JavaScript 3.41% TypeScript 95.01% HTML 0.24% Dockerfile 0.36% SCSS 0.04%

pdf-generator's Introduction

crc-pdf-generator

crc-pdf-generator is a NodeJS based service that is responsible for generating a PDF Report.

For local development you can edit the passed data in the src/index.js file. Then to view the changes in real time in the browser just do:

 npm ci
 docker-compose up
 npm run start:server

This will start the react scripts and will serve just the react app on localhost:8000.

This development will not have the header, but the content of the page will be printed identically into the PDF making this approach much faster to make changes to the PDF.

oc port-forward -n crc-pdf-generator svc/crc-pdf-generator-api 8000:8000

Service Integration

Please follow the Integration guide.

Endpoints

You currently have 2 choices for generating any of the available templates:

Production Endpoint

The generate endpoint will automatically download the template if it is available (requires the above-mentioned headers)

 POST http://localhost:8000/api/crc-pdf-generator/v2/generate

The request body:

type RequestBody = {
  service: string; // service name
  template: string; // template name
  [key: string]: any // additional API request config attributes
}

For local development only

The preview endpoint will instead return the pdf preview environment:

 GET http://localhost:8000/preview

While this endpoint is currently only available for local testing, the preview environment will eventually be exposed in production as well.

Endpoint Options

 `?template=x // See service-names for available templates. Required unless you want a hello world.` 
 `?orientation= landscape // optional`

Downloading a report in the browser

To download the report, you will need a small piece of JS code. Here is an example of downloading ROS executive report:

// use fetch or XHR. 
fetch('/api/crc-pdf-generator/v2/create', {
  method: 'POST',
  headers: {
    // do not forget the content type header!
    'Content-Type': 'application/json',
    'x-rh-identity': 'eyJpZGVudGl0eSI6eyJ1c2VyIjp7InVzZXJfaWQiOiIxMiJ9fX0='
  },

  body: JSON.stringify({
      // service and template params are mandatory
      service: 'ros',
      template: 'systemsReport',
      // ... any other config options accepted by your template
    }),
  })
  .then(async (response) => {
    const res = await response.json();
		console.log(res.statusID);
  });

Grab the statusID from the response and add it to the status call

fetch('/api/crc-pdf-generator/v2/download/b5b36108-b03f-4c3d-a1a9-8ccf3a922b20', {
  headers: {
    // do not forget the content type header!
    'Content-Type': 'application/json',
  }
  
  })
  .then(async (response) => {
    if (response.ok === false) {
      const res = await response.json()
      console.log(`PDF failed to generate: ${res.error.description}`)
      throw new Error(`PDF failed to generate: ${res.error.description}`)
    }
    return response.blob()
  })
  .then((blob) => {
    const url = window.URL.createObjectURL(blob);
    const a = document.createElement('a');
    a.href = url;
    // give name to the PDF file
    a.download = 'filename.pdf';
    document.body.appendChild(a); // we need to append the element to the dom -> otherwise it will not work in firefox
    a.click();
    a.remove(); //remove the element
  });

The PDF file will be downloaded in the browser.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.