Email pdf Reports From client

We have an Ignition system that has reports automatically generated by a client running on the gateway server. There us a pdf viewer set up on the client to view the generated reports via the IIS setup on the gateway server.
Now the client wants a button on the report viewer window to allow any user to email the viewed report to anyone. I’ve tried using the emailing script but the getFileAsBytes() function doesn’t seem to work with the “http://website/…/filename.pdf” file path.
Is there any way of retrieving the pdf file from the gateway and emailing it?

Add the following code to app.net

[code]def getStream(url):
from java.net import URL
con = URL(url).openConnection()
con.setRequestMethod(“GET”)
con.setDoOutput(0)
con.setDoInput(1)
stream = con.getInputStream()
return stream

def streamToBytes(stream):
bytes = []
byte = stream.read()
while byte != -1:
bytes.append(byte)
byte = stream.read()
return bytes

def getBytesArray(bytes):
from jarray import array

return array(bytes,"b")

def httpGetBytes(url):
from app.net import getStream,streamToBytes,getBytesArray
try:
stream = getStream(url)
bytes = streamToBytes(stream)
stream.close()
bytesArray = getBytesArray(bytes)
return bytesArray
except:
return None[/code]

Now, in your script, to get the bytes for your attachment, just run attachmentBytes = app.net.httpGetBytes("http://someurl.com/file.pdf")

Thanks for that Kyle, it worked a treat!

I’m not sure why I’m having a problem with this script but it seems to stem from the java read() function. The documentation says that this method “reads an unsigned byte from the input stream and returns an int between 0 and 255”. Now the problem I have is that the Jython array function (code = return array(bytes, “b”)) expects the bytes to be signed bytes (-128 to 127). As a result when i pass in the PyList that is created using the read() function I get an error that says the “value is too large for a byte.” I’ve spent a good portion of the day trying to figure out how to fix this to no avail. About ready to throw the computer through the window. Would appreciate any help. Thank you.

I can see why you’re confused! I recommend adding print statements to the streamToBytes() function to see what exactly is being appended into the array. Once you know that, you should be able to track down the problem.