Giter Club home page Giter Club logo

jsrtf's People

Stargazers

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

Watchers

 avatar  avatar

jsrtf's Issues

Browser

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

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

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.