Giter Club home page Giter Club logo

rtf-to-html's Introduction

@iarna/rtf-to-html

Convert RTF to HTML in pure JavaScript.

NOTE: This version has beeh hacked up by 'Jayson Larose' such that it doesn't consolidate line breaks.

const rtfToHTML = require('@iarna/rtf-to-html')

fs.createReadStream('example.rtf').pipe(rtfToHTML((err, html) => {
  // …
})
rtfToHTML.fromStream(fs.createReadStream('example.rtf'), (err, html) => {
  // …
})
rtfToHTML.fromString('{\\rtf1\\ansi\\b hi there\\b0}', (err, html) => {
  console.log(html)
  // prints a document containing:
  // <p><strong>hi there</strong></p>
})

This is built on rtf-parser and shares its limitations.

This generates complete HTML documents from RTF documents. It does not currently have the facility to work on snippets of either.

Supported features:

  • Paragraph detection (results in <p> tags) plus empty paragraph trimming.
  • Font (as font-family: Font Name, Font Family). Font families in RTF don't map perfectly to HTML. This is the mapping we currently use:
    • roman: serif
    • swiss: sans-serif
    • script: cursive
    • decor: fantasy
    • modern: sans-serif
    • tech: monospace
    • bidi: serif
  • Font size (as font-size: #pt)
  • Bold (as <strong>)
  • Italic (as <em>)
  • Underline (as <u>)
  • Strikethrough (as <s>)
  • Superscript (as <sup>)
  • Subscript (as <sub>)
  • Foreground color (as color: rgb(#,#,#))
  • Background color (as color: rgb(#,#,#))
  • Paragraph first-line indents (as text-indent: #pt)
  • Idented regions (as padding-left: #pt)
  • Text alignment: left, right, center, justify (as text-align:)

rtfToHTML([opts], cb) → WritableStream

  • opts - Optional options to pass to the HTML generator. See the section on Options for details.
  • cb - A callback accepting (err, html), see the section on the Callback for details.

Returns a WritableStream that you can pipe into.

rtfToHTML.fromStream(stream[, opts], cb)

  • stream - A readable stream that should contain RTF.
  • opts - Optional options to pass to the HTML generator. See the section on Options for details.
  • cb - A callback accepting (err, html), see the section on the Callback for details.

rtfToHTML.fromString(string[, opts], cb)

  • string - A string containing RTF.
  • opts - Optional options to pass to the HTML generator. See the section on Options for details.
  • cb - A callback accepting (err, html), see the section on the Callback for details.

Callback

rtfToHTML returns HTML produced from RTF using a standard Node.js style callback, which should accept the following arguments: `(err, html)`.

If we encounter an error in parsing then it will be set in err. Otherwise the resulting HTML will be in html.

Options are always optional. You can configure how HTML is generated with the following:
  • paraBreaks - (Defaults to \n\n) Inserted between resulting paragraphs.
  • paraTag - (Defaults to p) The tagname to use for paragraphs.
  • template - A function that is used to generate the final HTML document, defaults to:
    function outputTemplate (doc, defaults, content) {
      return `<!DOCTYPE html>
    <html>
      <head>
        <meta charset="UTF-8">
        <style>
        body {
          margin-left: ${doc.marginLeft / 20}pt;
          margin-right: ${doc.marginRight / 20}pt;
          margin-top: ${doc.marginTop / 20}pt;
          margin-bottom: ${doc.marginBottom / 20}pt;
          font-size: ${defaults.fontSize / 2}pt;
          text-indent: ${defaults.firstLineIndent / 20}pt;
        }
        </style>
      </head>
      <body>
        ${content.replace(/\n/, '\n    ')}
      </body>
    </html>`
    }

You can also configure some of the starting (default) state of the output formatting with:

  • disableFonts - Defaults to true. If you set this to false then we'll output font change information when we encounter it. This is a bit broken due to our not supporting styles.
  • fontSize - Defaults to the document-wide declared font size, or if that's missing, 24.
  • bold - Defaults to false
  • italic - Defaults to false
  • underline - Defaults to false
  • strikethrough - Defaults to false
  • foreground - Defaults to {red: 0, blue: 0, green: 0}
  • background - Defaults to {red: 255, blue: 255, green: 255}
  • firstLineIndent - Defaults to the document-wide value, or if that's missing, 0. This is how far to indent the first line of new paragraphs.
  • indent: Defaults to 0
  • align: Defaults to left.
  • valign: Defaults to normal

rtfToHTML(doc[, opts]) → HTML

This is internally how the other interfaces are implemented. Unlike the other interfaces, this one is synchronous.

  • doc - A parsed RTF document as produced by the rtf-parser library.
  • opts - Optional options, see the section on Options for details.

rtf-to-html's People

Contributors

iarna avatar romick2005 avatar jaysonlarose avatar mprtenjak avatar

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.