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

pdfmake-to-excel

v1.1.57

Published

A package to convert your pdfmake content definition to an excel file

Downloads

505

Readme

Main Purpose

The purpose of this package was to easily generate an Excel file from the Pdfmake library. Therefore, by providing the payload used to generate PDFs using pdfmake, you should be able to get an Excel file, without malformed cells or incorrectly structured cols and rows.

Here is the documentation to build pdfmake payloads: PDFMake Playground

This library don't require pdfmake.

Here is what this library brings:

  • Merge rows and cols
  • Sheet protection by password
  • Front-end excel download
  • Server-side excel download by streaming

❯ Installation

Install pdfmake-to-excel with npm

  npm install pdfmake-to-excel

❯ Usage/Examples

Import the ExcelConverter class from pdfmake-to-excel, instanciate it with the name of your excel file and your pdfmake table content definition object, then call the downloadExcel() method.

Pass the following arguments to the constructor

  • The Excel filename
  • The PDFMake table content definition object
  • An optional configuration object including
    • A sheet protection password
    • A default Options Excel configuration
import {ExcelConverter} from 'pdfmake-to-excel';

function downloadFile() {
    const exporter = new ExcelConverter(
        'Export test',
        contentDefinition,
        {
            protection: 'p@ssw0rd',
            defaultOptions: {defaultColWidth: 20}
        }
    );
    exporter.downloadExcel();
}

❯ Content Definition Object Format

Here is the documentation to build pdfmake payloads: PDFMake Playground. Here is how you should format your table content definition object

{
  "title": "Title displayed on your Excel file", //OPTIONAL
  "logo": "base64 of your image here" //OPTIONAL
  "data": [
    [ // LINE 01
      {
        "text": "Cell 01",  // CELL 01 spanned accross 2 rows
        "rowSpan": 2
      },
      {
        "text": "Cell 02", // CELL 02 Spanned accross 2 cells
        "colSpan": 2
      },
      {
        "text": ""
      }
    ],
    [ // Empty line from the first line rowSpan
      {
        "text": "" // Empty cell from the first line rowSpan
      }
    ]
  ]
}

❯ Multiple Sheets

To generate an Excel file with multiple sheets and a table on each sheet, all you have to do is to provide the ExcelConverter Class with a content definition object where the data attribute is an array of sheets, each sheets being an object with the name (sheetName property) and the table content definition's "data" (See content definition object format) property (sheetData property)

const exporter = new ExcelConverter(
    'File_name',
    {
        data: [
            {sheetName: 'Sheet_name 01', sheetData: contentDefinitionData1},
            {sheetName: 'Sheet_name 02', sheetData: contentDefinitionData2},
            {sheetName: 'Sheet_name 03', sheetData: contentDefinitionData3},
        ]
    }
);

❯ Streaming

In case you want to export your Excel file server-side, pdfmake-to-excel provides the getStream() method which takes in an optionnal argument which is your response as the first argument.

  • When the response argument is provided, the excel file is created and directly piped to your response.
  • When the response argument is not provided, you'll get the stream itself. Up to you to pipe it wherever you want

❯ Example Using NestJS

@Get('/export-excel-file')
async exportReportExcel(@Res() response:Response):Promise < any > {
    const exporter = new ExcelConverter('FileTest', contentDefinition);

    // Automatic pipe to response
    await exporter.getStream(response);


    // Get the stream
    const stream = await exporter.getStream();

    // Then pipe it if you want
    stream.pipe(response);
}

❯ Example Using AdonisJS

const ExportService = use('App/Services/ExportService');

const {ExcelConverter} = require('pdfmake-to-excel')

class ExportController{
  async exportAction({request, response}){

    const executor = ExportService(request.all());
    const excelConverter = new ExcelConverter('test-filename.png', await executor.getData());

    response.implicitEnd = false;
    response.header('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
    response.header('Content-Transfer-Encoding', 'binary');
    response.header('Content-Disposition', `attachment; filename="fichier.xlsx"`)

    excelConverter.getStream().pipe(response.response);
  }
}
module.exports = ExportController;