I write on Spring java. The controller already has a method that creates and returns a ModelAndView in the correct format of a PDF file, beautifully formatted with all sorts of embellishments that you don’t want to do again. Can I use this method to create a PDF file? What is needed for that?

For example, is it possible to call this controller method from another method, get the created model and write it just byte-by-byte into a file? Will anything good come out of this?

Or do I just get the HTML code in this case?

  • I do not need a response from the server, I just need to automatically generate a report and send it by email. - Helena2977

2 answers 2

All this is not very clear. Why is this the controller method? Does this method have input parameters? Then, to exit it from another method, you will need to transfer these parameters, and they come from the client ... From the point of view of building code, it is more logical to take out the creation of PDF as a separate method, and it will be more flexible to call it from the controller, for example.

In a good way, there should be a minimum of lines of code in the controller. If you need to return the PDF as a response from the server, then, in addition to ModelAndView, controllers can return a lot more. For example, ResponseEntity, with a generic byte [] parameter, plus add headers in response that you return application / pdf. Would bring your code to make it clearer what and why.

  • It's just that there is a giant code written by another person whom I don't even know. He has long since left the firm. And it turns out the page in PDF format of the desired type. There are graphs and tables in it. And he used jrxml giant. So I thought that you could not waste time analyzing this code. But apparently, this is the wrong approach. We'll have to figure out how he formatted everything. - Helena2977
  • one
    Maybe there is a means to facilitate the formatting of information pdf? To draw tables and graphs there? - Helena2977
  • There are all kinds of libraries such as IText, Apache PDF, but I didn’t work with them, I can’t say anything. - iksuy

Well, if the ModelAndView method returns pdf, then most likely when writing to a file with the .pdf extension, you should get what you want.