WebDev Python Resources, Display PDF in Perspective PDF Viewer or IFrame

I have a webserver that hosts PDF in a byte array and want to display it in a perspective PDFViewer or IFrame. I have working Code in Ignition that can save the file locally, but I would like to put that PDFdata directly in the PDF Viewer. I found some info searching forms that this should be possible with a Python Resource in the WebDev Module, but I need help on the Perspective side and the WebDev side.

Working Code to write to local File

request = urllib2.Request(myURL, headers=headers)
webURL = urllib2.urlopen(request)
result = webURL.read()
encoding = webURL.info()
obj = json.loads(result.decode('utf-8'))
PDFbyte = obj['filecontents']
binary_format = bytearray(PDFbyte)

with open("C:\Programing\Python\PDFfile.pdf", 'wb') as binary_file:
	binary_file.write(binary_format)

From Webdev you just need to load your PDF bytes in some way and add the appropriate headers to the response, e.g.:

From Perspective you should just need to pass the path to your webdev endpoint (/system/webdev/$project/$path).

I am not fully understanding. I have the PDF Bytes already as I can write it to a local file as a PDF. I have tried the below code and placed the path to my python resources in perspective, but still get PDF can’t be displayed.

return {'contentType':'application/pdf','bytes':binary_format}
request = urllib2.Request(myURL, headers=headers)
webURL = urllib2.urlopen(request)
result = webURL.read()
	
encoding = webURL.info()
obj = json.loads(result.decode('utf-8'))
PDFbyte = obj['olps_filecontents']
binary_format = bytearray(PDFbyte)
	
return {'contentType':'application/pdf','bytes':binary_format}

So you only want Ignition to act as a sort of proxy to this PDF that’s already hosted on a webserver?
I would skip Ignition entirely and either a: request from this other server directly in Perspective, or b: use an alternate frontend (i.e. a reverse proxy like nginx) running in parallel to Ignition to route these requests.

If you really want to do this via Webdev, I would skip any and all Python standard library stuff. You’re going to want to do a pure Java network fetch, get an InputStream, and copy it directly to the Webdev servlet’s output stream - that way, your actual PDF’s contents won’t be held in memory to stream to the client, only a minor buffer during the copy.

1 Like