简体   繁体   中英

Is there a way to render a pdf document in thymeleaf, which is stored in a remote server and without downloading it?

I have to write an application to render PDF documents with Thymeleaf. These documents are stored in a remote server and are accesed through HTTP.

I know it is possible to render PDF documents, when they are stored in the project´s ressource folder. But I don`t want to download them.

Is there any possible way?

I am receiving the file with the following code:

        URL url = new URL("http://10.139.1.240:8080/document.pdf");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        if (connection.getResponseCode() == 200) {
            try (InputStreamReader streamReader = new InputStreamReader(connection.getInputStream());
                 BufferedReader br = new BufferedReader(streamReader);
                 Stream<String> lines = br.lines()) {
                Desktop.getDesktop().browse(url.toURI());
           
            } catch (URISyntaxException e) {
                throw new RuntimeException(e);
            }
        }
  

You could create a method in your controller that allows user to download that file as described in answer to this question , then on the client side, place an iframe pointing to that endpoint.:

<iframe sec="/controller_mapping/method_mapping"/>

You can control it's width and height using style tag.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM