admin管理员组文章数量:1405532
I am using docxtemplater to convert JSON data to word document. Document is generating fine.
var sections = {"sections":[{"section_name":"Languages","data":"Tamil\nTelugu\nHindi\nEnglish","key":"8783"},{"section_name":"Skills","data":"JavaScript<br />jQuery<br />CSS<br />","key":"13486"}]};
function loadFile(url,callback){
JSZipUtils.getBinaryContent(url,callback);
}
loadFile("examples/doccc.docx",function(error,content){
if (error) { throw error; };
var zip = new JSZip(content);
var doc=new Docxtemplater().loadZip(zip);
doc.setOptions({nullGetter: function() {
return "";
}});
doc.setData(sections);
try {
// render the document (replace all occurences of {first_name} by John, {last_name} by Doe, ...)
doc.render();
}
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;
}
var out=doc.getZip().generate({
type:"blob",
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
}); //Output the document using Data-URI
saveAs(out,"output.docx");
});
This is my template
{#sections}
{section_name} - {data}
{/sections}
It generates the docx file with all the sections, but the "\n" new lines and
tags are printing literally in the document.
I need the new lines to be interpreted as new line.
Currently its printing as
Languages
Tamil\nTelugu\nHindi\nEnglish
Skills
JavaScript<br />jQuery<br />CSS<br />
in the word document.. Any idea how to print it as
Languages
Tamil
Telugu
Hindi
English
Skills
JavaScript
jQuery
CSS
Grateful for any help.
I am using docxtemplater to convert JSON data to word document. Document is generating fine.
var sections = {"sections":[{"section_name":"Languages","data":"Tamil\nTelugu\nHindi\nEnglish","key":"8783"},{"section_name":"Skills","data":"JavaScript<br />jQuery<br />CSS<br />","key":"13486"}]};
function loadFile(url,callback){
JSZipUtils.getBinaryContent(url,callback);
}
loadFile("examples/doccc.docx",function(error,content){
if (error) { throw error; };
var zip = new JSZip(content);
var doc=new Docxtemplater().loadZip(zip);
doc.setOptions({nullGetter: function() {
return "";
}});
doc.setData(sections);
try {
// render the document (replace all occurences of {first_name} by John, {last_name} by Doe, ...)
doc.render();
}
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;
}
var out=doc.getZip().generate({
type:"blob",
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
}); //Output the document using Data-URI
saveAs(out,"output.docx");
});
This is my template
{#sections}
{section_name} - {data}
{/sections}
It generates the docx file with all the sections, but the "\n" new lines and
tags are printing literally in the document.
I need the new lines to be interpreted as new line.
Currently its printing as
Languages
Tamil\nTelugu\nHindi\nEnglish
Skills
JavaScript<br />jQuery<br />CSS<br />
in the word document.. Any idea how to print it as
Languages
Tamil
Telugu
Hindi
English
Skills
JavaScript
jQuery
CSS
Grateful for any help.
Share Improve this question asked Apr 5, 2017 at 10:08 Jagan KJagan K 1,0573 gold badges15 silver badges39 bronze badges3 Answers
Reset to default 3I had the same problem and I solved it with :
doc = new Docxtemplater(zip, { linebreaks: true })
You can do :
In your template :
{#sections}
{section_name} -
{@data}
{/sections}
In your code, before setData :
sections.forEach(function(section){
var lines = section.data.split("\n").split(/<br \/>|\n/g)
var pre = "<w:p><w:r><w:t>";
var post = "</w:t></w:r></w:p>";
var lineBreak = "<w:br/>";
section.data = pre + lines.join(lineBreak) + post;
})
@Gabriel B. Thanks for the hint. I tried this and it works for me:
var doc = new docxtemplater();
var zip = new jszip(content);
// allow line break with \n
doc.loadZip(zip).setOptions({parser:retrieveCustomParser(), linebreaks:true});
doc.setData(data);
本文标签: javascriptNewlines or break tags with docxtemplaterStack Overflow
版权声明:本文标题:javascript - Newlines or break tags with docxtemplater - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744297517a2599415.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论