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

**URL:** <https://forum.inductiveautomation.com/t/webdev-python-resources-display-pdf-in-perspective-pdf-viewer-or-iframe/55358>\
**Category:** Ignition\
**Tags:** perspective, scripting\
**Created:** [January 25, 2022, 3:35pm UTC](https://forum.inductiveautomation.com/t/webdev-python-resources-display-pdf-in-perspective-pdf-viewer-or-iframe/55358 "2022-01-25T15:35:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zachary\_Kacel](https://avatars.discourse-cdn.com/v4/letter/z/ecb155/32.png) [@Zachary\_Kacel](https://forum.inductiveautomation.com/u/Zachary_Kacel)\
**Post date:** [January 25, 2022, 3:35pm UTC](https://forum.inductiveautomation.com/t/webdev-python-resources-display-pdf-in-perspective-pdf-viewer-or-iframe/55358/1 "2022-01-25T15:35:45Z")

</div>

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

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [January 25, 2022, 4:10pm UTC](https://forum.inductiveautomation.com/t/webdev-python-resources-display-pdf-in-perspective-pdf-viewer-or-iframe/55358/2 "2022-01-25T16:10:29Z")

</div>

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

> [@WebDev return PDF with file name](https://forum.inductiveautomation.com/t/webdev-return-pdf-with-file-name/45055/5):
>
> You got me down the right path. request["servletResponse"].addHeader('Content-Disposition', 'attachment; filename="{}.pdf"'.format(ticketNum)) Time to read up on the HttpServletResponse object in case this customer wants anything else. Sigh. Thanks for the help!

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

---

<div class="post-metadata">

**Author:** ![Zachary\_Kacel](https://avatars.discourse-cdn.com/v4/letter/z/ecb155/32.png) [@Zachary\_Kacel](https://forum.inductiveautomation.com/u/Zachary_Kacel)\
**Post date:** [January 25, 2022, 4:29pm UTC](https://forum.inductiveautomation.com/t/webdev-python-resources-display-pdf-in-perspective-pdf-viewer-or-iframe/55358/3 "2022-01-25T16:29:54Z")

</div>

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.

```auto
return {'contentType':'application/pdf','bytes':binary_format}

```

```auto
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}

```

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [January 25, 2022, 5:11pm UTC](https://forum.inductiveautomation.com/t/webdev-python-resources-display-pdf-in-perspective-pdf-viewer-or-iframe/55358/4 "2022-01-25T17:11:56Z")

</div>

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.
