*
The moose likes Other Open Source Projects and the fly likes iText Silent Print Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "iText Silent Print" Watch "iText Silent Print" New topic
Author

iText Silent Print

Megan Schanks
Greenhorn

Joined: Feb 06, 2004
Posts: 3
I have a web application that has a requirement to silent print a generated pdf (the user should not even see the pdf). I tried basing my code on the sample code from the iText site:

Document document = new Document();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter writer = PdfWriter.getInstance(document, baos);
document.open();
writer.addJavaScript("this.print(true);", false);
document.add(new Paragraph("HELLO WORLD!"));
document.add(Chunk.NEWLINE);
document.add(new Paragraph("Is anyone out there?"));
document.close();

// setting some response headers
response.setHeader("Expires", "0");
response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
// setting the content type
response.setContentType("application/pdf");
// the content length is needed for MSIE!!!
response.setContentLength(baos.size());

// write ByteArrayOutputStream to the ServletOutputStream
ServletOutputStream out = response.getOutputStream();
baos.writeTo(out);
out.flush();

No luck, the pdf is generated (and opens which I don't want) and will not print!

Thanks in advance,
Megan
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41180
    
  45
Welcome to JavaRanch.

The code you posted doesn't print anything - it streams a PDF file to a browser. That the file contains code to print it under certain circumstances is irrelevant. A few things to consider in this situation:
  • Without opening the file there's no chance of it being printed, since the embedded print code depends on the file being opened.
  • Whether the file is opened is not under the control of the web app. The user may have set the browser to save PDF files w/o opening them.
  • If it is opened, it will be displayed. No way around that, at least not with common viewers like Adobe Reader or OS X Preview.
  • If the client PDF viewer supports JavaScript (which is not a given - it may be disabled, or a viewer may be used that doesn't support it), and the code runs, a print dialog will be displayed that will require the user to click at least one button. No way around that either.

  • Given these, I think what you're trying to achieve is not possible. I'd argue that's a good thing, too, since printing something on a client system w/o the user noticing it is not a nice thing to do. Maybe in your situation it's acceptable or even desirable, but in general it's not, which is why it doesn't work that way.

    What is the rationale for wanting to do this?
    [ April 03, 2008: Message edited by: Ulf Dittmer ]

    Ping & DNS - my free Android networking tools app
    Megan Schanks
    Greenhorn

    Joined: Feb 06, 2004
    Posts: 3
    Thanks for the response.

    This line adds JavaScript to the PDF that will "silent print", although it isn't working:
    writer.addJavaScript("this.print(true);", false);

    What my users are really looking for is the ability to generate a pdf and silent print without opening the pdf at all. This is a requirement because the users do not want duplicates of a pdf printed.

    Any ideas?
    Swerrgy Smith
    Ranch Hand

    Joined: Mar 26, 2010
    Posts: 88
    Hi,

    I am also interested in silent printing with iText.
    However I did not find any solution.
    The line writer.addJavaScript("this.print(true);",false); will add some Javascript to PDF file but we still have to open it, then a dialog will appear to ask whether we want to print or not.

    Do you have any idea?

    Thank you.
    Ulf Dittmer
    Marshal

    Joined: Mar 22, 2005
    Posts: 41180
        
      45
    Re-read my previous post. Being able to print something without asking the user for permission is a risk in several ways; that's why it's not possible.
    Swerrgy Smith
    Ranch Hand

    Joined: Mar 26, 2010
    Posts: 88
    Ulf Dittmer wrote:Re-read my previous post. Being able to print something without asking the user for permission is a risk in several ways; that's why it's not possible.

    There a guy who confirms that he can do slient printing with iText.
    That's why I am trying to repeat what he have been able to do.

    http://threebit.net/mail-archive/itext-questions/msg02068.html
    Swerrgy Smith
    Ranch Hand

    Joined: Mar 26, 2010
    Posts: 88
    Swerrgy Smith wrote:
    Ulf Dittmer wrote:Re-read my previous post. Being able to print something without asking the user for permission is a risk in several ways; that's why it's not possible.

    There a guy who confirms that he can do slient printing with iText.
    That's why I am trying to repeat what he have been able to do.

    http://threebit.net/mail-archive/itext-questions/msg02068.html


    Finally, I have found the solution, very simple indeed

    com.lowagie.tools.Executable.printDocumentSilent(MyFileName);

    Note: this work only with iText 1.4 and not with recent version.
    Ulf Dittmer
    Marshal

    Joined: Mar 22, 2005
    Posts: 41180
        
      45
    Swerrgy Smith wrote:com.lowagie.tools.Executable.printDocumentSilent(MyFileName);

    Note: this work only with iText 1.4 and not with recent version.

    Well, that method is present even in the latest LGPL version, which was 2.1.7.

    But you need to execute Java code in order to use that; how does that help a client that downloads a PDF?
     
    jQuery in Action, 2nd edition
     
    subject: iText Silent Print
     
    Similar Threads
    Generate pdf file and display in a browser
    PDF with jsf
    I am trying to generate a PDF file in Hibernate
    Cannot forward after response has been committed? PDF save in Browser?
    Cannot forward after response has been committed? PDF save in Browser?