GlideSysAttachment writeBase64 not working correctly

Victorien
Giga Contributor

Hi !

 

I am having a problem using GlideSysAttachment writeBase64 function.

Currently, I am making a custom PDF using jspdf on the client side and then I am sending the body of the PDF to the server side using Ajax. On the server side, when I use GlideSysAttachment.write() with the required data and the string of the PDF's body, it works but the UTF-8 characters aren't correctly displayed. When I use writeBase64, nothing happens.

 

Is this the right way to do ? 

14 REPLIES 14

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Please share the relevant script for further help

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

This is my Ajax Script :

var AttachAjaxUtil = Class.create();
AttachAjaxUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	attach: function() {
		try {
			// Parameters
			var sysId = this.getParameter('sys_id');
			var tableName = this.getParameter('table_name');
			var filename = this.getParameter('filename');
			var type = this.getParameter('type');
			var attachment = this.getParameter('attachment');

			var rec = new GlideRecord(tableName);
			rec.get(sysId);
			var aId = new GlideSysAttachment().write(rec, filename, type, attachment);

			return JSON.stringify({attachmentID: aId});
		} catch (e) {
			gs.log("AAU - Error : " + e.toString());
		}
		return null;
	},
	type: 'AttachAjaxUtil'
});

 

Which is called like that in a client script :

var ga = new GlideAjax('AttachAjaxUtil');
	ga.addParam('sysparm_name', 'attach');
	ga.addParam('sys_id', g_form.getUniqueValue());
	ga.addParam('table_name', 'pm_project');
	ga.addParam('type', 'application/pdf');
	ga.addParam('filename', pdf.getFilename());
	ga.addParam('attachment', pdf.getBody());
	ga.getXML(function (response) {
		var answer = response.responseXML.documentElement.getAttribute("answer");
		console.log(answer);
	});

 

pdf.getBody() simply returns the result of this function (jspdf.output(), with no args).

 

With that I am able to generate a PDF with utf8 character problems!

Hi,

Can you share the complete client script as unable to relate it?

How are you using the function which you have shared the link

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

This is the client script (at least the only thing related to the PDF)

var pdf = new TimesheetPDF({});
pdf.build();
var ga = new GlideAjax('AttachAjaxUtil');
ga.addParam('sysparm_name', 'attach');
ga.addParam('sys_id', g_form.getUniqueValue());
ga.addParam('table_name', 'pm_project');
ga.addParam('type', 'application/pdf');
ga.addParam('filename', pdf.getFilename());
ga.addParam('attachment', pdf.getBody());
ga.getXML(function (response) {
	var answer = response.responseXML.documentElement.getAttribute("answer");
	console.log(answer);
});
pdf.save();

 

And the TimesheetPDF class (shorten, to make it simplier)

var TimesheetPDF = Class.create();

TimesheetPDF.prototype = {
	initialize : function() {
		this.doc = jspdf.jsPDF({
			orientation: "landscape"
		});

		this.filename = "my_pdf";
	},
	
	getFilename: function() {
		return this.filename;
	},
	
	getBody: function() {
		return this.doc.output();
	},

	save: function() {
		this.doc.save((this.filename || "Timesheet") + ".pdf");
	},

	build: function() {
		this.doc.text("Title", 100, 100, {maxWidth: 300, align: 'center'});
	},

	type : 'TimesheetPDF'
};

 

and the lib simply is https://github.com/MrRio/jsPDF