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

时间:2016-12-21 15:33:57

标签: java spring model-view-controller itext

这是我生成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.
        }

    }

这是我的控制器类,我想保存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 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

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

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