View PDF file in service portal

Juliana
Kilo Expert

I need to embed a PDF file for the user to view inside the page in service portal. I have tried the following, and nothing works.

 

The iframe downloads the atttachment instead of showing it.

<iframe id="inline_document" scrolling="no" style="width:100%;height:900px;border:none" src="sys_attachment.do?view=true&amp;sys_id=bc039edadbe923008fa34410ba9619b3">
</iframe>

The embed gives error saying it can't load the document.
<embed height="200px" src="/sys_attachment.do?sysparm_referring_url=tear_off&amp;view=true&amp;sys_id=bc039edadbe923008fa34410ba9619b3" type="application/pdf" width="200px"/>

The object gives error saying it can't load the document.
<object data="https://elodev.service-now.com/sys_attachment.do?sysparm_referring_url=tear_off&view=true&sys_id=bc039edadbe923008fa34410ba9619b3" type="application/pdf" width="100%" height="50%">
<p>Alternative text - include a link <a href="myfile.pdf">to the PDF!</a></p>


</object>

1 ACCEPTED SOLUTION

Juliana
Kilo Expert

I was able to make it work by converting the content Base64!

 

find_real_file.png

 

find_real_file.png

 

View solution in original post

33 REPLIES 33

Ihor
Giga Expert

This works for me fine:
HTML: (from Ashley's script, but do not forget to use {{data.final_string}} instead of {data.final_string})

<object data="{{data.final_string}}" 
        type="application/pdf" 
        height="100%" 
        width="100%" 
        class="internal">
</object>

In server side for SCOPED app you can use this method:

var sa = new GlideSysAttachment();
var gr = new GlideRecord('sys_attachment');
gr.get('d02cea531b029010d6c54229bc4bcb5b'); //sys_id of your attachment
var content = sa.getContentBase64(gr);
data.final_string = "data:application/pdf;base64,"+content;

OR you can store your base64 in property, and just use it like I did (string property with full base64 code in value):

var content = gs.getProperty('x_aaro2_hris_acces.document_to_sign_base64');
data.final_string = "data:application/pdf;base64,"+content;




Hi,

how will it work if the pdf is external stored. lets say sharepoint. So it above example would it be then:

<object data="https://sharepoint.blabla.com/blabla/file.pdf"
type="application/pdf"
height="100%"
width="100%"
class="internal">
</object>

or still being using iload. Is to view the pdf directly on the HTML / portal content.. 

Good Morning,

A couple of articles that might help:

https://www.reddit.com/r/servicenow/comments/d8z4b7/how_to_expose_data_from_external_application_in/

https://docs.servicenow.com/bundle/rome-servicenow-platform/page/administer/remote-tables/concept/remote-tables.html

Kind Regards

Ashley

I have added these scripts on my widget and sys ttachment has a PDF. used sys if of that here but PDF is not embeding.