Opening files in blender game

I want to open a .pdf in blender game (for instance clicking on a cube and opening a .pdf file). Is there a way to do this in python or logic bricks that anyone knows of?

There are two ways:

  1. Show inside BGE. This is a huge amount of work, but is possible given sufficient time. I recently wrote a .pptx viewer inside BGE. I would not suggest doing this unless you have a real need to
  2. Hand it out to the system to open. Python can do system calls, so you can tell the OS to ‘open this file.’ You can do this with pythons ‘os.startfile(path)’ command. This will open it in an external program (eg Adobe Reader)

Thank yo! Now I know it’s possible but sadly I’m not that good with python.
I tried doing this but the console said the file couldn’t be found

import bge
from bge import logic
import os

os.startfile(“file.pdf”)

make sure your file path is correct

Notice that the CWD (current directory) for os.startfile is not the directory where the .blend is found but where the blenderplayer is found. If you export the game by the classic methods that will be the same of the .blend, but before exporting the game that is the directory where blender is installed.

You have two solutions. The fast yet broken solution is to use absolute paths. Of course you won’t be to run the game on another computer if you do this.

The second solution is to change the CWD. You can do this with python. If you don’t want to do it you can also generate absolute paths based on the .blend absolute path (from the BGE API) but it’s the same result and not easier to implement.

Just like elmeunick9 says, it’s to do with where pythons working directory is. The trivial solution that looks in the same directory as the blend file (aka what you want 99% of the time for small projects) is:


import bge
import os

os.startfile(bge.logic.expandPath('//file.pdf'))