生成后如何将spring mvc生成的pdf文件直接保存到数据库中?

时间:2021-09-07 09:54:41

This is my class for generating a pdf. It generates a pdf when I submit the form, but I could not figure out how to save it to the database.

这是我生成pdf的课程。它在我提交表单时生成pdf,但我无法弄清楚如何将其保存到数据库中。

public class BookingPdf {

        private static String FILE = "D:/Hotels/pdf/";
        protected static Font font10;
        protected static Font font10b;
        protected static Font font12;
        protected static Font font12b;
        protected static Font font14;

        public static String generateBookingPDF(Booking booking) throws DocumentException, FileNotFoundException, IOException {
            Document document = new Document(PageSize.A4);
            String bookingname = booking.getFirstName() + " " + booking.getLastName();
            document.addHeader("HOTEL SWING", "Hotel Swing Booking confirmation");
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(FILE + "Booking" + " " + booking.getCustomer().getFirstName() + ".pdf "));
            writer.setViewerPreferences(PdfWriter.PageModeUseOC);
            writer.setPdfVersion(PdfWriter.VERSION_1_7);
            document.open();
            String html = htmlTemplate(booking);

            List unorderedList = new List(List.UNORDERED);
            unorderedList.add(new ListItem("Name       :" + booking.getFirstName() + " " + booking.getLastName()));
            unorderedList.add(new ListItem("Room  :" + booking.getRoom().getRoomNumber()));
            unorderedList.add(new ListItem("Total Price  :" + booking.getTotalPrice()));
            unorderedList.add(new ListItem("check in   :" + booking.getRoom().getRoomNumber()));
            unorderedList.add(new ListItem("Booking:" + booking.getbId()));

            document.add(unorderedList);
            document.close();
            return bookingname;

        }

        private static String htmlTemplate(Booking booking) {
            StringBuilder html = new StringBuilder();

            html.append("" + "<h1> Booking Confirmation</h1>  ");
            return html.toString();

        }

        public BookingPdf(Booking booking) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        public BookingPdf() {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

    }

here is my controller class from where I want to save the pdf location and save it to the database

这是我的控制器类,我想保存pdf位置并将其保存到数据库

@RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(@ModelAttribute("Booking") Booking booking, BindingResult result, HttpServletRequest request) throws SQLException, ParseException, DocumentException, IOException {
        Customer customer = customerService.getById(booking.getCustomer().getC_id());
        Room room = roomService.getById(booking.getRoom().getRo_id());
        long totalNights = (booking.getCheckoutDate().getTime() - booking.getCheckinDate().getTime()) / (24 * 60 * 60 * 1000);
        System.out.println(totalNights);
        booking.setTotalNights((int) totalNights);
        int totalPrice = (booking.getTotalNights() *  booking.getRoom().getRoomPrice());
        booking.setTotalPrice(totalPrice);
        System.out.println(totalPrice);
        System.out.println("check" + booking);

        try {
            booking.setCustomer(customer);
            booking.setRoom(room);
            if (booking.getbId() == 0) {
                bookingService.insert(booking);
                BookingPdf.generateBookingPDF(booking);

            } else 
               {
                bookingService.update(booking);
            }
        } catch (SQLException ex) {
        }
        return "redirect:";

2 个解决方案

#1


1  

You can use a BLOB dataType, converting the file in byte[] array. https://commons.apache.org/proper/commons-io/javadocs/api-2.5/org/apache/commons/io/IOUtils.html

您可以使用BLOB dataType,在byte []数组中转换文件。 https://commons.apache.org/proper/commons-io/javadocs/api-2.5/org/apache/commons/io/IOUtils.html

#2


1  

You can save the file to your server and then save the directory path in the database. So when the user needs to redownload it you have a separate controller for downloading pdfs generated in the past.

您可以将文件保存到服务器,然后将目录路径保存在数据库中。因此,当用户需要重新下载时,您有一个单独的控制器,用于下载过去生成的pdfs。

Another option is to use a library such as Jasper, where you can define a template, and when the report needs to be generated/downloaded you pass in the relevant parameters.

另一种选择是使用诸如Jasper之类的库,您可以在其中定义模板,并且当需要生成/下载报告时,您传递相关参数。

#1


1  

You can use a BLOB dataType, converting the file in byte[] array. https://commons.apache.org/proper/commons-io/javadocs/api-2.5/org/apache/commons/io/IOUtils.html

您可以使用BLOB dataType,在byte []数组中转换文件。 https://commons.apache.org/proper/commons-io/javadocs/api-2.5/org/apache/commons/io/IOUtils.html

#2


1  

You can save the file to your server and then save the directory path in the database. So when the user needs to redownload it you have a separate controller for downloading pdfs generated in the past.

您可以将文件保存到服务器,然后将目录路径保存在数据库中。因此,当用户需要重新下载时,您有一个单独的控制器,用于下载过去生成的pdfs。

Another option is to use a library such as Jasper, where you can define a template, and when the report needs to be generated/downloaded you pass in the relevant parameters.

另一种选择是使用诸如Jasper之类的库,您可以在其中定义模板,并且当需要生成/下载报告时,您传递相关参数。