3

I need a little help here with my PDF converter program.

So, I'm doing this mobile agent PDF converter using JADE framework. But, the problem that I am facing is more related to the way I convert a text file into PDF, send it across network as binary and restore PDF file back.

The program that I've written works properly on my MacBook. But, on a Windows, it restores my PDF file as an empty PDF.

Here is my code that I use for sending the PDF file across.

private void sendPDF(File f, String recipient) {
    String content = "";

    if(f != null) {
        try {
            FileInputStream fis = new FileInputStream(f);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            int noBytesRead = 0;
            byte[] buffer = new byte[1024];

            while((noBytesRead = fis.read(buffer)) != -1) {
                baos.write(buffer, 0, noBytesRead);
            }

            content = baos.toString();
            fis.close();
            baos.close();

            System.out.println("Successful PDF-to-byte conversion.");
        } catch (Exception e) {
            System.out.println("Exception while converting PDF-to-byte.");
            content = "failed";
            e.printStackTrace();
        }
    } else {
        System.out.println("PDF-to-file conversion failed.");
        content = "failed";
    }

    ACLMessage message = new ACLMessage(ACLMessage.INFORM);
    message.addReceiver(new AID(recipient, AID.ISLOCALNAME));
    message.setContent(content);

    myAgent.send(message);
    System.out.println("PDF document has been sent to requesting client.");
}

And, here is the code that I use to restore the PDF back.

private File restorePDF(String content) {
    String dirPDF = dirBuffer + "/" + new Date().getTime() + ".pdf";
    File f = new File(dirPDF);

    try {
        if(!f.exists()) f.createNewFile();

        byte[] buffer = new byte[1024];
        ByteArrayInputStream bais = new ByteArrayInputStream(content.getBytes());
            FileOutputStream fos = new FileOutputStream(f);

        int noBytesRead = 0;
        while((noBytesRead = bais.read(buffer)) != -1) {
                fos.write(buffer, 0, noBytesRead);
    }

        fos.flush();
        fos.close();
        bais.close();
    } catch (Exception e) {
        e.printStackTrace();
        f = null;
    }

    return f;
}

Any help on this would be much appreciated! :)

Mike Causer
  • 8,196
  • 2
  • 43
  • 63
hsoetikno
  • 123
  • 1
  • 1
  • 3

3 Answers3

1

Question is a little bit confusing, since there is nothing specific about PDF content.

I am supposing you actually want to send bytes, actually send a string, and the string encoding is different on the client and server.

This is usually where troubles happen:

 content = baos.toString();

and:

 content.getBytes()
Nicolas Modrzyk
  • 13,961
  • 2
  • 36
  • 40
  • I think that's it. Combined with the fact that reading in a PDF and then converting it into a string seems very risky even if the encoding matches on both ends. @hsoetikno: Cannot you just write bytes? If not, maybe use BASE64 or something like that. – Thilo Aug 29 '11 at 02:30
1

A PDF file is a binary file format with lookup tables and lots of binary data blocks to making it a String will break it. If you want to know about the insides of a PDF file, I have written a whole load of blog posts about it (http://www.jpedal.org/PDFblog/2010/09/understanding-the-pdf-file-format-series/)

mark stephens
  • 3,205
  • 16
  • 19
0

One issue is that you're using the wrong separator char. Java has a built in function that will return the correct char for the correct os. See separator char.

Your code will look something like this

String dirPDF = dirBuffer + File.separatorChar + new Date().getTime() + ".pdf";

For reference:

separatorChar

The system-dependent default name-separator character. This field is initialized to contain the first character of the value of the system property file.separator. On UNIX systems the value of this field is '/'; on Microsoft Windows systems it is '\'.

Preston
  • 3,273
  • 4
  • 26
  • 35