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

 

I am trying to do something similar but loads the file has binary data. (see below). Can you please let me know if I am doing anything wrong.

 

find_real_file.png

find_real_file.png

 

Good Morning,

Hope you're doing well,

On line 3 in your html code you have a ':' after /pdf, this should be a ';'

Currently:

data="data:application/pdf:base64,{{data.base64}}"

Replace with:

data="data:application/pdf;base64,{{data.base64}}"

Take a look at my second post as well with the new code, you should be able to simplify your code a lot more after you get it working.

Hope this helps,

Kind Regards

Ashley

I have the exact same requirement to achieve and used same client and server code as above, there is no error but PDF is not shown on the widget on portal. What is missing here ? 

 

2 Widgets are part of portal page. Page renders 1 other  widget but not this widget meant for pdf embed

 

Server code of widget 

(function() {
	
	var attach = '99e44c6797bb7910d8a97c9fe153af44'; // sys id of record that has the pdf attachment
	var gr = new GlideRecord('sys_attachment');
	gr.addQuery('sys_id', attach);
	gr.query();
	if(gr.next()) {
		var sa = new GlideSysAttachment();
		var binData = sa.getBytes(gr);
		var base64 = GlideStringUtil.base64Encode(binData);
		data.base64 = base64;	
		data = base64;	
	}
})();

HTML code 

  <object data="data&colon;application/pdf;base64,{{data.base64}}" type="application/pdf" width="50%" height="60%" class="internal">
  <param name="view" value="fitH" /></object>

Pls help 

I have the exact same client and server code as above, there is no error but PDF is not shown on the widget on portal. What is missing ? 

 

2 Widgets are part of portal page. Page renders 1 other  widget but not this widget meant for pdf embed

 

Server code of widget 

(function() {
	
	var attach = '99e44c6797bb7910d8a97c9fe153af44'; // sys id of record that has the pdf attachment
	var gr = new GlideRecord('sys_attachment');
	gr.addQuery('sys_id', attach);
	gr.query();
	if(gr.next()) {
		var sa = new GlideSysAttachment();
		var binData = sa.getBytes(gr);
		var base64 = GlideStringUtil.base64Encode(binData);
		data.base64 = base64;	
		data = base64;	
	}
})();

HTML code this-

  <object data="data&colon;application/pdf;base64,{{data.base64}}" type="application/pdf" width="50%" height="60%" class="internal">
  <param name="view" value="fitH" /></object>

 

How to use this script on catalog item. Is there any variable need to configure ?