简体   繁体   中英

How to silently print a pdf from a servlet output in a web application

In struts1 web application, we have an itextpdf generator that write directly pdf in the struts servlet output stream. We use itextepdf to generate a receipt, so we need to print the generated pdf without showing it to the user . The user should not be able to download the receipt to avoid to print the same receipt twice. What's the best way to accomplish this? The code to serve the pdf is like that :

public ActionForward printReceipt(final ActionMapping mapping,
        final ActionForm form, final HttpServletRequest request,
        final HttpServletResponse response) throws Exception {
    final OutputStream out = response.getOutputStream();
    final Document doc = new Document();
    final PdfWriter docWriter = PdfWriter.getInstance(doc, out);
    doc.open();
    final PdfContentByte cb = docWriter.getDirectContent();
    cb.beginText();
    // .. write all required data in the pdf
    doc.close();
    docWriter.close();
    out.flush();
    out.close();
    return mapping.findForward(null);
}

You may try following steps for a trick:

  1. include iframe pointing to the servlet you mentioned.
  2. Hide the iframe by css. ie display:none;
  3. Give print command from main html using

    window.frames["printf"].print(); where printf is the id of iframe

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM