Printing Problem

R

ruds

Hi,
I'm developing a web application in JSP on Tomcat5.5 as server.
What I want is few documents(.doc, .ppt files) are present in a
specific directory when the manager approves them they should get
converted into PDF's in the background.
I have downloaded a free pdf converter(PDFCreator.exe) for this. If I
call this through astandalone java program using Runtime class it
works perfectly, but when I'm calling the same java program through a
JSP (as I want to convert documents at runtime) then the external
program is called but does not give output.
1. I have tried calling cmd and the the PDFCreator but no result is
seen.
2. I have also tried using JPS API and tried still no output is
generated.
please help me, I have been searching for the solution for a long
time.
 
C

charlesbos73

1. I have tried calling cmd and the the PDFCreator but no result is
seen.
2. I have also tried using JPS API and tried still no output is
generated.
please help me, I have been searching for the solution for a long
time.

Please stop spamming this group with this "problem" of yours.

I've asked you many questions and I gave you lots of hints
as to what you could do to try to find what's wrong, but
obviously you refuse to apply them and you keep posting
basically the exact same message to this group.

The problem is that you don't have the mindset needed
to solve the many problems you'll encounter during your
programming career.

Either you change your mindset or you look for another
job...

Make a script (like a Windows batch script [a .bat file])
that wraps your PDF Creator call. Make that script take
no input at all. Use it with a default file to try at
first.

1. Does it work when you call the *script* manually ?

2. Does it work when you call that same script from
a standalone Java app using Runtime.exec ?

3. Does it work when you call that same script from
your JSP/Servlet ?

please help me, I have been searching for the
solution for a long time.

No you haven't. You're putting your head in the
sand, you're not listening to advice, you're not
answering questions.

All you've done is posting this same pathetic
message to this newsgroup hoping that someone
would come with a magical fix.

There are no stupid jobs in this world.

I suggest flipping burgers.
 
R

ruds

Make a script (like a Windows batch script [a .bat file])
that wraps your PDF Creator call.  Make that script take
no input at all.  Use it with a default file to try at
first.

Yes I have made a batch file with default file.
1. Does it work when you call the *script* manually ?

It works perfectly when called manually.
2. Does it work when you call that same script from
a standalone Java app using Runtime.exec ?

gives correct output when called through a standalone Java app using
Runtime.exec.
3. Does it work when you call that same script from
your JSP/Servlet ?
PDF creator is called but does not give output... nor writes anything
in the output file.

I have tried all the things. ikts not that I haven't. just that I dont
have anyone to guide me here, no ones from programming background that
I'm get stuck.

Now, If I'm doing the same thing using JPS a blank file is being
created.
My revised code:

package pack;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterJob;
import java.io.FileInputStream;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.DocAttribute;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.JobName;
import javax.print.attribute.standard.OrientationRequested ;

public class Printjob {
public static void main(String[] args)
{
String filename = "C:\\1.doc" ;
new Printjob().printPDFPages(filename);
}

public void printPDFPages(String fileLocation) {
try {
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet
();
pras.add(OrientationRequested.PORTRAIT);
pras.add(new Copies(1));
pras.add(new JobName(fileLocation, null));

PrintService ps1 = null;
PrintService pss[] = PrintServiceLookup.lookupPrintServices(null,
null);

String printerName = "PDFCreator";

for (int i = 0; i < pss.length; i++) {
if (pss.getName().equalsIgnoreCase(printerName)) {
ps1 = pss;
break; }
}
System.out.println("Default printer: "+ps1.getName());
System.out.println("Printing to " + ps1);
System.out.println("Filename " + fileLocation);

DocAttributeSet attributeSet = new HashDocAttributeSet ();
FileInputStream fin = new FileInputStream(fileLocation);
Doc doc = new SimpleDoc(fin,
DocFlavor.INPUT_STREAM.AUTOSENSE,null);
ps1.createPrintJob().print(doc, pras);
fin.close();
Thread.sleep(30000);
System.out.println("All Done !!");
} catch (Exception ie) {ie.printStackTrace();}
}
}

A blank pdf file is created with name as .pdf and not 1.pdf as
required when I execute the program.
 
A

Andrew Thompson

DocFlavor.INPUT_STREAM.AUTOSENSE,null);
   ps1.createPrintJob().print(doc, pras);

Try *not* closing the FileInputStream.
   fin.close();

"Print service implementors should close any print data streams (ie
Reader or InputStream implementations) that they obtain from the
client doc. ..."
 
R

ruds

I tried by calling the batch file too, the bat file runs ok when
executed.It also creates pdf when called from a standalone java app,
but fails to give output when the bat file is called through servlet.
My code when called through servlet:

p = Runtime.getRuntime().exec("cmd /c C:\\test.bat");
int iwaitfor = 240; //2 mins
while(iwaitfor > 0) //wait till thread dead or "iwaitfor" in time.
iwaitfor--;
System.out.println("Sleep started");
Thread.sleep(3000);
System.out.println("Sleep ended");
p.destroy();
stdInput = new BufferedReader(new InputStreamReader(p.getInputStream
()));

Even then the PDFCreator is just seen running without giving output
when called through servlet.
 
L

Lew

ruds said:
I tried by calling the batch file too, the bat file runs ok when
executed.It also creates pdf when called from a standalone java app,
but fails to give output when the bat file is called through servlet.
My code when called through servlet:

p = Runtime.getRuntime().exec("cmd /c C:\\test.bat");
int iwaitfor = 240; //2 mins
while(iwaitfor > 0) //wait till thread dead or "iwaitfor" in time.
iwaitfor--;

I don't know what you mean by "mins" in the comment, but that loop might take
literally no time at all, depending on what the optimizer does with it. At
most, on modern systems I'd expect it to take about a fraction of a
microsecond to execute.

FWIW, "iwaitfor" does not conform to the Java convention for variable names.
System.out.println("Sleep started");
Thread.sleep(3000);
System.out.println("Sleep ended");
p.destroy();

Perhaps three seconds isn't long enough for the execution of the batch file to
complete?

You'd be better off actually waiting for the execution than guessing about the
timing.

stdInput = new BufferedReader(new InputStreamReader(p.getInputStream
()));

Even then the PDFCreator is just seen running without giving output
when called through servlet.

Interestingly, you kill the subprocess before you try to read its output.
I've not used 'Process', but that seems suspect to me.
 
J

John B. Matthews

[...]
Perhaps three seconds isn't long enough for the execution of the
batch file to complete?

You'd be better off actually waiting for the execution than guessing
about the timing.



Interestingly, you kill the subprocess before you try to read its
output. I've not used 'Process', but that seems suspect to me.

Same here. I've used Process, although never from a servlet. Out of
curiosity, I tried it. No problem, as long as one resists the temptation
to make "cmd" a request parameter:

<code>
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

/** @author John B. Matthews */

public class Test extends HttpServlet {

public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Exec";
out.println("<html>");
out.println("<head>");
out.println("<title>" + title + "</title>");
out.println("</head>");
out.println("<body bgcolor=\"white\">");
out.println("<h2>" + title + ": " + new Date() + "</h2>");
doExec("ls", out);
out.println("</body>");
out.println("</html>");
}

private void doExec(String cmd, PrintWriter out) {
String s;
try {
Process p = Runtime.getRuntime().exec(cmd);
// read from the process's stdout
BufferedReader stdout = new BufferedReader (
new InputStreamReader(p.getInputStream()));
while ((s = stdout.readLine()) != null) {
out.println(s + "<br>");
}
// read from the process's stderr
BufferedReader stderr = new BufferedReader (
new InputStreamReader(p.getErrorStream()));
while ((s = stderr.readLine()) != null) {
out.println(s + "<br>");
}
p.getInputStream().close();
p.getOutputStream().close();
p.getErrorStream().close();
out.println("Exit value: " + p.waitFor() + "<br>");
}
catch (Exception e) {
e.printStackTrace(out);
}
}
}
</code>
 
L

Lew

Same here. I've used Process, although never from a servlet. Out of
curiosity, I tried it. No problem, as long as one resists the temptation
to make "cmd" a request parameter:

I note that you do not kill the subprocess prior to attempting to read its
output, that you do not use a timed wait, and that you do use
'Process#waitFor()', the opposite of the three points in the OP's code that
worried me.
 
J

John B. Matthews

[QUOTE="Lew said:
Same here. I've used Process, although never from a servlet. Out of
curiosity, I tried it. No problem, as long as one resists the
temptation to make "cmd" a request parameter:

I note that you do not kill the subprocess prior to attempting to
read its output, that you do not use a timed wait, and that you do
use 'Process#waitFor()', the opposite of the three points in the OP's
code that worried me.[/QUOTE]

Good points. Misusing destroy() is one I actually tried. I got an
informative stack trace showing a java.io.IOException, the method name,
and the line number where things went awry:

java.io.IOException: ... at Test.doExec(Test.java:35) at ...

The OP might want to verify that no diagnostic output has been discarded
in testing. In addition, the ant <reload/> task can ensure that current
classes are being used. As you and others have suggested, only a
methodical approach will prevail.
 
R

ruds

Even if I remove those statements that you all are wooried about,and
use p.waitfor(); does not change anything.
I.e I still see the PDFCreator running for a long time with no output
being generated.
 
L

Lew

ruds said:
Even if I remove those statements that you all are wooried about,and
use p.waitfor(); does not change anything.

Do you mean 'p.waitFor()'?
I.e [sic] I still see the PDFCreator running for a long time with no output
being generated.

Is the task waiting for input?
 
R

Roedy Green

I'm developing a web application in JSP on Tomcat5.5 as server.
What I want is few documents(.doc, .ppt files) are present in a
specific directory when the manager approves them they should get
converted into PDF's in the background.
I have downloaded a free pdf converter(PDFCreator.exe) for this. If I
call this through astandalone java program using Runtime class it
works perfectly, but when I'm calling the same java program through a
JSP (as I want to convert documents at runtime) then the external
program is called but does not give output.
1. I have tried calling cmd and the the PDFCreator but no result is
seen.
2. I have also tried using JPS API and tried still no output is
generated.
please help me, I have been searching for the solution for a long
time.

This is because a spawned program directs its System.out output back
to mother. see http://mindprod.com/jgloss/exec.html

Mom has to do something with it. Alternatively spawn a command
interpreter and have it redirect the output and invoke pdfcreator.exe
--
Roedy Green Canadian Mind Products
http://mindprod.com

"Species evolve exactly as if they were adapting as best they could to a changing world, and not at all as if they were moving toward a set goal."
~ George Gaylord Simpson
 
R

ruds

To Lew:
Is the task waiting for input?
I'm giving the input file file as the argument to pdfcreator.exe.

To Roedy Green:
This is because a spawned program directs its System.out output back
to mother.  seehttp://mindprod.com/jgloss/exec.html

Mom has to do something with it.  Alternatively spawn a command
interpreter and have it redirect the output and invoke pdfcreator.exe

Can you explain in more detail please?
I'm calling the command interpreter which in turns calls the
pdfcreator.exe.

p = Runtime.getRuntime().exec("cmd /c C:\\test.bat");

My test.bat code is:
@echo off
start C:\\"Program Files"\\PDFCreator\\PDFCreator.exe /NOSTART /PFC:\
\1.doc
echo File created.
 
L

Lew

ruds said:
My test.bat code is:
@echo off
start C:\\"Program Files"\\PDFCreator\\PDFCreator.exe /NOSTART /PFC:\
\1.doc
echo File created.

Why the extra backslashes in the .bat file?
 
R

ruds

Why the extra backslashes in the .bat file?

The extra backslashes are used since single backslash is considered as
special character and not as separator.
 
A

Andrew Thompson

The extra backslashes are used since single backslash is considered as
special character and not as separator.

That is required when *Java* is parsing the
String. In this case, it is being parsed by
the Windows CLI.
 
R

ruds

In a .bat file?  Are you quite certain?

Oops, sorry.
Ok I removed that and now I'm getting the proper output for the sample
file.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top