GithubHelp home page GithubHelp logo

jsrtf's Introduction

GitHub Release Build Status npm version

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 = require('jsrtf');

Using YModules:

    // require('jsrtf'); // if required
    modules.require(['jsrtf'], (jsRTF) => {
        // ...
    });

Examples

Simple

    // Create RTF object
    var myDoc = new jsRTF();

    // Formatter object
    var textFormat = new jsRTF.Format({
        spaceBefore : 300,
        spaceAfter : 300,
        paragraph : true,
    });

    // Adding text styled with formatter
    myDoc.writeText('Demo text.', textFormat);

    // Make content...
    var content = myDoc.createDocument();

A little more complex sample

    // Extending color table
    Object.assign(jsRTF.Colors, {
        darkGreen : new jsRTF.RGB(0,64,0),
    });

    var
        // Default page margin size (twips)
        pageMargin = 3000,
        // Page options
        pageOptions = {
            // Language: Russian
            language : jsRTF.Language.RU,
            // Set page size: A4 horizontal
            pageWidth : jsRTF.Utils.mm2twips(297),
            pageHeight : jsRTF.Utils.mm2twips(210),
            // 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 = pageOptions.pageWidth - pageOptions.marginLeft - pageOptions.marginRight,
        // Create RTF object
        myDoc = new jsRTF(pageOptions)
    ;

    // Formatter object
    var
        defaultFontSize = 12,
        titleStyle = new jsRTF.Format({
            spaceBefore : 500,
            spaceAfter : 500,
            paragraph : true,
            align : 'center',
            fontSize : 30,
            color : jsRTF.Colors.ORANGE,
            border : { type : 'single', width : 10, color : jsRTF.Colors.RED },
            // borderColor : jsRTF.Colors.RED,
            borderTop : { type : 'double', width : 50, spacing : 100, color : jsRTF.Colors.GREEN },
        }),
        emphasisStyle = new jsRTF.Format({
            color : jsRTF.Colors.darkGreen, // Custom color added above
        }),
        textStyle = new jsRTF.Format({
            spaceBefore : 300,
            spaceAfter : 300,
            paragraph : true,
            fontSize : defaultFontSize,
            color : jsRTF.Colors.BLACK,
        })
    ;

    // Adding text styled with formatter
    myDoc.writeText('Title', titleStyle);

    // Adding complex element with inline and default stylings
    myDoc.addElement([
        new jsRTF.ContainerElement([
            new jsRTF.TextElement('Striked ', emphasisStyle),
            'content and',
        ], { strike : true }),
        new jsRTF.TextElement(' display `textStyle` variable: ', emphasisStyle),
        new jsRTF.TextElement(JSON.stringify(textStyle)), // TODO: Code coloring plugin?
    ], {
        paragraph : true,
        spaceBefore : 500,
        spaceAfter : 500,
    });

    // Add table
    var cellPadding = 100,
        cellBaseProps = {
            spaceBefore : cellPadding,
            spaceAfter : cellPadding,
            leftIndent : cellPadding,
            rightIndent : cellPadding,
        },
        table = new jsRTF.TableElement({
            format : new jsRTF.Format({
                // tableBorder : 10,
                tableWidth : contentWidth,
            }),
            rowFormat : new jsRTF.Format(Object.assign({}, cellBaseProps, {
                // rowBorderTop : { type : 'single', width : 10, color : jsRTF.Colors.GREEN }, // ???
                // strike : true,
                // color : jsRTF.Colors.GRAY,
            })),
            firstRowFormat : new jsRTF.Format(Object.assign({}, cellBaseProps, {
                cellVerticalAlign : 'bottom',
                tableHeader : true,
                bold : false,
                color : jsRTF.Colors.WHITE,
                cellBgColor : jsRTF.Colors.RED,
            })),
            cellFormat : new jsRTF.Format({
                cellBorderRight : { type : 'single', width : 10, color : jsRTF.Colors.BLACK },
                cellBorderTop : { type : 'single', width : 10, color : jsRTF.Colors.BLACK },
                cellBorderLeft : { type : 'single', width : 10, color : jsRTF.Colors.BLACK },
                cellBorderBottom : { type : 'single', width : 10, color : jsRTF.Colors.BLACK },
            }),
            cellFormats : [
                new jsRTF.Format({ widthRatio : 0.2, strike : true, bold : true, color : jsRTF.Colors.GREEN }),
                new jsRTF.Format({ widthPercents : 80, underline : true, color : jsRTF.Colors.MAROON }),
            ],
        })
    ;
    // Add rows
    table.addRow([ 'Table row', 'with two\ncolumns' ]);
    table.addRow([ 'Second row', 'and the second column' ]);
    myDoc.addTable(table);

    myDoc.writeText('Demo text.', textStyle);

    // Make content
    var content = myDoc.createDocument();

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.createDocument();
    var buffer = new Buffer(data, 'binary');
    fs.ensureDirSync(path.dirname(resultFile));
    fs.writeFile(resultFile, buffer, function (error) {
        if ( !error ) {
            console.info('Created file', resultFile);
        }
        else {
            console.error(error);
        }
    });

Moreover, it is necessary to properly convert the encoding. For example, using iconv:

    var Iconv = require('iconv').Iconv;
    var conv = new Iconv('utf8', 'windows-1251');
    data = conv.convert(data);

...or iconv-lite:

    var iconvLite = require('iconv-lite');
    data = iconvLite.encode(data, 'win1251');

Or write your own encode function like this (for default windows' cyrillic windows-1251):

    function utf8_decode_to_win1251 (srcStr) {
        var tgtStr = '', c = 0;
        for ( var i = 0; i < srcStr.length; i++ ) {
            c = srcStr.charCodeAt(i);
            if ( c > 127 ) {
                if ( c > 1024 ) {
                    if ( c === 1025 ) { c = 1016; }
                    else if ( c === 1105 ) { c = 1032; }
                    c -= 848;
                }
                // c = c % 256; // ???
            }
            tgtStr += String.fromCharCode(c);
        }
        return tgtStr;
    }
    data = utf8_decode_to_win1251(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.

Documentation

jsrtf's People

Contributors

jrowny avatar nytamin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

jsrtf's Issues

How to encode Win-1251 and special characters, for example `Abc Ø абв`?

Greetings @lilliputten!
Thank You for jsrtf library! This is not an issue but a question..

Can You please help mу how to encode non-latin (russian) letters, that are mixed with special symbols, for example: > Abc Ø абв
Here is english text, special symbol and russian text.

I have existing RTF template with 'placeholder' text inside, and what I need is to replace this 'placeholder' with 'Abc Ø абв':
s1-2023-09-27_182041

If I use Your function utf8_decode_to_win1251 - it successfully writes russian letters but finally I get "Ш" Instead of 'Ø':
s2-2023-09-27_175823

Here is my example code and input and output files:
input rtf: https://mega.nz/file/CtNB2CiY#yid1nLq9P6Jo8zSRAsXeGai-mZLV6xP1OvN1jDpFyG4
output generated by the code below: https://mega.nz/file/asMExKJI#q8oRn1J9oWMlUck6tJ6MdpVGiIjt81kNFRo7T3eSBTU

const http = require('http');
const port = 3100;

function utf8_decode_to_win1251(srcStr) {
  var tgtStr = "",
    c = 0;
  for (var i = 0; i < srcStr.length; i++) {
    c = srcStr.charCodeAt(i);
    if (c > 127) {
      if (c > 1024) {
        if (c === 1025) {
          c = 1016;
        } else if (c === 1105) {
          c = 1032;
        }
        c -= 848;
      }
      // c = c % 256; // ???
    }
    tgtStr += String.fromCharCode(c);
  }
  return tgtStr;
}


const server = http.createServer(function (req, res) {

  const fs = require('fs');

  // read existing file
  fs.readFile("C:\input.rtf", "utf8", (err, inputText) => {
    if (err) {
      console.error(err);
      return;
    }

    // I want to replace 'placeholder' text in file with this test text:
    let text = `Abc Ø абв`; // Abc Ø абв

    text = utf8_decode_to_win1251(text); // text with encoded russian letters 'Abc Ø àáâ'

    // replace placeholder from input RTF with text with non-latin characters 'Abc Ø àáâ':
    inputText = inputText.replace("placeholder", text);

    // RTF uses 8-bit so need to convert from unicode
    let buf = Buffer.from(inputText, "ascii"); // "binary" also gives wrong output text https://stackoverflow.com/a/34476862/348736


    // write output file to disk
    fs.writeFile("C:\output.rtf", buf, function (error, resultFile) { // result file contains 'Abc Ш абв', which is wrong..
      if (!error) {
        console.info('Created file', resultFile);
      }
      else {
        console.error(error);
      }
    });
  });
});


server.listen(port, function (error) {

  if (error) {
    console.log(`${error}`);
  } else {
    console.log(`listening on port ${port}`);
  }
})

I use LibreOffice v. 7.6.1.2 (X86_64) to view RTF files

Browser

Hello! Congrats on your initiative!
Can this code be used directly in the browser?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.