2

I'm using a servlet to send a pdf stream to a browser.

There is a request that the pdf must open the print dialog when I show it to the user. I was successful doing this using iText api. Something like this:

stamper.setPageAction(PdfWriter.PAGE_OPEN, new PdfAction(PdfAction.PRINTDIALOG), 1);

Now I do need to close the pdf file after print. I tried using pdfactions, but I'm can't get it. What I tried is:

writer.setAdditionalAction(PdfWriter.DID_PRINT, PdfAction.javaScript("app.execMenuItem('Close');", writer));

or

writer.setAdditionalAction(PdfWriter.DID_PRINT, PdfAction.javaScript("app.close();", writer));

I don't necessarily need to use pdfActions, but I don't see how to get it after user sent the pdf to printer.

Do you have any ideas?

1 Answer 1

2

There's a reason why app.execMenuItem('Close'); and app.close() don't work. These methods are designed to close the standalone version of Adobe Reader/Acrobat. I guess you're viewing the PDF in a browser, in which case you use Adobe Reader as a plug-in, Chrome's PDF viewer, pdf.js in Firefox, or any other PDF viewer.

Problem #1: you need to close the browser window from a PDF document. PDFs don't have the power to control your browser. Suppose they would: wouldn't that be a serious security issue?

Problem #2: you embed the PDF inside a HTML page (e.g. using an <object> tag) and establish a communication between the JavaScript in the PDF and the JavaScript in the HTML. I've described how to do this in my book, but: it won't work with all browsers on all OSs. For instance: Chrome's PDF viewer and Firefox's pdf.js will completely ignore your commands.

You are asking a solution using our iText library, but you're asking something that can't be done with any software.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Bruno, for your answer. Regarding your points, I decided to try the link you point in Problem #2, since my target browser is IE8 (it's a requirement from client). However, I coudn't get it working. It seems that messageHandler is not catching the event message. I also ignored post_from_html.js from the example you gave, since I just want to get a message from PDF, and not the opposite. Can you give me any tip on this? Thanks.
Ok... I could figure it out. Point #2 was enough for my needs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.