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

 Hi Tommy Jensen , ChrisBurks

 

I have also kind a similar requirement,
Requirement: I need to download the UI Page using Jspdf but I don,t know How to import Jspdf in servicenow.
Can You please tell me How do it.

Thanks in Advance