Write attachment from jsPDF output

Tommy Jensen
Giga Guru

I am creating a pdf with the library jsPDF and this works and I can preview the pdf in the Service Portal.

I now need to write the pdf into an attachment on a case but I cannot get it to work.

First question, which output datatype from jsPDF ( http://raw.githack.com/MrRio/jsPDF/master/docs/jsPDF.html#output ) should I use ? For view on screen I am using datauristring.

 

My function to write the attachment is in a global script include and looks like this : 


insertAttachment: function (gr, fileName, contentType, content){
		
		var contentArr = content.split(',');		
		var att = new GlideSysAttachment();		
		att.writeBase64(gr, fileName, contentType, contentArr[1]);
		att.writeBase64(gr, fileName, contentType, gs.base64Decode(contentArr[1]));        
		att.write(gr, fileName, contentType, contentArr[1]);
		att.write(gr, fileName, contentType, gs.base64Decode(contentArr[1]));
		
	},

Neither of the writeBase64 works. No attachment is stored.

The two write do write attachments but they cannot be opened.

Of course I have also tried to not do a split on the content input.

 

Contenttype is set to: application/pdf

Content: I have tried almost all of the possible types available from jsPDF.

 

I am stuck 😞

1 ACCEPTED SOLUTION

ChrisBurks
Mega Sage

Have you tried using the Attachment API client side? 

If you have the table name and sys_id of the record you want to attach you should be able to do something like this using "blob" as output:

var config = {
	'headers':{
		'Content-type':'application/pdf',
		'Accept':'application/json'
	}
}

$http.post("/api/now/attachment/file?table_name=incident&table_sys_id=9d385017c611228701d22104cc95c371&file_name=myfile.pdf",doc.output("blob"),config);

Note: Replace your respective values in the url for table_name, table_sys_id, and file_name. Also remember to pass $http to the client controller

 

 

View solution in original post

5 REPLIES 5

ChrisBurks
Mega Sage

Have you tried using the Attachment API client side? 

If you have the table name and sys_id of the record you want to attach you should be able to do something like this using "blob" as output:

var config = {
	'headers':{
		'Content-type':'application/pdf',
		'Accept':'application/json'
	}
}

$http.post("/api/now/attachment/file?table_name=incident&table_sys_id=9d385017c611228701d22104cc95c371&file_name=myfile.pdf",doc.output("blob"),config);

Note: Replace your respective values in the url for table_name, table_sys_id, and file_name. Also remember to pass $http to the client controller

 

 

I have not thought of that. Will try that.

Hi @ChrisBurks 

Thanks for the solution, this works when we add the auth header here. But i see another issue after the post call in client script on service portal, if we check the gs.getuser() it  returns the user we have used in the auth header and not the logged in user on the portal. Can you suggest any solution about this??

Hi @Yogi12 ,

This would depend on your setup. If the person who is logged in has the permissions to add the attachment then you shouldn't have to setup a different user to authorize the post as the example I posted is done client side and the user would already be logged in.