Automatically Save Report PDF

Hi,

After learning about reports over the last week. I have now managed to create the report, albeit a couple of visual tweaks but its there.

I have a bit that starts transactions groups while the machine is running. That bit ends and another bit captures various data from the DB’s to populate the report.
I then want that same bit to activate a save report scrpit using the file name from a tag. I can create the script to get a pop up save or print box but I cant figure out how to automatically save.

But, I see after triggering the create report bit, it takes a second or two for the report to populate from the databases. So I might want to put a little delay in the save script. If that’s not possible I will create a PLC tag to do this.

What you’ll want to do is set a propertychange event on the dataset on the report. If you’re on 7.7 you can use the invokeLater option.

On the report viewer component, a method called getBytesPDF() exists.

report= event.source.parent.getComponent("Report Viewer")
system.file.writeFile("C:\temp\MyReport.pdf", report.getBytesPDF())

Thanks,

At the moment I have this code on a property change event on a label driven by a button for testing purposes.

data = event.source.parent.getComponent("Report") time = event.source.parent.getComponent('Report').CurrentTime reportBytes = data.getBytesPDF() filepath = ("C:Users\Craig\Desktop\Stones") filename = system.file.saveFile (time, ".pdf") if filename != None: system.file.writeFile(filename, reportBytes)

It works ok ‘ish’

At the moment it throws up an error becuase the file name expects 3 arguments. If I remove the .pdf it works but the file has no extension. Im unsure what the 3rd argument should be on that line.

The second problem is that is brings up a user save box rather then saving the report itself.

I like your idea of saving the report on a dataset property change. How do I go about that ?

I have a PLC tag that I made to prompt the save file, so I could put it on a client tag change script if needed.

I’ve modified the code so it automatically saves the file.

report = event.source.parent.getComponent("Report") bytes = report.getBytesPDF() time = event.source.parent.getComponent('Report').CurrentTime filename = "C:\\Users\Craig\Desktop\Stones\\time.pdf" if filename != None: system.file.writeFile(filename, bytes)

At the moment the file is saved as time.pdf and the next report over writes that file which is no good. How do I use the defined time in the filename.

I’ve now modified the code to work on a client change script.

I still haven’t worked out how to use my ‘time’ and the script seems to execute twice. Once on tag true then again on false.

window = system.gui.getWindow("Logging PDF") report = window.rootContainer.getComponent("Report") time = window.rootContainer.getPropertyValue("CurrentTime") bytes = report.getBytesPDF() filename = "C:\\Users\Craig\Desktop\Stones\\time.pdf" if filename != None: system.file.writeFile(filename, bytes)

[quote=“craigb”]I’ve now modified the code to work on a client change script.

I still haven’t worked out how to use my ‘time’ and the script seems to execute twice. Once on tag true then again on false.

window = system.gui.getWindow("Logging PDF") report = window.rootContainer.getComponent("Report") time = window.rootContainer.getPropertyValue("CurrentTime") bytes = report.getBytesPDF() filename = "C:\\Users\Craig\Desktop\Stones\\time.pdf" if filename != None: system.file.writeFile(filename, bytes)[/quote]

Here’s the basic idea:
(You’ll need to modify your time variable if it has illegal characters in it)

filename = "C:\\Users\Craig\Desktop\Stones\\%s.pdf" % time

Thankyou,

That saves the file as None.

Heres my time format dateFormat(now(), "dd-MMM-yy_h-mm a")

I’m guessing its not pulling the CurrentTime from my custom property

[quote=“craigb”]Thankyou,

That saves the file as None.

Heres my time format dateFormat(now(), "dd-MMM-yy_h-mm a")

I’m guessing its not pulling the CurrentTime from my custom property[/quote]

Could be. You can verify if that is the case or not by printing the value of time and watching the console window or use print type(time) and if the type is None then that’s the next problem to solve.

Hi guys,

I have been playing the the code above all day and I keep getting this error:

Traceback (most recent call last):

File “event:propertyChange”, line 2, in

AttributeError: ‘NoneType’ object has no attribute ‘getBytesPDF’

Ignition v7.7.2 (b2014121709)
Java: Oracle Corporation 1.8.0_31

I did a little searching and it seems either my event.source.parent.getComponent(“Report”) is not working correctly or my Reports don’t have the attribute.

I have tried event.source.parent.getComponent(“Report”), event.source.parent.getComponent(‘Report’), and event.source.parent.getComponent(Report)

I have also received the error NoneType object has no attribute ‘currenttime’ but i assume it’s the same general problem

thanks

AttributeError: 'NoneType' object has no attribute 'getBytesPDF' means that you tried to call ‘getBytesPDF’ on a variable that had the value None. If you’re using the line “report = window.rootContainer.getComponent(“Report”)”, make sure there really is a component on that window, and that it has exactly the name “Report” (without the quotes), and that it really is a report. Also, triple check the spelling of your variable names – that’s tripped me up more than once in Python. :cry:

So, I have been using this code for a while now and its been working perfectly.

window = system.gui.getWindow("Logging PDF") report = window.rootContainer.getComponent("Report") time = system.tag.read("Current Time").value bytes = report.getBytesPDF() filename = "C:\\Users\Craig\Desktop\Julia\\%s.pdf" % time print time if filename != None: system.file.writeFile(filename, bytes)

I would like to take it a little further. I have 2 tags “Stalk Number” and “Joint Number” I would like to have the file saved as “Stalk Number_Joint Number” so the output would be 1_10 for example.

I’m not sure how to merge the 2 tags to create the file name, any suggestions ?

You are really, really close to what you want to do. :slight_smile:

After your line time =… add stalk = system.tag.read("Stalk Number").value joint = system.tag.read("Joint Number").value Then change your filename line to filename = "C:\\Users\Craig\Desktop\Julia\\%s_%s_%s.pdf" % (time, stalk, joint)