jsRTF
An RTF document creation library for javascript.
Based on Jonathan Rowny's node-rtf.
Features
TODO
Installation
npm install --save jsrtf
Usage
Using CommonJS:
var jsRTF = ;
Using YModules:
// require('jsrtf'); // if required modules;
Examples
Simple
// Create RTF object var myDoc = ; // Formatter object var textFormat = spaceBefore : 300 spaceAfter : 300 paragraph : true ; // Adding text styled with formatter myDoc; // Make content... var content = myDoc;
A little more complex sample
// Extending color table Object; var // Default page margin size (twips) pageMargin = 3000 // Page options pageOptions = // Language: Russian language : jsRTFLanguageRU // Set page size: A4 horizontal pageWidth : jsRTFUtils pageHeight : jsRTFUtils // Landscape page format -- which effect it making? landscape : true // Margins: marginLeft : pageMargin marginTop : pageMargin marginBottom : pageMargin marginRight : pageMargin // Calculate content width (for 100% tables, for example) contentWidth = pageOptionspageWidth - pageOptionsmarginLeft - pageOptionsmarginRight // Create RTF object myDoc = pageOptions ; // Formatter object var defaultFontSize = 12 titleStyle = spaceBefore : 500 spaceAfter : 500 paragraph : true align : 'center' fontSize : 30 color : jsRTFColorsORANGE border : type : 'single' width : 10 color : jsRTFColorsRED // borderColor : jsRTF.Colors.RED, borderTop : type : 'double' width : 50 spacing : 100 color : jsRTFColorsGREEN emphasisStyle = color : jsRTFColorsdarkGreen // Custom color added above textStyle = spaceBefore : 300 spaceAfter : 300 paragraph : true fontSize : defaultFontSize color : jsRTFColorsBLACK ; // Adding text styled with formatter myDoc; // Adding complex element with inline and default stylings myDoc; // Add table var cellPadding = 100 cellBaseProps = spaceBefore : cellPadding spaceAfter : cellPadding leftIndent : cellPadding rightIndent : cellPadding table = format : // tableBorder : 10, tableWidth : contentWidth rowFormat : Object firstRowFormat : Object cellFormat : cellBorderRight : type : 'single' width : 10 color : jsRTFColorsBLACK cellBorderTop : type : 'single' width : 10 color : jsRTFColorsBLACK cellBorderLeft : type : 'single' width : 10 color : jsRTFColorsBLACK cellBorderBottom : type : 'single' width : 10 color : jsRTFColorsBLACK cellFormats : widthRatio : 02 strike : true bold : true color : jsRTFColorsGREEN widthPercents : 80 underline : true color : jsRTFColorsMAROON ; // Add rows table; table; myDoc; myDoc; // Make content var content = myDoc;
See section below for tips about document saving.
See more examples in demo folder.
Saving & encoding documents
When you save a document it is necessary to keep in mind that the rtf documents to use 8-bit encoding, while JavaScript uses Unicode. Thus, before saving you need to convert your data to binary buffer (if it contains some non-latin1 characters, see below for decoding from unicode samples):
var data = myDoc; var buffer = data 'binary'; fs; fs;
Moreover, it is necessary to properly convert the encoding. For example, using iconv:
var Iconv = Iconv; var conv = 'utf8' 'windows-1251'; data = conv;
...or iconv-lite:
var iconvLite = ; data = iconvLite;
Or write your own encode function like this (for default windows' cyrillic windows-1251):
{ var tgtStr = '' c = 0; for var i = 0; i < srcStrlength; i++ c = srcStr; if c > 127 if c > 1024 if c === 1025 c = 1016; else if c === 1105 c = 1032; c -= 848; // c = c % 256; // ??? tgtStr += String; return tgtStr; } data = ;
See also possible codepage-related rtf entities:
\ansicpgN
: N = codepage number (eg 1251 for samples above). Not used now but may be implemented.
To save documents directly from the browser you can use FileSaver or StreamSaver.