Skip to main content

DocumentRenderV2FromBase64Command

Overview

The DocumentRenderV2FromBase64Command generates professional Word documents (DOCX) from a Base64-encoded template with dynamic data binding. Unlike DocumentRenderV2Command which fetches templates from the database by name, this command accepts the template as a Base64 string directly.

Use this command when:

  • ✅ Template is uploaded by users at runtime
  • ✅ Template comes from external API or file upload
  • ✅ Template is dynamically generated or modified
  • ✅ You want to bypass database storage for templates
  • ✅ Testing templates without saving to database first

Key Differences from DocumentRenderV2Command

FeatureDocumentRenderV2CommandDocumentRenderV2FromBase64Command
Template SourceDatabase (by documentName)Base64 string (documentBase64)
Storage RequiredYes - template must be in DBNo - template provided inline
Use CaseProduction templatesDynamic/uploaded templates
PerformanceFaster (cached in DB)Slightly slower (decode Base64)

Syntax

var result = doCmd('DocumentRenderV2FromBase64Command', {
Data: {
documentBase64: '<Base64-encoded-Word-document>',
context: JSON.stringify(dataObject),
isPdfOutput: false // Optional: true for PDF output
}
});

Parameters

ParameterTypeRequiredDescription
documentBase64string✅ YesBase64-encoded Word document template (.docx)
contextstring✅ YesStringified JSON object with template data
isPdfOutputboolean❌ No⚠️ Not yet implemented - Currently returns DOCX regardless of this setting (default: false)

Return Value

{
"isSuccessful": true,
"message": "Document rendered successfully from Base64 input.",
"data": "<Base64-encoded-rendered-document>"
}

Note: The data field contains the rendered document as Base64. Decode it to get the file bytes.


Template Syntax

Same as DocumentRenderV2Command - uses Handlebars-style placeholders:

Variables

{{customerName}}
{{accountNumber}}
{{loanAmount}}

Loops (for arrays/lists)

{{#transactions}}
Date: {{date}}, Amount: {{amount}}
{{/transactions}}

Conditionals (for truthy values)

{{#approved}}
Your loan is approved!
{{/approved}}

Common Use Cases

1. User-Uploaded Template

// User uploads a Word template file
var uploadedFileBase64 = context.uploadedTemplate; // From file upload

var result = doCmd('DocumentRenderV2FromBase64Command', {
Data: {
documentBase64: uploadedFileBase64,
context: JSON.stringify({
customerName: context.customerName,
loanAmount: context.loanAmount,
approvedDate: new Date().toISOString().split('T')[0]
})
}
});

// Save the rendered document
context.renderedDocumentBase64 = result.data;

2. External API Template

// Fetch template from external service
var apiResult = doCmd('ExecuteRestApiCommand', {
Data: {
url: 'https://template-service.com/api/templates/loan-agreement',
method: 'GET',
headers: {
'Authorization': 'Bearer ' + context.apiToken
}
}
});

// The API returns the template as Base64
var templateBase64 = JSON.parse(apiResult.data).templateBase64;

// Render with context
var rendered = doCmd('DocumentRenderV2FromBase64Command', {
Data: {
documentBase64: templateBase64,
context: JSON.stringify({
customerName: context.customerName,
loanAmount: context.loanAmount,
term: context.loanTerm
})
}
});

context.finalDocument = rendered.data;

3. Dynamic Template Modification

// User selects a base template and customizes it
var baseTemplateBase64 = context.selectedTemplateBase64;

// Render with dynamic context
var result = doCmd('DocumentRenderV2FromBase64Command', {
Data: {
documentBase64: baseTemplateBase64,
context: JSON.stringify({
customerName: context.customerName,
customField1: context.userInput1,
customField2: context.userInput2,
transactions: context.transactionList
})
}
});

// Email the document
doCmd('SendMailCommand', {
Data: {
email: [context.customerEmail],
subject: 'Your Custom Document',
message: 'Please find your document attached.',
attachments: [{
fileName: 'CustomDocument.docx',
content: result.data,
contentType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
}]
}
});

4. PDF Output from Base64 Template

⚠️ Note: PDF conversion is not yet implemented. Setting isPdfOutput: true will return a DOCX file with a warning message.

var result = doCmd('DocumentRenderV2FromBase64Command', {
Data: {
documentBase64: context.uploadedTemplateBase64,
context: JSON.stringify({
invoiceNumber: context.invoiceNumber,
customerName: context.customerName,
items: context.invoiceItems,
totalAmount: context.totalAmount
}),
isPdfOutput: true // Currently not implemented - will return DOCX
}
});

// For now, result.data contains Base64-encoded DOCX (not PDF)
// Check result.message for implementation status
context.invoiceDocument = result.data;

5. Testing Template Before Saving

// Test a new template without saving to database first
var testTemplateBase64 = context.newTemplateBase64;

var testResult = doCmd('DocumentRenderV2FromBase64Command', {
Data: {
documentBase64: testTemplateBase64,
context: JSON.stringify({
testCustomer: 'John Doe',
testAmount: 50000,
testDate: '2026-01-17'
})
}
});

if (testResult.isSuccessful) {
// Template works! Save to database
doCmd('CreateDocumentTemplateDefinitiionCommand', {
Data: {
name: 'NewLoanTemplate',
content: testTemplateBase64,
description: 'Tested and working'
}
});
} else {
throw new Error('Template test failed: ' + testResult.message);
}

How to Get Base64 from File

JavaScript (Browser - File Upload)

// User uploads file
const fileInput = document.getElementById('templateFile');
const file = fileInput.files[0];

const reader = new FileReader();
reader.onload = function(e) {
const base64 = e.target.result.split(',')[1]; // Remove "data:..." prefix

// Now use base64 in your command
const result = doCmd('DocumentRenderV2FromBase64Command', {
Data: {
documentBase64: base64,
context: JSON.stringify({ name: 'John' })
}
});
};
reader.readAsDataURL(file);

C# (Server-Side)

// Read file and convert to Base64
byte[] fileBytes = File.ReadAllBytes("template.docx");
string base64 = Convert.ToBase64String(fileBytes);

// Use in BPM workflow context
context.templateBase64 = base64;

PowerShell

# Convert Word file to Base64
$bytes = [System.IO.File]::ReadAllBytes("C:\Templates\template.docx")
$base64 = [Convert]::ToBase64String($bytes)

# Output or use in API call
$base64 | Out-File "template.base64.txt"

Error Handling

Invalid Base64 String

{
"isSuccessful": false,
"message": "Invalid Base64 string for documentBase64.",
"data": {
"error": "The input is not a valid Base-64 string..."
}
}

Missing Parameter

{
"isSuccessful": false,
"message": "Error rendering document from Base64: Missing required property 'documentBase64'.",
"data": {
"message": "Missing required property 'documentBase64'.",
"stackTrace": "..."
}
}

Corrupted Document

{
"isSuccessful": false,
"message": "Error rendering document from Base64: File contains corrupted data.",
"data": {
"message": "File contains corrupted data.",
"stackTrace": "..."
}
}

Best Practices

✅ Do's

  1. Validate Base64 Before Processing

    // Check if valid Base64
    try {
    atob(context.templateBase64); // Browser
    // Or server-side validation
    } catch (e) {
    throw new Error('Invalid Base64 template');
    }
  2. Limit Template Size

    // Check size before processing (Base64 is ~33% larger than binary)
    var maxSizeBytes = 5 * 1024 * 1024; // 5 MB
    var estimatedSize = (context.templateBase64.length * 3) / 4;

    if (estimatedSize > maxSizeBytes) {
    throw new Error('Template too large (max 5 MB)');
    }
  3. Cache Frequently Used Templates

    // For templates used often, save to database first
    if (context.useFrequently) {
    doCmd('CreateDocumentTemplateDefinitiionCommand', {
    Data: {
    name: 'FrequentTemplate',
    content: context.templateBase64
    }
    });

    // Then use DocumentRenderV2Command (faster)
    var result = doCmd('DocumentRenderV2Command', {
    Data: {
    documentName: 'FrequentTemplate',
    context: JSON.stringify(data)
    }
    });
    }
  4. Store Context Separately

    // Don't embed large data in template
    // ❌ Bad: {{largeDataArray}}
    // ✅ Good: Pass as context
    context: JSON.stringify({
    summary: 'Total transactions: ' + context.transactions.length,
    transactions: context.transactions.slice(0, 100) // Limit size
    })

❌ Don'ts

  1. Don't Store Base64 in Context Long-Term

    • Base64 is 33% larger than binary
    • Use database storage for permanent templates
  2. Don't Skip Validation

    • Always validate Base64 format
    • Check file size limits
    • Verify it's actually a Word document
  3. Don't Process Untrusted Templates Without Validation

    • Scan for malicious macros
    • Validate document structure
    • Use in sandboxed environment

Performance Considerations

Template SizeProcessing TimeRecommendation
< 100 KB< 1 second✅ Ideal
100 KB - 1 MB1-3 seconds✅ Good
1 MB - 5 MB3-10 seconds⚠️ Consider optimization
> 5 MB> 10 seconds❌ Too large - optimize template

Optimization Tips:

  • Remove embedded images (use placeholders)
  • Compress images before embedding
  • Remove unused styles and formatting
  • Use simpler table structures

Comparison Table

ScenarioUse DocumentRenderV2CommandUse DocumentRenderV2FromBase64Command
Pre-designed templates
User uploads template
External API template
Testing new templates
High performance needed
Template versioning
Dynamic template generation
Frequent reuse


Migration from DocumentRenderV2Command

Before (Database Template):

var result = doCmd('DocumentRenderV2Command', {
Data: {
documentName: 'LoanAgreement',
context: JSON.stringify(data)
}
});

After (Base64 Template):

// Get template as Base64 (from upload, API, etc.)
var templateBase64 = context.uploadedTemplateBase64;

var result = doCmd('DocumentRenderV2FromBase64Command', {
Data: {
documentBase64: templateBase64,
context: JSON.stringify(data)
}
});

Testing

Test with Sample Template

// Create a simple test template (in Base64)
var simpleTemplateBase64 = 'UEsDBBQABgAIAAAAIQ...'; // Your Base64 here

var testResult = doCmd('DocumentRenderV2FromBase64Command', {
Data: {
documentBase64: simpleTemplateBase64,
context: JSON.stringify({
name: 'Test User',
date: new Date().toISOString(),
amount: 12345
})
}
});

if (testResult.isSuccessful) {
console.log('Template rendering works!');
console.log('Output size:', testResult.data.length, 'chars');
} else {
console.error('Template test failed:', testResult.message);
}

Known Limitations

⚠️ Current Limitations:

  1. PDF Conversion Not Implemented

    • Setting isPdfOutput: true currently returns DOCX format
    • A warning message is logged and returned in the response
    • PDF conversion requires additional infrastructure (LibreOffice, Gotenberg, etc.)
    • Workaround: Convert DOCX to PDF on the client side or use a separate service
  2. File Size Considerations

    • Base64 encoding increases size by ~33%
    • Large templates (>5 MB) may cause performance issues
    • Recommend keeping templates under 2 MB for best performance

Security Considerations

⚠️ Important Security Notes:

  1. Validate User-Uploaded Templates

    • Scan for macros and embedded code
    • Limit file size (max 5-10 MB)
    • Validate document structure
  2. Sanitize Context Data

    • Escape special characters in user input
    • Validate data types
    • Limit array sizes in context
  3. Use Sandboxing

    • Process templates in isolated environment
    • Limit processing time (timeout after 30s)
    • Monitor memory usage
  4. Audit Logging

    • Log all template processing requests
    • Track source of Base64 templates
    • Monitor for suspicious patterns

Version History

  • v1.0 (January 2026) - Initial release
    • Base64 template support
    • Same rendering engine as DocumentRenderV2Command
    • PDF output support

See Also