Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Printing Problem

2,017 views
Skip to first unread message

ruds

unread,
May 8, 2009, 1:50:27 AM5/8/09
to
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.

charlesbos73

unread,
May 8, 2009, 8:42:01 AM5/8/09
to
On May 8, 6:50 am, ruds <rudra...@gmail.com> wrote:
...

> 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.

ruds

unread,
May 8, 2009, 11:23:51 PM5/8/09
to
> 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[i].getName().equalsIgnoreCase(printerName)) {
ps1 = pss[i];
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.

Andrew Thompson

unread,
May 8, 2009, 11:38:11 PM5/8/09
to
On May 9, 1:23 pm, ruds <rudra...@gmail.com> wrote:
...
> 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. ..."

--
Andrew T.
pscode.org

ruds

unread,
May 9, 2009, 12:33:28 AM5/9/09
to
> > DocFlavor.INPUT_STREAM.AUTOSENSE,null);
> >    ps1.createPrintJob().print(doc, pras);
>
> Try *not* closing the FileInputStream.
>
> >    fin.close();

Tried it still a blank file is created.

ruds

unread,
May 9, 2009, 12:56:22 AM5/9/09
to
I have tried changing the printer too, without any change in the
result.

ruds

unread,
May 9, 2009, 5:58:57 AM5/9/09
to
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.

Lew

unread,
May 9, 2009, 3:09:19 PM5/9/09
to
ruds wrote:
> 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.

<http://java.sun.com/javase/6/docs/api/java/lang/Process.html#waitFor()>

> 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.

--
Lew

John B. Matthews

unread,
May 9, 2009, 4:42:36 PM5/9/09
to
In article <gu4kd0$i8a$1...@news.albasani.net>, Lew <no...@lewscanon.com>
wrote:

[...]


> 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.
>
> <http://java.sun.com/javase/6/docs/api/java/lang/Process.html#waitFor()>
>
> > 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.

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>

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

Lew

unread,
May 9, 2009, 5:58:00 PM5/9/09
to
Lew wrote:
>> Interestingly, you kill the subprocess before you try to read its
>> output. I've not used 'Process', but that seems suspect to me.


John B. Matthews wrote:
> 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.

--
Lew

John B. Matthews

unread,
May 10, 2009, 12:37:38 PM5/10/09
to
In article <gu4u98$v2d$1...@news.albasani.net>, Lew <no...@lewscanon.com>
wrote:

> John B. Matthews wrote:
> > 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.

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.

ruds

unread,
May 10, 2009, 11:47:12 PM5/10/09
to
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.

Lew

unread,
May 11, 2009, 12:33:02 AM5/11/09
to
ruds wrote:
> 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?

--
Lew

Roedy Green

unread,
May 11, 2009, 7:06:02 AM5/11/09
to
On Thu, 7 May 2009 22:50:27 -0700 (PDT), ruds <rudr...@gmail.com>
wrote, quoted or indirectly quoted someone who said :

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

ruds

unread,
May 12, 2009, 12:03:01 AM5/12/09
to
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.

Lew

unread,
May 12, 2009, 7:42:24 PM5/12/09
to
ruds wrote:
> 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?

--
Lew

ruds

unread,
May 16, 2009, 12:48:20 AM5/16/09
to
> 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.

Andrew Thompson

unread,
May 16, 2009, 12:56:23 AM5/16/09
to
On May 16, 2:48 pm, ruds <rudra...@gmail.com> wrote:
(Lew writ)

> > 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.

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

--
Andrew T.
pscode.org

Lew

unread,
May 16, 2009, 1:42:45 AM5/16/09
to
(Please attribute quotes) Lew wrote:
>> Why the extra backslashes in the .bat file?

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

In a .bat file? Are you quite certain?

--
Lew

ruds

unread,
May 16, 2009, 7:24:05 AM5/16/09
to
> 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.

0 new messages