Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 15 Next »


Storing PDF Files in a Database

Ignition can store and display PDF files from data stored in databases. It stores and reads PDF files as bytes.

In a database, each column is a certain datatype. Keep in mind, that different databases use different datatypes, so it's best to become familiar your database and what datatypes support a large amount of PDF bytes. For example, in MySQL, the datatype that accepts PDF bytes is a LongBlob datatype, so you will need to set the PDF Data column to the LongBlob datatype. MS SQL accepts the Varbinary datatype, so you'll need to set the PDF Data column to a Varbinary datatype.  

Uploading PDF Files to the Database

Ignition can upload PDF documents to a database. Imagine the user pressing a button, a file browser pops up, the user selects the PDF file and presses Enter, and then the file gets uploaded into the database. The following script demonstrates how this might work.


# Find the path to the PDF file.
path = system.file.openFile("pdf")

# Read the file as bytes, and ask the user for a filename.
data = system.file.readFileAsBytes(path)
name = system.gui.inputBox("Enter a name for the file")

# Insert the data and name into the database.
system.db.runPrepUpdate("INSERT INTO documents (name, data) VALUES (?,?)", [name,data])

Displaying PDF Files from the Database

Ignition can render the PDF docment inside the PDF Viewer component, which is a part of the Reporting Module. To view PDF files in the Client, your Ignition server must have the Reporting Module installed. Once the module is installed, you can load the bytes from the database into the PDF Viewer component with the following script which is enacted on a Dropdown component property event handler.

# Get the id from the dropdown.
id = event.newValue

# Query for the raw pdf data and name by using the id.
bytes = system.db.runScalarQuery("SELECT data FROM documents WHERE id = %d" % id)
name = system.db.runScalarQuery("SELECT name FROM documents WHERE id = %d" % id)

# Load the bytes and the name into the PDF Viewer component.
event.source.parent.getComponent('PDF Viewer').loadPDFBytes(bytes, name)

On this page ...


IULocgo


Storing Files in a Database

 

 


Related Topics ...

 

 

  • No labels