# Automatically Save Report PDF

**URL:** <https://forum.inductiveautomation.com/t/automatically-save-report-pdf/9679>\
**Category:** Ignition\
**Created:** [February 8, 2015, 10:09pm UTC](https://forum.inductiveautomation.com/t/automatically-save-report-pdf/9679 "2015-02-08T22:09:42Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![craigb](https://avatars.discourse-cdn.com/v4/letter/c/9f8e36/32.png) [@craigb](https://forum.inductiveautomation.com/u/craigb)\
**Post date:** [February 8, 2015, 10:09pm UTC](https://forum.inductiveautomation.com/t/automatically-save-report-pdf/9679/1 "2015-02-08T22:09:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Stevenson.Yuan](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@Stevenson.Yuan](https://forum.inductiveautomation.com/u/Stevenson.Yuan)\
**Post date:** [February 17, 2015, 3:47pm UTC](https://forum.inductiveautomation.com/t/automatically-save-report-pdf/9679/2 "2015-02-17T15:47:46Z")

</div>

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.

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

---

<div class="post-metadata">

**Author:** ![craigb](https://avatars.discourse-cdn.com/v4/letter/c/9f8e36/32.png) [@craigb](https://forum.inductiveautomation.com/u/craigb)\
**Post date:** [February 17, 2015, 5:40pm UTC](https://forum.inductiveautomation.com/t/automatically-save-report-pdf/9679/3 "2015-02-17T17:40:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![craigb](https://avatars.discourse-cdn.com/v4/letter/c/9f8e36/32.png) [@craigb](https://forum.inductiveautomation.com/u/craigb)\
**Post date:** [February 17, 2015, 10:25pm UTC](https://forum.inductiveautomation.com/t/automatically-save-report-pdf/9679/4 "2015-02-17T22:25:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![craigb](https://avatars.discourse-cdn.com/v4/letter/c/9f8e36/32.png) [@craigb](https://forum.inductiveautomation.com/u/craigb)\
**Post date:** [February 17, 2015, 10:58pm UTC](https://forum.inductiveautomation.com/t/automatically-save-report-pdf/9679/5 "2015-02-17T22:58:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![JGJohnson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jgjohnson/32/29582_2.png) [@JGJohnson](https://forum.inductiveautomation.com/u/JGJohnson)\
**Post date:** [February 17, 2015, 11:01pm UTC](https://forum.inductiveautomation.com/t/automatically-save-report-pdf/9679/6 "2015-02-17T23:01:40Z")

</div>

[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`

---

<div class="post-metadata">

**Author:** ![craigb](https://avatars.discourse-cdn.com/v4/letter/c/9f8e36/32.png) [@craigb](https://forum.inductiveautomation.com/u/craigb)\
**Post date:** [February 17, 2015, 11:16pm UTC](https://forum.inductiveautomation.com/t/automatically-save-report-pdf/9679/7 "2015-02-17T23:16:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![JGJohnson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jgjohnson/32/29582_2.png) [@JGJohnson](https://forum.inductiveautomation.com/u/JGJohnson)\
**Post date:** [February 17, 2015, 11:53pm UTC](https://forum.inductiveautomation.com/t/automatically-save-report-pdf/9679/8 "2015-02-17T23:53:23Z")

</div>

[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.

---

<div class="post-metadata">

**Author:** ![TheOldeBoy](https://avatars.discourse-cdn.com/v4/letter/t/5f9b8f/32.png) [@TheOldeBoy](https://forum.inductiveautomation.com/u/TheOldeBoy)\
**Post date:** [February 25, 2015, 8:24pm UTC](https://forum.inductiveautomation.com/t/automatically-save-report-pdf/9679/9 "2015-02-25T20:24:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![KathyApplebaum](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kathyapplebaum/32/7261_2.png) [@KathyApplebaum](https://forum.inductiveautomation.com/u/KathyApplebaum)\
**Post date:** [February 25, 2015, 8:45pm UTC](https://forum.inductiveautomation.com/t/automatically-save-report-pdf/9679/10 "2015-02-25T20:45:31Z")

</div>

`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. 😢

---

<div class="post-metadata">

**Author:** ![craigb](https://avatars.discourse-cdn.com/v4/letter/c/9f8e36/32.png) [@craigb](https://forum.inductiveautomation.com/u/craigb)\
**Post date:** [March 25, 2015, 2:20pm UTC](https://forum.inductiveautomation.com/t/automatically-save-report-pdf/9679/11 "2015-03-25T14:20:03Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![KathyApplebaum](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kathyapplebaum/32/7261_2.png) [@KathyApplebaum](https://forum.inductiveautomation.com/u/KathyApplebaum)\
**Post date:** [March 25, 2015, 3:06pm UTC](https://forum.inductiveautomation.com/t/automatically-save-report-pdf/9679/12 "2015-03-25T15:06:29Z")

</div>

You are really, really close to what you want to do. 🙂

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