0

I'm attempting to pass a large/long string, a PDF binary read from disk, into a Java app and so far am having very little success. The app works when I read the file in from a local disk, so I know the problem is related to how I'm importing the data. Roughly this is what I'm looking at:

I'm reading, manipulating the pdf in PHP and using exec(); to touch the Java app, this is where I'm at with the Java:

Works:

File input = new File("C:\\Users\\Jack\\Downloads\\col_terror.pdf");
document = PDDocument.load(input);

Does not work: PHP:

exec("/path/to/jar/java -jar JavaAppHere.jar $pdf_string",$ouput);

Java:

public static void main(String[] args) throws Exception {
...
document = PDDocument.load( args[0] );
...
}

I feel this is something quite simple I am not understanding about passing strings as args, though it has been a couple years since I've made a venture into the land of Java.

jbrain
  • 561
  • 3
  • 7
  • 18
  • 4
    What is the error you receive? What happens when you run the java code through the debugger? What is the value of `args[0]`? – Matt Aug 10 '12 at 14:22
  • I'm not running this through a debugger, I'm testing it on the dev machine since I need to see how it behaves within the existing app, there are no errors thrown that I can see and args[0] is the binary pdf string. – jbrain Aug 10 '12 at 14:46
  • Java `args` is expecting a string in, probably, UTF-16. A binary PDF is not a string in any encoding. You need to load the PDF either as binary, or into some library method that is expecting a PDF. The `args[]` array should contain text. In your case, probably just the filename of the PDF, with the actual retrieval being done within the `main()` method. – rossum Aug 10 '12 at 14:56
  • rossum: I didn't run into problems loading the PDF from a local file, but the PHP code and Java code reside on different machines so I can't read the file locally, which is why I was trying to pass the PDF, as read from disk by the PHP routine, directly to the Java app and have it return the results. A bit more difficult than I had imagined it would be. – jbrain Aug 10 '12 at 15:07

1 Answers1

0

Please read the Javadoc of PDDocument. If PDDocument refers to PDDocument you can see that you are passing data while PDDocument.load(java.lang.String) expects the filename. You also don't seem to encode $pdf_string with escapeshellarg()

Since there are too many variables invovled in passing binary data around as shell arguments, I'd advice against it. The easiest solution is to write the PDF to a temporary file and pass the filename to java. The alternative is to pass the PDF data via stdin.

poison
  • 189
  • 3