How to display pdf document on jsf page in iFrame

Can someone help me display a PDF document on a JSF page only in iframe?

Thanks in advance,

Suresh

+3
source share
2 answers

Just use the <iframe>usual way:

<iframe src="/path/to/file.pdf"></iframe>

If your problem is more likely that the PDF is not located in WebContent, but rather located somewhere else in the disk file system or even in the database, then you basically need Servletwhich one InputStreamit receives and writes it in the OutputStreamresponse:

response.reset();
response.setContentType("application/pdf");
response.setContentLength(file.length());
response.setHeader("Content-disposition", "inline; filename=\"" + file.getName() + "\"");
BufferedInputStream input = null;
BufferedOutputStream output = null;

try {
    input = new BufferedInputStream(new FileInputStream(file), DEFAULT_BUFFER_SIZE);
    output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);

    byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
    int length;
    while ((length = input.read(buffer)) > 0) {
        output.write(buffer, 0, length);
    }
} finally {
    close(output);
    close(input);
}

This way you can simply point to this servlet instead: for example:

<iframe src="/path/to/servlet/file.pdf"></iframe>

You can find a complete example of a similar servlet in this article .

<iframe> JSF, , JSF 1.2 . JSF 1.1 HTML- , <iframe> <f:verbatim>, JSF, :

<f:verbatim><iframe src="/path/to/servlet/file.pdf"></iframe></f:verbatim>
+9
+1

Source: https://habr.com/ru/post/1726232/


All Articles