neckbeard69
neckbeard69

Reputation: 21

Generating a PDF in Java

I'm trying to write a PDF file in Java to say the words hello neckbeards but when I run my program, Adobe Reader opens but an error comes up saying:

There was an error opening this document.
The file is already open or in use by another application.

Here's my code:

import java.awt.Desktop;
import java.io.*;

public class count10 {

    public static void main(String[] args) throws Exception {

        File tempfile = File.createTempFile("report", ".pdf");
        FileWriter pfile = new FileWriter(tempfile);
        pfile.write("hello neckbeards");

        Desktop dtop = null;

        if (Desktop.isDesktopSupported()) {
            dtop = Desktop.getDesktop();
        }
        if (dtop.isSupported(Desktop.Action.OPEN)){
            String path = tempfile.getPath();
            dtop.open(new File(path));
        }
    }
}

Upvotes: 1

Views: 5607

Answers (5)

Kedar
Kedar

Reputation: 167

    Try this code....
    Document document=new Document();
    PdfWriter.getInstance(document,new FileOutputStream("E:/data.pdf"));
    document.open();
    PdfPTable table=new PdfPTable(2);
               table.addCell("Employee ID");
               table.addCell("Employee Name");
               table.addCell("1");
               table.addCell("Temperary Employee");
               document.add(table);
               document.close();

You have to import....
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

Upvotes: 0

Alba Mendez
Alba Mendez

Reputation: 4635

There are many errors here:

  • You are writing plain text. Adobe Reader will throw an error as the file is not a valid PDF!
    To write PDFs, use libraries like iText or PDFBox.

  • Before you can write or read a file, you open a connection from your program to the file.
    So, when you end writing/reading the file, don't forget to close the connection so that other programs (such as Adobe Reader) can read the file too! To close the file, simply do:

    pfile.close();
    
  • The main method shouldn't throw any exception. Instead, if an error occurs, it must be catched and do the appropiate actions (tell the user, exit, ...). To read/write files (or anything), this is the recommended structure:

    FileReader reader = null;
    try {
        reader = new FileReader("file.txt"); //open the file
    
        //read or write the file
    
    } catch (IOException ex) {
        //warn the user, log the error, ...
    } finally {
        if (reader != null) reader.close(); //always close the file when finished
    }
    
  • The final if has a bad place. The correct code would be:

    if (Desktop.isDesktopSupported()) {
        Desktop dtop = Desktop.getDesktop();
        if (dtop.isSupported(Desktop.Action.OPEN)) {
            dtop.open(tempfile);
        }
    }
    

    Also, notice that I call the open method passing the file directly.
    There's no need to make a copy of it.

Upvotes: 5

darioo
darioo

Reputation: 47193

After you write to your file, you have to close it, using pfile.close();

Note that what you wrote is just a text file with contents hello neckbeards and extension .pdf. It is not a PDF file in a normal sense that can be opened with a PDF reader like Adobe Reader.

Use a library like iText to create real PDF files.

A file must follow the PDF implementation (PDF file) to be a valid PDF file. As you can see, this is much more involved, than "just" writing text to a file.

Upvotes: 4

Conan
Conan

Reputation: 2358

I'm not familiar with opening files from the desktop in that way, but it's worth closing your FileWriter after writing the file. Incidentally, I've found XSLT to be a good way of producing PDF documents, as you get a great deal of control over the output and ongoing maintenance and adjustments don't involve re-compiling your code (useful if you have a marketing department who like changing their minds). Take a look at XSL-FO if you're interested, Apache FOP is a good implementation.

Upvotes: 0

Vincent Ramdhanie
Vincent Ramdhanie

Reputation: 103145

To create a PDF file you can use a library such as iText. It seems to me that you are simply creating a plain text file and then attempting to open it with a PDF reader.

Upvotes: 4

Related Questions