Error running java program for printing through PHP

Asked

Viewed 325 times

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 java program that does this.

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

java -jar crachaPrinter.jar imagem.png

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

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

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

The java program 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)

A request, excerpt from the code where the error is called:

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

I don’t understand why the same file. jar finds a print service when executed directly in cmd and not when calling the function by PHP exec, which can be done?

I need that when the page generates the image, it calls this script to automatically send for printing, that’s the way I found since PHP doesn’t do it natively currently, I’ve already used exec() to run programs like this, but I’ve never had this kind of trouble.

  • what’s on line 49 of Crachaprinter.java?

  • It is the Try that executes the function to execute the Pronterjob

  • could edit your question by adding this snippet of Try code?

  • 1

    When you open the CMD, you run the jar as an OS user. Is apache running with that same user? Checked if this could be an OS access permissions issue?

  • @Ricardogiaviti I have not checked anything in this regard, I will take a look at what I can get out of it and I will give back. I need to make this work for yesterday haha :(

  • Which OS are you using? Is the goal to print to a printer connected to the server or client? What type of printer (USB, Parallel, Serial, uses native driver, or needs a legacy driver)? I have written several applications making PHP+Printer folding and each scenario has its details... answer these questions, maybe I can help.

  • Friend, you could put the java code of the print?

  • Bah Master, I haven’t had this code in a long time, sorry.

Show 3 more comments

2 answers

1

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

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

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

0

It might be bullshit else is that this error is not born from the fact that the file you are trying to create is off the server computer?

If this is the problem create a temporary folder of files inside the server in it will contain the files that will be sent to the client, make a code that will download the file from the client side after you finish downloading the file, you delete it. In case you do not want to miss the function of deleting the file you can program to delete it anyway at a certain time

Browser other questions tagged

You are not signed in. Login or sign up in order to post.