Spreadsheets in Node.js
The Document Authoring Node.js entry point imports, saves, and exports spreadsheets without a browser. Use it to convert XLSX files to PDF or save uploaded XLSX files as SheetJSON.
Before you start, refer to the Node.js getting started guide for requirements, installation, and TypeScript configuration. Spreadsheet methods use the same system as document methods.
Document Authoring for Node.js is a licensed feature. Contact Sales to enable it for production use.
The examples omit error handling. Add it before you use this code in production.
Convert an XLSX file to PDF
Save this code as convert-spreadsheet.mjs. Place input.xlsx in the same directory:
import { createDocAuthSystem } from '@nutrient-sdk/document-authoring/node';import { readFile, writeFile } from 'node:fs/promises';
const system = await createDocAuthSystem();
try { const spreadsheet = await system.importSpreadsheet(await readFile(new URL('./input.xlsx', import.meta.url)), { format: 'xlsx', });
const pdf = await spreadsheet.export({ format: 'pdf' }); await writeFile(new URL('./output.pdf', import.meta.url), new Uint8Array(pdf));} finally { system.destroy();}Run the script with node convert-spreadsheet.mjs. The PDF includes every visible sheet. To export one sheet, pass its zero-based index in sheet:
const pdf = await spreadsheet.export({ format: 'pdf', sheet: 0 });Store an XLSX file as SheetJSON
Import the XLSX file and save it as SheetJSON. You can then load and export the saved spreadsheet without the original file:
import { createDocAuthSystem } from '@nutrient-sdk/document-authoring/node';import { readFile, writeFile } from 'node:fs/promises';
const system = await createDocAuthSystem();
try { const imported = await system.importSpreadsheet(await readFile(new URL('./input.xlsx', import.meta.url)), { format: 'xlsx', }); const saved = await imported.saveSpreadsheet(); await writeFile(new URL('./stored.sheetjson', import.meta.url), new Uint8Array(await saved.arrayBuffer()));
const reopened = await system.loadSpreadsheet(await readFile(new URL('./stored.sheetjson', import.meta.url))); const xlsx = await reopened.export({ format: 'xlsx' }); await writeFile(new URL('./output.xlsx', import.meta.url), new Uint8Array(xlsx));} finally { system.destroy();}For details about what SheetJSON retains and XLSX export omits, refer to the save and export guide.
The spreadsheet editor isn’t available
The @nutrient-sdk/document-authoring/node entry point creates systems without a user interface. Calling createSpreadsheetEditor() on these systems throws an error.
Learn more
Node.js in production
Scale the system across workers and configure assets on servers.
Import and load
Learn about import options, progress, and cancellation.
Save and export
Learn about SheetJSON, XLSX, and PDF output.