@zola_do/docx
v0.1.10
Published
DOCX template processing for NestJS
Readme
@zola_do/docx
DOCX template processing for NestJS using docx-templates.
Installation
# Install individually
npm install @zola_do/docx
# Or via meta package
npm install @zola_do/nestjs-sharedUsage
Module Setup
import { Module } from '@nestjs/common';
import { DocxModule } from '@zola_do/docx';
@Module({
imports: [DocxModule],
})
export class AppModule {}Generating Documents
Fill a DOCX template with data. Use {placeholder} syntax in your Word document:
import { Injectable } from '@nestjs/common';
import { DocxService } from '@zola_do/docx';
@Injectable()
export class ReportService {
constructor(private readonly docxService: DocxService) {}
async generateReport(templateBuffer: Buffer, data: Record<string, any>) {
// Template contains {name}, {date}, etc.
const result = await this.docxService.generateDocx(templateBuffer, data);
return result; // Buffer of filled DOCX
}
}Validating Templates
Ensure a template has all required placeholders before generating:
const missingProps = await this.docxService.validateDocument(
templateBuffer,
['name', 'date', 'amount'],
);
if (missingProps.length > 0) {
throw new Error(`Missing placeholders: ${missingProps.join(', ')}`);
}Template Syntax
Uses docx-templates syntax. Placeholders use {variableName}. Supports loops, conditionals, and images—see the docx-templates documentation.
Exports
DocxModule— Register the DOCX moduleDocxService—generateDocx,validateDocument
Related Packages
- @zola_do/document-manipulator — PDF/DOCX merge and conversion, uses similar template population
- @zola_do/minio — Store generated documents
