• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

iText Silent Print

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
     
    Megan Schanks
    Greenhorn
    Posts: 3
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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?
     
    Ranch Hand
    Posts: 96
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Rancher
    Posts: 43081
    77
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 96
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Posts: 96
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Rancher
    Posts: 43081
    77
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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?
     
    Talk sense to a fool and he calls you foolish. -Euripides A foolish tiny ad:
    We need your help - Coderanch server fundraiser
    https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
    reply
      Bookmark Topic Watch Topic
    • New Topic