Search code examples
javascriptodtwebodf

Dynamically creating ODT using WebODF / Javascript


Using javascript, I need to create an .odt file and populate the contents with data in javascript variables. The only thing that I have found that might work is WebODF. An example that seems similar to it is here.

When I am trying to do something similar to PDF with pdfkit (using node) I can do something like this:

PDFDocument = require('pdfkit');
var doc = new PDFDocument();
doc.pipe(fs.createWriteStream(fileName));
doc.text("Fist line");
doc.text("Second line");

Is it possible to do something similar to it using WebODF? I've found ops.OpInsertText, but I'm not sure how I can use it to actually insert text.

Again, ideally the solution is only in javascript.


Solution

  • If I got your question right, you want to create a new file dynamically using data in JavaScript variable.

    You ca refer this answer to load a file from javascript variable in form of byte Array. And this will get you up and running with a odt file ,which you can save to desired location.

    function saveByteArrayLocally(error, data) {
        var mime = "application/vnd.oasis.opendocument.text";
        var blob = new Blob([data.buffer], {type: mime});
    
        var res = $http({
            method: 'POST', url: myWebServiceUrl,
            headers: {'Content-Type': undefined},
            data: blob
        });
    
        res.success(function(data, status, headers, config) {
            console.log(status);
        });
    }
    

    NOTE: You can use multer,express.js framework to design services as backend to save files.