1

Hi my code is as follows:

public StreamedContent getTempPdfFile() throws IOException {
    File testPdfFile = new File("D:\\AFC150_20180819_0103.pdf");
    streamedContent = new DefaultStreamedContent(new FileInputStream(testPdfFile), "application/pdf",
            "AFC150_20180819_0103.pdf");
    return streamedContent;
}
<h:panelGrid columns="1" cellpadding="5">
    <p:media value="#{realReport.tempPdfFile}" player="pdf" width="1000px" height="300px">
        <f:param name="id"  />
    </p:media>
</h:panelGrid>

But the PDF file is not getting displayed on the page.

Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
Vasu Sharma
  • 31
  • 1
  • 5

2 Answers2

1

Here is my sample, hope it helps, the code has the inline and attachment:

void sendBackPDFToClient()
{
        //File temp = File.createTempFile(fileName, ".pdf");
        File testPdfFile = new File("D:\AFC150_20180819_0103.pdf");
        FacesContext fc = FacesContext.getCurrentInstance();
        ExternalContext ec = fc.getExternalContext();

        ec.responseReset(); 
        ec.setResponseContentType("application/pdf"); 
        ec.setResponseContentLength((int)testPdfFile.length()); 

        //Inline
        //ec.setResponseHeader("Content-Disposition", "inline; filename=\"" + testPdfFile.getName() + "\""); 

        //Attach for Browser
        ec.setResponseHeader("Content-Disposition", "attachment; filename=\"" + testPdfFile.getName() + "\""); 

        OutputStream output = ec.getResponseOutputStream();
        Files.copy(testPdfFile.toPath(), output);
        fc.responseComplete();


}
Vinh Can Code
  • 407
  • 3
  • 14
0

Have you tried the Document Viewer component of PrimeFaces Extensions?

I think its much easier to use than p:media and offers more features including ability to load it from a URL, a Stream, or a Resource. That basic example shows you how to do it with all 3 types.

JAVA:

public StreamedContent getTempPdfFile() throws IOException {
     File testPdfFile = Paths.get("D:\\AFC150_20180819_0103.pdf").toFile();
     return new DefaultStreamedContent(new FileInputStream(testPdfFile), "application/pdf",
                "AFC150_20180819_0103");
}

XHTML:

<pe:documentViewer height="500" width="1000" value="#{realReport.tempPdfFile}"/>
Melloware
  • 10,435
  • 2
  • 32
  • 62
  • I tried using document viewer but i am getting this error: Unexpected server response (403) while retrieving PDF – Vasu Sharma Aug 22 '18 at 07:23
  • 403 is a security exception that means you don’t have permission to that url. Can you paste your. Sample code you used so I can see it? – Melloware Aug 22 '18 at 10:07