Dynamically creating ODT using WebODF / Javascript
Using javascript
, I need to create a file .odt
and fill the content with data in variables javascript
. The only one I have found that can work is this WebODF
. An example that looks like it is here .
When I try to do something similar to PDF
using 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");
Can you do something like this with WebODF
? I found ops.OpInsertText , but I'm not sure how I can use it to insert text.
Again, ideally the solution is in javascript
.
source to share
If I asked the question correctly, you want to dynamically create a new file using the data in a JavaScript variable.
You can post this answer to load a file from a javascript variable as a byte array. And it will allow you to work with the odt file, which you can save in the 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 the multer framework, express.js to create services as a backend for saving files.
source to share
This might help you. In this example, I am adding the value returned from promt to the cursor position inside webodf. Likewise, you can insert data into any other offest () elements. pressing crtl + space will show promt and whatever we type is inserted into odf.
function insertBreakAtPoint(e) {
var range;
var textNode;
var offset;
var key = prompt("Enter the JSON Key", "name");
{% raw %}
var key_final = '{{address.'+key+'}}';
{% endraw %}
var caretOverlay=$('.webodf-caretOverlay').offset();
if (document.caretPositionFromPoint) {
range = document.caretPositionFromPoint(
caretOverlay.left, caretOverlay.top
);
textNode = range.offsetNode;
offset = range.offset;
} else if (document.caretRangeFromPoint) {
range = document.caretRangeFromPoint(
caretOverlay.left, caretOverlay.top
);
textNode = range.startContainer;
offset = range.startOffset;
}
#only split TEXT_NODEs
if (textNode.nodeType == 3) {
var replacement = textNode.splitText(offset);
var keynode = document.createTextNode(key_final);
textNode.parentNode.insertBefore(keynode, replacement);
}
}
function KeyPress(e) {
var evtobj = window.event? event : e
if (evtobj.keyCode == 32 && evtobj.ctrlKey)
insertBreakAtPoint();
}
document.onkeydown = KeyPress;
source to share