Error executing java program for printing through PHP

1

I have a web application that generates an image for a badge and to be able to send it automatically to print, I created a program in java that does this.

The program runs perfectly, I generated a jar, pasted together with the php files inside the WAMP www, when I open the cmd, I navigate to this folder and execute the following ...

java -jar crachaPrinter.jar imagem.png

The java program runs perfectly and sends it to print, everything happens right ...

But when I put my php script in the following line ...

exec("java -jar crachaPrinter.jar imagem.png");

The program in java returns the following error:

  

Apr 01, 2014 4:26:27 PM crachaprinter.CrachaPrinter main Grave: null java.awt.print.PrinterException: No print service found. at sun.print.RasterPrinterJob.print (Unknown Source) at sun.print.RasterPrinterJob.print (Unknown Source) at crachaprinter.CrachaPrinter.main (CrachaPrinter.java:49)

On requests, snippet of the code where the error is called:

try {
        printJob.print();
    } catch (PrinterException ex) {
        Logger.getLogger(CrachaPrinter.class.getName()).log(Level.SEVERE, null, ex);
    }

I do not understand why the same .jar file encounters a print service when executed directly in cmd and not when I call the function by PHP exec, what can be done?

I need that when the page generates the image, it calls this script to send automatically to print, this was the way I found it since PHP does not do this natively currently, I already used exec () to execute programs in this way, but I've never had this kind of problem.

    
asked by anonymous 01.04.2014 / 21:39

2 answers

1

Instead of using exec() that can cause a security flaw in your code, it is not better with PHP to redirect to a page containing the image and use the window.print() method of javascript to be able to print of the same?

$imagem = //obter imagem;
<img src="$imagem.path" />
<script language="text/javascript">
  window.print()
</script>

PS: Of course you do not have to do everything on the same page or with HTML embedded JS and etc., I just wanted to give you a short example of how you could do it.

    
09.05.2014 / 22:23
0

It may be bullshit but will not this error stem from the fact that the file you are trying to create is outside the server computer?

If this is the problem create a temporary files folder inside the server in it will contain the files that will be sent to the client, make a code that has been downloaded from the client side after downloading the file, you deletes the same. If you do not want to eliminate errors in deleting the file you can program it to delete it anyway at a certain time

    
15.05.2015 / 15:47