简体   繁体   English

如何从Web应用程序中的servlet输出中静默打印pdf

[英]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. 在struts1 Web应用程序中,我们有一个itextpdf生成器,可直接将pdf写入struts servlet输出流。 We use itextepdf to generate a receipt, so we need to print the generated pdf without showing it to the user . 我们使用itextepdf生成收据,因此我们需要在不向用户显示的情况下打印生成的pdf。 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 : 服务pdf的代码如下:

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. 包含指向您提到的servlet的iframe。
  2. Hide the iframe by css. 通过CSS隐藏iframe。 ie display:none; 即显示:无;
  3. Give print command from main html using 使用以下命令从主html提供打印命令

    window.frames["printf"].print(); where printf is the id of iframe 其中printf是iframe的ID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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