以编程方式将生成的PDF文件打开到浏览器选项卡中



我使用Apache PDFBox api创建了Pdf文件,点击打印按钮,如下所示。到目前为止,我将该文件保存到我的驱动器(文件系统)中。我需要的是将Pdf文件直接打开到浏览器中,而不是保存到驱动器中,以便可以根据需要打印或下载。

TestPdfBean.java

@ManagedBean(name = "pdfBean")
@ViewScoped
public class TestPdfBean implements Serializable {
    public void createAndOpenPdf() {
        PDDocument document = new PDDocument();
        PDPage page = new PDPage();
        document.addPage(page);
        PDFont font = PDType1Font.HELVETICA;
        PDPageContentStream content = new PDPageContentStream(doc,page);
        content.beginText();
        content.setFont(font, 12);
        content.moveTextPositionByAmount(100, 700);
        content.drawString(" Generating Pdf content...");
        content.endText();
        content.close();
        document.save("/home/ck/Test/test.pdf");
        document.close();
    }

}

test.xhtml代码片段

<h:form id="pdfForm">
     <p:panelGrid columns="2">
        <h:outputText value="Create Pdf file.." />
        <p:commandButton value="Print" actionListener="#{pdfBean.createAndOpenPdf}" />
    </p:panelGrid>
</h:form>

我已经将上面的portlet部署到liferay-portal-6.1.1中。

是否有办法通过Primefaces或Jsf或Liferay直接打开pdf文件到浏览器中?

我建议您看一下Liferay Faces为Liferay 6.1编写的jsf2-export-pdf-portlet演示。它有一个JSF 2的示例。

创建HttpServlet并在响应中返回PDF。然后只需将您的commandButton链接到servlet的URL,您就完成了…

如果返回ByteArrayOutputStream,浏览器将打开文档。

        `FacesContext context = FacesContext.getCurrentInstance();
        HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
        response.setContentType("application/pdf");
        response.setHeader("Content-disposition", "attachement;filename=Quote_.pdf");
        response.encodeRedirectURL("_blank");
        byte[] bytes = printPDFBean.createQuotationPDF(current, cfgDoc, cfgEntity, quoteDetailList, customer).toByteArray();
        response.getOutputStream().write(bytes);
        context.responseComplete();`

这段代码调用我的createQuotationPDF方法,它返回ByteArrayOutputStream对象。问题是,文档是在当前窗口打开的,而不是在新窗口打开的。

       `public ByteArrayOutputStream createQuotationPDF(Quotation currentQuote, CfgDoc cfgDoc, CfgEntity cfgEntity, List<QuotationDetail> quoteDetailList, Customer customer) {
        String filePath = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/");
        String imagesFolder = filePath + FacesContext.getCurrentInstance().getExternalContext().getInitParameter("smefin.images");
        String fontsFolder = filePath + FacesContext.getCurrentInstance().getExternalContext().getInitParameter("smefin.fonts");
        PDDocument doc = null;
        PDPage page = null;
        ByteArrayOutputStream output;
        output = new ByteArrayOutputStream();
        try {
            doc = new PDDocument();
            page = new PDPage();
            doc.addPage(page);
            File fontLatoBlackFile = new File(fontsFolder + "Lato-Black.ttf");
            File fontLatoLiteFile = new File(fontsFolder + "Lato-Light.ttf");
            Encoding encoding = new WinAnsiEncoding();
            PDFont fontLatoBold = PDTrueTypeFont.load(doc, fontLatoBlackFile, encoding);
            PDFont fontLatoLite = PDTrueTypeFont.load(doc, fontLatoLiteFile, encoding);
                content.beginText();
                content.setFont(fontLatoBold, 11);
                content.newLineAtOffset(50, 60);
                content.showText("Text");
                content.endText();
            }
            doc.save("D:/TestData/Quote_.pdf");
            doc.close();
        } catch (Exception e) {
            System.out.println("" + e);
        }
        return output;
    }`

相关内容

  • 没有找到相关文章

最新更新