Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

line break bug #407

Closed
araneta opened this issue Jul 18, 2018 · 3 comments
Closed

line break bug #407

araneta opened this issue Jul 18, 2018 · 3 comments

Comments

@araneta
Copy link

araneta commented Jul 18, 2018

Environment

  • Version of docxtemplater : "docxtemplater": "^3.6.2",
  • Used docxtemplater-modules :
  • Runner : nodejs v8.9.0

How to reproduce my problem :

My template is the following : https://drive.google.com/file/d/1uz4l5Dw4vsmG73RXLDO1xKukoJwqBvnW/view?usp=sharing

With the following js file :

var fgen = uuidv4()+'.docx';
var pre = '<w:p><w:r><w:t>';
var post = '</w:t></w:r></w:p>';
var lineBreak = '<w:br/>';
var str = signaturex.content.replace("\n", lineBreak);
req.body.signature_text = pre+str+post;
dispo.generateDocx(dispo.docTypeFolder()+docType.fileName, req.body, fgen); 

//dispo.js
//generate doc from doc type
exports.generateDocx = function(templateDocx, fieldValues, resultDocx){
	//Load the docx file as a binary
	var content = fs
    .readFileSync(templateDocx, 'binary');

	var zip = new JSZip(content);

	var doc = new Docxtemplater();
	doc.loadZip(zip);
	//set the templateVariables
	doc.setData(fieldValues);

	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 buf = doc.getZip()
				 .generate({type: 'nodebuffer'});

	// buf is a nodejs buffer, you can either write it to a file or do anything else with it.
	
	fs.writeFileSync(path.resolve(config.generated_docx_folder, resultDocx), buf);
		
}

the signaturex.content variable is plain text:
a.n. Rektor,
Kepala Biro AUAK IAIN Tulungagung

(Tanda Tangan dan Cap Dinas)

Nama Lengkap
NIP.

=============================

its working but the font family changed to times new roman and the text position is changed to the left side, please check the result doc at https://drive.google.com/file/d/1SotE0XTYXSzb1y8mzMvy8ijLNP1xbCj_/view?usp=sharing

Thanks

@edi9999
Copy link
Member

edi9999 commented Jul 19, 2018

Hello @araneta ,

The raw-xml module is indeed too low level to handle the newlines very well. I will consider removing it from the documentation.

The best I can advice for now is to do as @jtetherton suggested in another issue : #144 (comment)

That is rewrite DocUtils to be :

DocUtils.charMap = {
	"&": "&amp;",
	"'": "&apos;",
	"<": "&lt;",
	">": "&gt;",
	"\n":"<w:br/>"
};

This way, you won't have to use raw xml tags, every tagvalue containing a newline will create them in the resulting document.

@edi9999 edi9999 closed this as completed Jul 19, 2018
@edi9999
Copy link
Member

edi9999 commented Jul 19, 2018

In docxtemplater 4.0, this will be the default behavior.

@edi9999
Copy link
Member

edi9999 commented Jul 20, 2018

Hello @araneta ,

I wanted to let you know that I have now made it more easy to add newlines.

You now do not need to use the rawmodule anymore, but it can be set with a global option :

    doc.setOptions({linebreaks:true});
    doc.setData({text: "My text,\nmultiline"});
    doc.render();

You will need docxtemplater 3.8.0, which I have just published.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants