簡體   English   中英

如何從Web應用程序中的servlet輸出中靜默打印pdf

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

在struts1 Web應用程序中,我們有一個itextpdf生成器,可直接將pdf寫入struts servlet輸出流。 我們使用itextepdf生成收據,因此我們需要在不向用戶顯示的情況下打印生成的pdf。 用戶應該不能下載收據,以避免兩次打印相同的收據。 做到這一點的最佳方法是什么? 服務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);
}

您可以嘗試按照以下步驟操作:

  1. 包含指向您提到的servlet的iframe。
  2. 通過CSS隱藏iframe。 即顯示:無;
  3. 使用以下命令從主html提供打印命令

    window.frames["printf"].print(); 其中printf是iframe的ID

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM