jsreport Node.js Client

1.2.1 · maintenance · verified Tue Apr 21

jsreport-client is a Node.js remote client library designed to interact with a jsreport server, facilitating programmatic report generation and management of jsreport entities. It acts as a wrapper around the jsreport REST API, abstracting HTTP communication to allow developers to render PDF reports, work with templates, and manage data from a Node.js application. The package is currently at version 1.2.1, with its last update in March 2021. Releases are infrequent, suggesting it is in a maintenance phase rather than active feature development. Key differentiators include its promise-based API for rendering and its seamless integration with remote jsreport instances, supporting both on-premise servers and the jsreportonline SaaS offering. It handles request retry mechanisms and error reporting, providing a robust interface for report automation.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to initialize the jsreport client and render a PDF report using a simple Handlebars template and data, saving the output to a file.

import Client from 'jsreport-client';
import fs from 'fs/promises';
import path from 'path';

async function generateReport() {
  // Initialize the jsreport client with the server URL (and optional credentials)
  const client = Client({
    serverUrl: process.env.JSREPORT_SERVER_URL ?? 'http://localhost:5488',
    username: process.env.JSREPORT_USERNAME,
    password: process.env.JSREPORT_PASSWORD,
    // Ensure strictSSL is false for self-signed certificates in development
    strictSSL: process.env.NODE_ENV === 'production' // Only enforce SSL for production
  });

  try {
    // Render a report using a raw template definition and some data
    console.log('Attempting to render report...');
    const response = await client.render({
      template: {
        content: '<h1>Hello {{:name}} from jsreport!</h1><p>Generated at {{new Date().toLocaleString()}}</p>',
        engine: 'handlebars',
        recipe: 'chrome-pdf' // Assuming chrome-pdf recipe is available on the server
      },
      data: {
        name: 'JSReport User'
      }
    });

    // Save the generated report content to a PDF file
    const outputPath = path.join(process.cwd(), 'hello-jsreport-report.pdf');
    await fs.writeFile(outputPath, await response.content());
    console.log(`Report successfully generated and saved to ${outputPath}`);
  } catch (e: any) {
    console.error('Error generating report:', e.message);
    if (e.statusCode) {
      console.error('Status Code:', e.statusCode);
      console.error('Response Body:', e.body);
    }
    process.exit(1);
  }
}

generateReport();

view raw JSON →