0

我已经使用 IText 库创建了 pdf 现在我希望当用户单击按钮时,它应该向用户提示一个保存对话框,以允许浏览目标位置来存储文件。

现在我的代码看起来像

    Document document = new Document();
    try {

                    FacesContext context = FacesContext.getCurrentInstance();
        HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
        response.reset();  
        response.setContentType("application/pdf");

        response.setHeader("Content-Disposition", "attachment; filename=ABC.pdf");
        response.setHeader("Cache-Control", "no-cache");  
        response.setDateHeader("Expires", 0);  
        response.setHeader("Pragma", "No-cache");
        PdfWriter.getInstance(document, response.getOutputStream());

        document.addTitle("Form");
        document.addSubject("Form");

        document.open();

        Paragraph paragraph12 = new Paragraph("FORM",

        FontFactory.getFont(FontFactory.HELVETICA,

        18, Font.BOLD, new CMYKColor(0, 255, 255, 17)));

        Chapter chapter1 = new Chapter(paragraph12, 1);
        paragraph12.setSpacingAfter(25);
        paragraph12.setAlignment(Element.ALIGN_CENTER);
        document.add(paragraph12);
        // Centered

        Chunk underline = new Chunk(AppNo);
        underline.setUnderline(0.2f, -2f);

        Paragraph paragraph = new Paragraph("Application No:");
        paragraph.add(underline);
        paragraph.setAlignment(Element.ALIGN_LEFT);
        paragraph.setSpacingAfter(20);
        document.add(paragraph);
        // Left
        paragraph = new Paragraph("To,");
        paragraph.setAlignment(Element.ALIGN_LEFT);
        document.add(paragraph);
        // Left with indentation
        paragraph = new Paragraph(
                "");
        paragraph.setAlignment(Element.ALIGN_LEFT);
        document.add(paragraph);

        Paragraph paragraph2 = new Paragraph(
                "Subject: ");

        paragraph2.setSpacingAfter(15);
        paragraph2.setSpacingBefore(15);
        paragraph2.setAlignment(Element.ALIGN_LEFT);
        paragraph2.setIndentationRight(50);
        document.add(paragraph2);



        Paragraph paragraph3 = new Paragraph("I  ");
        String laName=deaclarationOpr.getDeclarationDetailsobj()
                .getLaName();
        if(laName!=null){
        Chunk name = new Chunk(laName);
        name.setUnderline(0.2f, -2f);
        paragraph3.add(name);
        }
        paragraph3.add("  request you to process the Application  ");
        paragraph3.add(underline);
        paragraph3.add("  for product  ");


        String productName=deaclarationOpr
                .getDeclarationDetailsobj().getProductName();
        if(productName!=null){
        Chunk product = new Chunk(productName);
        product.setUnderline(0.2f, -2f);
        paragraph3.add(product);
        }
        paragraph3
                .add("");

        paragraph3.setSpacingAfter(15);
        paragraph3.setSpacingBefore(15);
        paragraph3.setAlignment(Element.ALIGN_LEFT);
        paragraph3.setIndentationLeft(50);
        paragraph3.setIndentationRight(50);
        document.add(paragraph3);

        Paragraph paragraph4 = new Paragraph(
                "");





        String code=deaclarationOpr
                .getDeclarationDetailsobj().getAdvisorCode();
        if(code!=null){
        Chunk advisorCode = new Chunk(code);
        advisorCode.setUnderline(0.2f, -2f);
        paragraph4.add(advisorCode);
        }
        paragraph4
                .add("");

        paragraph4.setSpacingAfter(15);
        paragraph4.setSpacingBefore(15);
        paragraph4.setAlignment(Element.ALIGN_LEFT);
        paragraph4.setIndentationLeft(50);
        paragraph4.setIndentationRight(50);
        document.add(paragraph4);

        Paragraph paragraph5 = new Paragraph(
                "");

        paragraph5.setSpacingAfter(15);
        paragraph5.setSpacingBefore(15);
        paragraph5.setAlignment(Element.ALIGN_LEFT);
        paragraph5.setIndentationLeft(50);
        paragraph3.setIndentationRight(50);
        document.add(paragraph5);

        Paragraph paragraph6 = new Paragraph(
                "");

        paragraph6.setSpacingAfter(15);
        paragraph6.setSpacingBefore(15);
        paragraph6.setAlignment(Element.ALIGN_LEFT);
        paragraph6.setIndentationLeft(50);
        paragraph6.setIndentationRight(50);
        document.add(paragraph6);

        Paragraph paragraph7 = new Paragraph("Date:      ");
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        // get current date time with Date()
        Date date = new Date();
        paragraph7.add(dateFormat.format(date));
        paragraph7.setSpacingBefore(15);
        paragraph7.setIndentationLeft(50);
        paragraph7.setIndentationRight(50);
        paragraph7.setAlignment(Element.ALIGN_LEFT);
        document.add(paragraph7);

        Paragraph paragraph8 = new Paragraph("Name of the Proposer:      ");

        String propoer=deaclarationOpr
                .getDeclarationDetailsobj().getProposerName();
        if(propoer!=null){
        Chunk proposerName = new Chunk(propoer);
        proposerName.setUnderline(0.2f, -2f);
        paragraph8.add(proposerName);
        }
        paragraph8.setAlignment(Element.ALIGN_LEFT);
        paragraph8.setIndentationLeft(50);
        paragraph8.setIndentationRight(50);
        document.add(paragraph8);

        Paragraph paragraph9 = new Paragraph("Place:      ");
        paragraph9.setIndentationLeft(50);
        paragraph9.setIndentationRight(50);
        paragraph9.setAlignment(Element.ALIGN_LEFT);
        document.add(paragraph9);


        document.close();

context.responseComplete(); } catch (FileNotFoundException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } catch (DocumentException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); }

以下过滤器在 web.xml 中定义

<filter>
        <display-name>RichFaces Filter</display-name>
        <filter-name>richfaces</filter-name>
        <filter-class>org.ajax4jsf.Filter</filter-class>
        <init-param>
            <param-name>createTempFiles</param-name>
            <param-value>false</param-value>
        </init-param>
        <init-param>
            <param-name>maxRequestSize</param-name>
            <param-value>20000000</param-value>
        </init-param>
        <init-param>
            <param-name>enable-cache</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>richfaces</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>INCLUDE</dispatcher>
    </filter-mapping>

而上面的代码是写在Backing Bean类的方法中的

并调用

<tr>
<td align="center"><a4j:commandButton
value="Print" styleClass="button"
action="#{declarationBB.convertToPdf}"></a4j:commandButton></td>
</tr>

我在新页面下面

%PDF-1.4 %��3 0 obj <>stream x��V����Ժ@��rs�0��m|셒F6[�T���H[��E D'��7�� 
4

1 回答 1

0

只需将 PDF 写入 HTTP 响应而不是本地文件。

代替

PdfWriter.getInstance(document, new FileOutputStream("D:/Files/ABC.pdf"));

经过

FacesContext context = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment; filename=\"ABC.pdf\"");
PdfWriter.getInstance(document, response.getOutputStream());

// ... (your PDF building code here)

context.responseComplete();

并在与下载按钮关联的操作方法中调用它。Content-Disposition值为的标题attachment将强制进行另存为对话框(除非浏览器配置为在 Adob​​e Reader 中自动打开 PDF 文件!)。

也可以看看:


更新:根据您的更新,您正在通过 ajax 调用下载。

<a4j:commandButton value="Print" styleClass="button" action="#{declarationBB.convertToPdf}"></a4j:commandButton>

这行不通。您不能通过 ajax 下载文件。Ajax 由 JavaScript 执行,但由于明显的安全原因,它没有工具来以编程方式强制与某个变量中异步获取/生成的内容进行“另存为”对话。改为正常请求。改为使用<h:commandButton>

于 2013-01-07T12:38:46.660 回答