使用 docxtemplater 换行或打断标签
Newlines or break tags with docxtemplater
我正在使用 docxtemplater 将 JSON 数据转换为 word 文档。文档生成正常。
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");
});
这是我的模板
{#sections}
{section_name} - {data}
{/sections}
它生成包含所有部分的 docx 文件,但“\n”新行和
标签在文档中按字面意义打印。
我需要将新行解释为新行。
目前打印为
Languages
Tamil\nTelugu\nHindi\nEnglish
Skills
JavaScript<br />jQuery<br />CSS<br />
在 word 文档中..知道如何将其打印为
Languages
Tamil
Telugu
Hindi
English
Skills
JavaScript
jQuery
CSS
感谢您的帮助。
你可以这样做:
在您的模板中:
{#sections}
{section_name} -
{@data}
{/sections}
在您的代码中,在 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;
})
我遇到了同样的问题,我用 :
解决了它
doc = new Docxtemplater(zip, { linebreaks: true })
@Gabriel B. 感谢提示。我试过了,它对我有用:
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);
我正在使用 docxtemplater 将 JSON 数据转换为 word 文档。文档生成正常。
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");
});
这是我的模板
{#sections}
{section_name} - {data}
{/sections}
它生成包含所有部分的 docx 文件,但“\n”新行和
标签在文档中按字面意义打印。
我需要将新行解释为新行。
目前打印为
Languages
Tamil\nTelugu\nHindi\nEnglish
Skills
JavaScript<br />jQuery<br />CSS<br />
在 word 文档中..知道如何将其打印为
Languages
Tamil
Telugu
Hindi
English
Skills
JavaScript
jQuery
CSS
感谢您的帮助。
你可以这样做:
在您的模板中:
{#sections}
{section_name} -
{@data}
{/sections}
在您的代码中,在 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;
})
我遇到了同样的问题,我用 :
解决了它doc = new Docxtemplater(zip, { linebreaks: true })
@Gabriel B. 感谢提示。我试过了,它对我有用:
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);