1

In my Spring Boot app, I am generating pdf file from html string and save it a temp location by using the following approach:

@Override
public PdfResponse downloadPdfFromUrl(final PdfRequest request, final String html) {

    // some codes omitted for brevity

    Pdf pdf = new Pdf();
    String filePath = request.getDownloadPath()+ "/" + request.getItemUuid()+ ".pdf";
     pdf.saveAs(filePath);

    PdfResponse response = new PdfResponse();
    response.setFileDownloadPath(filePath);
    response.setFileName(request.getItemUuid());

    return response;
}
@Data
public class PdfResponse {
    private UUID fileName;
    private String fileDownloadPath;
    private Long size;
}

At this point, I want to save the generated pdf as blob and return it in a proper format.

1. The client will receive the blob file and then open it as pdf. In this case I think I should create and save blob file from pdf after generating it? Is that right?

2. How could I generate blob from pdf?

3. Which type should I return the generated blob file? Is MultipartFile is a proper format? And I think I cannot return blob directly and have to save it first?

1 Answers1

0

The type matching databases blob storages in java are simply bytes array.

From your filepath, you have to get your pdf binaries from filepath and then send it to your persistance storage like you want :

String filePath = request.getDownloadPath()+ "/" + request.getItemUuid()+ ".pdf";
pdf.saveAs(filePath);
byte[] pdfData = Files.readAllBytes(Paths.get(filePath));

And your pdfResponse should look like this :

@Data
public class PdfResponse {
    private UUID fileId;
    private String fileName;
    private byte[] pdfData;
    private Long size;
}

Last but not least I think you will want to be able to download that PDF file from a Spring controller. Then you can achieve it this way (It's same logic for a PDF or an image) : https://www.baeldung.com/spring-controller-return-image-file (Just replace .jpg with .pdf)

Gweltaz Niquel
  • 629
  • 4
  • 14
  • 1
    Thank you so much, voted up (I will try and probably it works). On the other hand, as I am new for this topic, could you please clarify me about the following issues? –  Jul 28 '22 at 08:38
  • 1
    **1.** I use `ResponseEntity` in my Controller as return type, is that a proper way for Spring Boot for this scenario? I generally return `ResponseEntity` and I think `Resource` may be changed by a proper object e.g. `byte[]` or `MultipartFile`. But not sure. –  Jul 28 '22 at 08:41
  • **2.** What is the differences between `MultipartFile` and `Resource`? Could we convert the generated pdf directly to these types without saving disk? –  Jul 28 '22 at 08:42
  • My returned pdf file is less than 1MB, in this scene, should I use `ResponseEntity` or `MultipartFile` from `Controller`? –  Jul 28 '22 at 08:56
  • So you can use Spring Resource. I talked about byte arrays as you ask about blobs. You should prefer InputStreams. Take a look at https://stackoverflow.com/questions/5673260/downloading-a-file-from-spring-controllers – Gweltaz Niquel Jul 28 '22 at 09:05