
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2019 11:48 PM
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 😞
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-09-2019 05:43 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-09-2019 05:43 AM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-15-2019 10:52 PM
I have not thought of that. Will try that.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-26-2023 11:47 AM
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??
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-26-2023 05:22 PM
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.