I want to be able to open a PDF file when the user clicks on 'HELP' in my application. The PDF file is located with the JAR, extracted to a tmp directory and then selected to open by awt.Desktop.getDesktop () (to allow for windows and linux use).
When I run the app from Eclipse then it works fine, the PDF opens with no errors. When I export to JAR and run then I get an error stating the 'PDF document is damaged', if I navigate manually to the PDF document (on my ubuntu machine /tmp/546434564.pdf) then I get the same error when I try to open the file. I am confused what is going on. The file size of the 'damaged' PDF is the same as the working one, so I dont think it is a permission issue.
The code I am using is:
public Main() throws FontFormatException, IOException {
//lets load the font
Font font = Font.createFont(Font.TRUETYPE_FONT, this.getClass().getResourceAsStream("/Coalition_v2.ttf")).deriveFont(Font.PLAIN, 14);
System.out.println(font);
//lets write the tmp file for help to the machine now
try {
java.io.InputStream iss = getClass().getResourceAsStream("/nullpdf.pdf"); //update the filename here when the help guide is written
byte[] data = new byte[iss.available()];
iss.read(data);
iss.close();
String tempFile = "file";
File temp = File.createTempFile(tempFile, ".pdf");
FileOutputStream fos = new FileOutputStream(temp);
fos.write(data);
fos.flush();
fos.close();
tmphelpfile = temp.getAbsolutePath();
System.out.println(tmphelpfile);
} catch (IOException ex) {
ex.printStackTrace();
System.out.println("TEMP FILE NOT CREATED - ERROR in tmp file writing");
}
And then to call the pdf:
JMenu mnHelpGuide = new JMenu("Help Guide");
mnHelpGuide.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// Help();
Desktop d = java.awt.Desktop.getDesktop ();
try {
System.out.println(tmphelpfile);
d.open (new java.io.File (String.valueOf(tmphelpfile)));
} catch (IOException e1) {
// TODO Auto-generated catch block
System.out.println("Couldnt open your file - error on HELP Guide");
e1.printStackTrace();
}
}
});