0

My requirement for PDF download is:

  1. When user click on "Download" link, front-end will POST a JSON data to the back-end.
  2. Back-end will process the JSON, generating a PDF file from it's parameters.
  3. In response I (back-end) need to send a download link and unique document id.
  4. Frond-end will open that link (GET) in new window which will hit the back-end and download will start.

How should I do this?

Thanks in advance.

André
  • 2,184
  • 1
  • 22
  • 30
user2758176
  • 193
  • 1
  • 7
  • 20
  • What you have tried so far? Do you have the PDF byte array? Are you using a plain java servlet to map the download URL? How the json will be sent from the client? Is it a POST? a GET? Please, elaborate. – André Jun 30 '15 at 12:33
  • I am using spring. JSON data will come in POST request. After that I am creating JASPER Report using this data. – user2758176 Jun 30 '15 at 12:38

1 Answers1

2

If you're using Spring you create a Controller

@Controller
@RequestMapping(value = "/report")
public class ReportController {

    @Autowired
    ReportFileStore reportFileStore;

    @RequestMapping(value = "/download/{uniqueId}", method = RequestMethod.GET)
    public void getFile(@PathVariable("uniqueId") String uniqueId, HttpServletResponse response) {
        //Find the generated PDF from somewhere, might be a disk, or RAM
        byte[] pdf = getFromStore(uniqueId);

        writeToResponse(pdf,response);
        deleteFileFromStore(uniqueId);
    }

    private byte[] getFromStore(String uniqueId){
        return reportFileStore.getFile(uniqueId);
    }
}

The method writeToResponse is a standard servlet download code, you can see a example here or here.

About the getFromStore, that is a simple method get the byte[] of the PDF generated from Jasper, when you generate you can have put method that stores the byte[] with the uniqueId.

I'd use a interface like this

public interface ReportFileStore {
     void storeFile(String uniqueId,byte[] content);
     byte[] getFile(String uniqueId);
     InputStream getFile(String uniqueId);
     void deleteFile(String uniqueId);
}

And implement it using a VFS mapped on a RAM or Disk.


Of course, on your PDF generation, you need to generate a unique ID for it, you can try using UUID. Use this UUID with the ReportFileStore to save the PDF file. It's unclear if the "need to return a download link and a unique id" can be done just returning the uniqueId, then hardcoding the download location on the front-end. If not, return a JSON mapping it.

Community
  • 1
  • 1
André
  • 2,184
  • 1
  • 22
  • 30
  • Could you also share sample code for getFromStore(uniqueId) – user2758176 Jun 30 '15 at 13:27
  • I added a sample implementation on a controller. – André Jun 30 '15 at 13:30
  • That `getFromStore()` would be `reportFileStore.getFile()`, i think... Also `deleteFileFromStore()` would be `reportFileStore.deletFile()`... – Usagi Miyamoto May 08 '17 at 07:39
  • @UsagiMiyamoto, yes, they have the same signature, the code above is just pseudo-code on how to create a file download endpoint. The idea is to generate the PDF, store at the `interface ReportFileStore` and then use it stream the file contents at the Controller – André May 09 '17 at 01:02