open-xml-templating / docxtemplater-build Goto Github PK
View Code? Open in Web Editor NEWBuilt versions of docxtemplater
Built versions of docxtemplater
Hi,
I'm using the below code , but I got this error : Docxtemplater is not defined.
<html>
<script src="js/docxtemplater.min.js"></script>
<script src="js/FileSaver.min.js"></script>
<script src="js/jszip-utils.min.js"></script>
<!--
Mandatory in IE 6, 7, 8 and 9.
-->
<!--[if IE]>
<script type="text/javascript" src="js/jszip-utils-ie.js"></script>
<![endif]-->
<script>
var loadFile=function(url,callback){
JSZipUtils.getBinaryContent(url,callback);
}
loadFile("sample.pptx",function(err,content){
if (err) { throw e};
doc=new Docxtemplater(content);
doc.setOptions({fileType: "pptx"});
doc.setData( {"first_name":"Hipp",
"last_name":"Edgar",
"phone":"0652455478",
"description":"New Website"
}
) //set the templateVariables
doc.render() //apply them (replace all occurences of {first_name} by Hipp, ...)
out=doc.getZip().generate({type:"blob"}) //Output the document using Data-URI
saveAs(out,"output.docx")
})
</script>
</html>
what's wrong here ?
Hi,
Please can someone help. DocxTemplater works in Firefox and Chrome fine, but it does't work in IE 11. I have also tested the online demo at https://docxtemplater.com/demo/ in IE 11 and it doesn't work. Can someone please help?
Many thanks!
Hi,
The things I know are:
docxtemplater code:
while (cursor < contentLength) {
cursor = content.indexOf("<", cursor);
if (cursor === -1) {
break;
}
var offset = cursor;
cursor = content.indexOf(">", cursor);
var tagText = content.slice(offset, cursor + 1);
var _getTag = getTag(tagText),
tag = _getTag.tag,
position = _getTag.position;
var text = allMatches[tag];
if (text == null) {
continue;
}
totalMatches.push({
type: "tag",
position: position,
text: text,
offset: offset,
value: tagText,
tag: tag
});
}
my script:
function doIt() {
function loadFile(url, callback) {
JSZipUtils.getBinaryContent(url, callback);
}
loadFile('../tag-example.docx', function(error, content) {
if (error) {
throw error;
}
var zip = new JSZip();
zip.file('word/document.xml', content);
var doc = new Docxtemplater().loadZip(zip);
doc.setData({
first_name: 'John',
last_name: 'Doe',
phone: '0652455478',
description: 'New Website'
});
try {
// render the document (replace all occurences of {first_name} by John, {last_name} by Doe, ...)
doc.render();
console.log('asdf');
} catch (error) {
var e = {
message: error.message,
name: error.name,
stack: error.stack,
properties: error.properties
};
console.log(JSON.stringify({ error: e }));
// The error thrown here contains additional information when logged with JSON.stringify (it contains a property object).
throw error;
}
console.log('rendered');
var out = doc.getZip().generate({
type: 'blob',
mimeType:
'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
}); //Output the document using Data-URI
saveAs(out, 'output.docx');
});
}
Hi,
I installed via npm install docxtemplater
My Page has a script tag to the js-file and from the development-tools (F12) I can see, the file has been loaded. But if I use var doc = new Docxtemplater().loadZip(zip); console Output is 'Docxtemplater' is undefined.
Within the docxtemplater.js-file I can find var Docxtemplater=function()...
So, what is the Problem here?
Any help appriciated, with Kind Regards, Ronny
My codes:
docx
var DocxReader = function() {
// Var
this.url = "";
this.error = null;
this.zipContent = null;
this.file = {
contentAsString: null,
contentAsXml: null
};
this.docxtemplater = null;
this.name = "output.docx";
// Methods
this.Load = function(url, loaded) {
this.url = url;
var splitUrl = this.url.split('/');
this.name = splitUrl[splitUrl.length - 1];
var docThis = this;
JSZipUtils.getBinaryContent(this.url, function(error, content) {
if (error) {
docThis.error = error;
return;
}
docThis.zipContent = new JSZip(content);
var documentxml = docThis.zipContent.file("word/document.xml");
var strDocumentxml = documentxml.asText();
docThis.file.contentAsString = strDocumentxml;
docThis.file.contentAsXml = $(strDocumentxml);
docThis.docxtemplater = new Docxgen();
docThis.docxtemplater.loadZip(docThis.zipContent);
loaded();
});
};
this.loadZip(zip) {
if (zip.loadAsync) {
throw new XTInternalError(
"Docxtemplater doesn't handle JSZip version >=3, see changelog"
);
}
this.zip = zip;
this.updateFileTypeConfig();
return this;
}
this.Search = function(txt) {
var lowerCaseTxt = txt.toLowerCase();
var innerDocument = this.file.contentAsXml.find('w\\:body').text().toLowerCase();
if (innerDocument.indexOf(lowerCaseTxt) != -1) {
return true;
}
return false;
};
this.Replace = function(oldtxt, newtxt) {
var oldContent = this.file.contentAsString;
var newContent = oldContent.replace(new RegExp(oldtxt, 'g'), newtxt);
this.zipContent.file("word/document.xml", newContent);
// Update object
this.docxtemplater.loadZip(this.zipContent);
this.file.contentAsString = newContent;
this.file.contentAsXml = $(newContent);
};
this.ReplaceVariable = function(variables) {
this.docxtemplater = setData(variables);
this.docxtemplater.render();
// Update object
this.zipContent = this.docxtemplater.getZip();
var documentxml = this.zipContent.file("word/document.xml");
var strDocumentxml = documentxml.asText();
this.file.contentAsString = strDocumentxml;
this.file.contentAsXml = $(strDocumentxml);
};
this.SetName = function(name) {
this.name = name + ".docx";
};
this.Download = function() {
var out = this.docxtemplater.getZip().generate({
type: "blob",
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
})
saveAs(out, this.name);
};
this.UploadToSharePoint = function(clientContext, jsomListObject, onSucceed, onFailed) {
var base64 = this.zipContent.generate();
var listRootFolder = jsomListObject.get_rootFolder();
clientContext.load(listRootFolder);
clientContext.executeQueryAsync(Function.createDelegate(this, function() {
var fileName = listRootFolder.get_serverRelativeUrl() + '/' + this.name;
//Create FileCreationInformation object using the read file data
var createInfo = new SP.FileCreationInformation();
createInfo.set_content(base64);
createInfo.set_url(fileName);
//Add the file to the library
var uploadedDocument = jsomListObject.get_rootFolder().get_files().add(createInfo);
//Load client context and execcute the batch
clientContext.load(uploadedDocument);
clientContext.executeQueryAsync(Function.createDelegate(this, onSucceed), Function.createDelegate(this, onFailed));
}), Function.createDelegate(this, onFailed));
};
}
scripts:
<script src="/js/docxgen.min.js"></script>
<script src="/js/jszip.min.js"></script>
<script src="/js/jszip.utils.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/docxtemplater/3.1.7/docxtemplater.js"></script>
<script src="/js/file-saver.min.js"></script>
<script src="/js/DocxReader.js"></script>
js using docxreader
```
var docx = new DocxReader();
docx.Load(tmppath, function() {
docx.Replace("Lorem", "REPLACED");
try {
// render the document (replace all occurences of {first_name} by John, {last_name} by Doe, ...)
docx.docxtemplater.render();
} catch (error) {
var e = {
message: error.message,
name: error.name,
stack: error.stack,
properties: error.properties,
}
console.log(JSON.stringify({ error: e }));
throw error;
}
});
I'm using Google Chrome and get docxtemplater is not defined on console.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.