1

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);
            }
        }
  
  • On the server side, you can open the PDF with PDFBox and create an image for each page. Then you can show the images on client side without the need to download the full PDF file. See [Convert PDF files to images with PDFBox](https://stackoverflow.com/questions/23326562/convert-pdf-files-to-images-with-pdfbox). – vanje Jan 07 '23 at 18:29

1 Answers1

0

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.

Kamil Bęben
  • 1,064
  • 8
  • 12