odt2html

1.0.1 • Public • Published

odt2html

Node.js OpenOffice ODT format to HTML converter binary and library.

odt2html can be used to convert an OpenOffice .odt document (and basically every document created in respect with the Open Document Format) to HTML.

As a module

$ npm i odt2html --save

The module is promise-based:

var OdtConverter = require('odt2html');
 
OdtConverter.toHTML({
    path: 'path/to/file.odt'
})
.then(function(html) {
    // Use HTML here
}, function(err) {
    // An error occured during conversion
});

It accepts the following set of options:

var opts = {
    path: 'path/to/file.odt',
    // {String} - required
    imgFolder: 'path/to/export/images',
    // {String} - optional
    // The imgFolder will be created if it doesn't exist
    // Existing content might be overwritten by images
    // If imgFolder is not provided, document images will be inserted inline in base64
    beautify: true,
    // {Boolean} - optional (default: false)
    // Pretty-print/Indent rendered HTML
    trim: true,
    // {Boolean} - optional (default: false)
    // Remove empty paragraphs from rendered HTML
    openFilters: {},
    // {Object} - optional
    // Add custom open tags filters to improve/modify encoding to HTML
    closeFilters: {},
    // {Object} - optional
    // Add custom close tags filters to improve/modify encoding to HTML
    styleFilters: {}
    // {Object} - optional
    // Add custom style tags filters to improve/modify encoding to HTML
};
 
OdtConverter.toHTML(opts)
.then(function(html) {});

Extending filters

odt2html relies on sax to parse the XML content from the original document and encode it to HTML.

openFilters

The native openFilters can be extended by passing them in the option hash to the converter. Open filters will be called on the sax onopentag event.

Here is an example for the text:h open filter to convert titles:

var openFilters = {
  'text:h': function(node, styles, footnotes, opts, odt) {
    var tag = 'h';
    var level = node.attributes['text:outline-level'];
    if (level > 6) {
      level = 6;
    }
    tag += level;
 
    return { xml: node.name, html: tag };
};

The open filter function receives the following arguments:

// node:      the sax node element received from the onopentag event
// styles:    the global styles table
// footnotes: the global footnotes reference
// opts:      options hash of the converter
// odt:       input document as an adm-zip zipFile

Check out the adm-zip repo for more informations about this format.

To create an HTML tag for the currently processed node, the open filter function must return an object in the following form:

{
    xml: node.name,
    // {String} - required
    // passed sax node name (i.e. 'text:p')
    html: tag,
    // {String} - required
    // html tag name without the brackets (i.e. 'h' or 'p')
    attrs: attrs,
    // {Array} - optional
    // An array of attributes to create the HTML tag
    // The attributes are in the form { name: 'href', value: 'https://link.com' }
    isSelfClosing: true
    // {Boolean} - optional
    // Is the returned HTML tag self-closing (i.e. { xml: 'draw:image', html: 'img', isSelfClosing: true })
}

Open filter functions can also return a string of HTML that will be appended in place, or null, in which case the node won't be processed to HTML.

closeFilters

The native closeFilters can be extended by passing them in the option hash to the converter. Close filters will be called on the sax onclosetag event.

They are called with the same arguments as the openFilters, except for node, since the onclosetag sax event only pass the closing tag as a text string.

Their return value is not used.

styleFilters

The native styleFilters can be extended by passing them in the option hash to the converter. Style filters will be called on the sax onopentag event.

If a style filter function is matched for an opening tag, the tag won't be further processed to HTML (i.e. it won't be searched for in the openFilters).

Style filters are used to manage the styles table, which helps adding text decoration tags (<strong>, <em>, <blockquote>, ...) and manage lists styling. They receive as arguments the current node and the global styles table and their return value is not used.

Readme

Keywords

none

Package Sidebar

Install

npm i odt2html

Weekly Downloads

35

Version

1.0.1

License

Apache-2.0

Last publish

Collaborators

  • taranvohra
  • gitbook-bot
  • samypesse
  • jpreynat
  • emmerich