Issue displaying a PDF document using <image>

I am trying to display a pdf file using the <image> tag. I followed the advice from
[Show pdf from filesystem outside project directory]
Unfortunately, this example does not work. Just in case, I’m providing the project for error reproduction.

showPdfIssue.zip (4.0 MB)

If I try to display a large pdf file, roughly more than 2Mb, an error occurs (show errors). If the file is smaller, it shows icon like that…

2023-10-17T20:31:35.672+06:00 ERROR 515952 --- [nio-8080-exec-4] o.a.c.c.C.[.[.[/].[springServlet]        : Servlet.service() for servlet [springServlet] threw exception

java.io.IOException: Connection reset by peer
	at java.base/sun.nio.ch.FileDispatcherImpl.write0(Native Method) ~[na:na]
	at java.base/sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:62) ~[na:na]
	at java.base/sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:132) ~[na:na]
	at java.base/sun.nio.ch.IOUtil.write(IOUtil.java:97) ~[na:na]
	at java.base/sun.nio.ch.IOUtil.write(IOUtil.java:53) ~[na:na]
	at java.base/sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:532) ~[na:na]
	at org.apache.tomcat.util.net.NioChannel.write(NioChannel.java:118) ~[tomcat-embed-core-10.1.8.jar:10.1.8]
	at org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper.doWrite(NioEndpoint.java:1381) ~[tomcat-embed-core-10.1.8.jar:10.1.8]
	at org.apache.tomcat.util.net.SocketWrapperBase.doWrite(SocketWrapperBase.java:764) ~[tomcat-embed-core-10.1.8.jar:10.1.8]
	at org.apache.tomcat.util.net.SocketWrapperBase.writeBlocking(SocketWrapperBase.java:589) ~[tomcat-embed-core-10.1.8.jar:10.1.8]
	at org.apache.tomcat.util.net.SocketWrapperBase.write(SocketWrapperBase.java:533) ~[tomcat-embed-core-10.1.8.jar:10.1.8]
	at org.apache.coyote.http11.Http11OutputBuffer$SocketOutputBuffer.doWrite(Http11OutputBuffer.java:540) ~[tomcat-embed-core-10.1.8.jar:10.1.8]
	at org.apache.coyote.http11.filters.ChunkedOutputFilter.doWrite(ChunkedOutputFilter.java:112) ~[tomcat-embed-core-10.1.8.jar:10.1.8]
	at org.apache.coyote.http11.Http11OutputBuffer.doWrite(Http11OutputBuffer.java:193) ~[tomcat-embed-core-10.1.8.jar:10.1.8]
	at org.apache.coyote.Response.doWrite(Response.java:613) ~[tomcat-embed-core-10.1.8.jar:10.1.8]
	at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:334) ~[tomcat-embed-core-10.1.8.jar:10.1.8]
	at org.apache.catalina.connector.OutputBuffer.flushByteBuffer(OutputBuffer.java:780) ~[tomcat-embed-core-10.1.8.jar:10.1.8]
	at org.apache.catalina.connector.OutputBuffer.append(OutputBuffer.java:683) ~[tomcat-embed-core-10.1.8.jar:10.1.8]
	at org.apache.catalina.connector.OutputBuffer.writeBytes(OutputBuffer.java:382) ~[tomcat-embed-core-10.1.8.jar:10.1.8]
	at org.apache.catalina.connector.OutputBuffer.write(OutputBuffer.java:360) ~[tomcat-embed-core-10.1.8.jar:10.1.8]
	at org.apache.catalina.connector.CoyoteOutputStream.write(CoyoteOutputStream.java:96) ~[tomcat-embed-core-10.1.8.jar:10.1.8]
...

Could someone provide a working example of displaying a pdf file?

just a quick shot…
I also added those settings to the application.properties:
spring.servlet.multipart.max-file-size=1000MB
spring.servlet.multipart.max-request-size=1000MB
server.tomcat.max-swallow-size=1000MB

1 Like

Unfortunately, increasing the limits doesn’t help. Documents of any length are not displayed.

This works with 33kb pdf file i get a blank screen on a 19mb pdf…
woes of jmix 2.x

I use and iframe and have to add configuration to allow the xframe

@Override
        protected void configure(HttpSecurity http) throws Exception {
            super.configure(http);

            http.headers(headers ->
                    headers.contentSecurityPolicy(secPolicy ->
                            secPolicy.policyDirectives("frame-ancestors localhost:8080")
                    )
            );
        }

in the application i use a b64

File file = ResourceUtils.getFile("A17_FlightPlan.pdf");
        byte[] encoded = Base64.encodeBase64(FileUtils.readFileToByteArray(file));
        String b64 = new String(encoded, StandardCharsets.US_ASCII);
        htmlObject.setSrc("data:application/pdf;base64," + b64);

Example HERE
https://www.sendspace.com/file/acxuil

1 Like

there is still an issue with chrome if the b64 is larger than one 1mb .. it wont display..
How do we resolve this in Jmix?

ok this works as expected in an iframe

      String filePath = "A17_FlightPlan.pdf";
        File file = ResourceUtils.getFile(filePath);
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(FileUtils.readFileToByteArray(file));
        final StreamResource resource = new StreamResource(filePath,
                () -> byteArrayInputStream);
        final StreamRegistration registration = VaadinSession.getCurrent().getResourceRegistry().registerResource(resource);
        htmlObject.setSrc(registration.getResourceUri().toString());
2 Likes

It’s working like a charm! Thank you!

I did adapt the Show pdf from filesystem outside project directory and attach it here with your code. Unfortunatly it is not working; what did I do wrong ?

Best regards

Felix
showPdfIssue.zip (3.9 MB)

Again, went through the thread but still not able to get displaying generated PDF work.

Can someone provide a complete example?

Hi Samy

For me, this is working

<htmlObject data="https://web-preview.pspdfkit.com/showcases/8.pdf"/>

Best regards

Felix

1 Like

See also these examples for Jmix 2: