PDF Viewer Dynamic

Hello Everyone.

Im working in project and i have a problem , i need to create some views with PDF viewer and if the pdf has changes automactlly it change on the pdf viewer. the idea is that it not stay static.

You'll need to script getting the file modified time and store that...keep polling that file and if the time changes, then refresh the binding on the PDF viewer. There isn't a way that I know of for the PDF viewer component to "know" when the source file has changed.

Since this is perspective, any tracking of changes will need to be done on the gateway, not the client. A filewatcher could be used to determine changes of files.

can you help me with this script?

Feel free to DM me to discuss integration services if you need assistance. Some of the folks on this forum are integrators (including myself) and make their living by solving problems for others. However, feel free to post what you have tried and we can help guide you in the right direction...without doing the actual work.

For example, I can tell you that you will need to use import os, glob libraries to get a listing of all files in a particular path (provided the gateway has access to that folder), filter the files you need (such as looking for .pdf files only), make sure they are files and not folders (yes you could potentially have a folder named Test.pdf), then get the modified date using os.path.getmtime() function. From there, decide what you want to do with the file.

Ewww! Use the java libraries! (java.io.File and friends.)

1 Like

I will look into this. Thanks Phil.

This looks cleaner

import os, glob
files = list(filter(os.path.isfile, glob.glob("C:\SomeFolder\*.csv")))
files.sort(key=lambda x: os.path.getmtime(x))

than this

import java.io.File as File

def fileFilter(filename):
	return str(filename).lower().endswith(".csv")
#end def
files = list(File("C:\SomeFolder").listFiles(fileFilter))
files.sort(key=lambda x: File.lastModified(x))

Ignition's underlying system is Java...but we script in Jython (Python-ish). Is using the java library more performant than the python one (less conversions to native Java)? Or is it more of a preference. I recall your statement about better quality fur. In that case, I'd substitute your Ewww for this:

giphy

1 Like

Yes.

It will also not screw up unicode characters in file names, or make mistakes when accessing/setting timestamps (because jython's datetime module is woefully out of date, and will never be updated).

I don't consider broken code to be cleaner than unbroken code.

Also, a more apples to apples comparison:

from java.io import File
fileFilter = lambda filename: str(filename).lower().endswith(".csv")
target = File(r"C:/SomeFolder")
files = sorted(target.listFiles(fileFilter), key=lambda f: f.lastModified())

It's not really all that different.

3 Likes

I would tweak that to:

from java.io import File
fileFilter = lambda filename: filename.lower().endswith(".csv")
target = File(r"C:/SomeFolder")
files = sorted(target.list(fileFilter), key=lambda f: f.lastModified())

The .listFiles(...) method has an ambiguous signature.

Or perhaps:

from java.io import File
target = File(r"C:/SomeFolder")
files = sorted(target.list(lambda f: f.lower().endswith(".csv")), key=lambda f: f.lastModified())

{Sigh} Never mind. You need .listFiles() to be able to check .lastModified().

I should add a system.file.listFiles or something that internally uses Files.find, maybe...

I'll put it on the list of things to think about.

java.nio.file has more classes that can be used for this task, including FileSystem.getPathMatcher support for globs.

1 Like

Where i should put this code? for call the file

Lots of places. Maybe a timer script? It all depends on the overall scope of your application. Maybe this PDF file an instruction manual which may only change once a year...if that's the case, don't do anything, let the user open the page and the PDF viewer will fetch the file. If the PDF file is changing while the user has the perspective page open (and doesn't navigate away from the page often), then you'll need to implement more logic on the page. I wonder...are you trying to make an application to display work instructions for operators that are modified by another dept. throughout the day?

yeah i understand your point but how can i view the file on PDF VIewer?

You need to serve the file to your Perspective session at a URL. This is typically done with the WebDev module.

now how can call the script to view on PDFViewer

i have the WebDev module but the file keep static

Browsers cache everything they receive, by default. Some options:

  • Send a cache control header from WebDev with a short timeout.

  • Include a "throw away" query parameter (random number, perhaps) in the URL to default defeat browser caching.

  • Don't re-use file names for a particular purpose. Add version numbers or similar to ensure uniqueness.