1

I am doing coding in jsp. I have generated pdf using itext. And trying to save the document into database(mysql).

        String filename="patient.pdf";
        response.setContentType("application/pdf");
        Document document = new Document();
        PdfWriter pd= PdfWriter.getInstance(document, new FileOutputStream(filename));
        document.open();
        document.addTitle("Dentistree");
        document.add(new Paragraph("Hello,"+"\n\t"+"Thanks for making an appointment to Dentistree"+"\n\t"+"You have provided case history for your appointment. Here are the details of the appointment\n\t"+"List of the diseases that you are suffering through are as follows:"+di+"\n\n\nAre you pregnant?\t"+pregnant+"\n\n\nAre nursing a child?\t"+nursing+"\n\n\nDo you chew pan-masala?\t"+pan+"\n\n\nDo you smoke?\t"+smoke+"\n\n\nAllergic?\t"+all+"List of the medicines:"+medicines));
        document.close();



        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dentistree","root", "tiger");
        String str="insert into appointment(email,pdf)  values(?,?)";
        PreparedStatement st = con.prepareStatement(str);
        st.setString(1, mail);
        st.setBlob(2,pd);

New to java coding. Please help

Abhishek
  • 111
  • 1
  • 3
  • 15
  • Any issues/error/exception? What's your question please? – SMA Mar 22 '15 at 08:20
  • If you need to store a PDF as a BLOB into a database, why on earth are you create a file on the file system? Create the PDF in memory as a `ByteArrayOutputStream` and store the resulting `byte[]` in your database. – Bruno Lowagie Mar 22 '15 at 08:54
  • Do you actually execute the statement? – mkl Mar 22 '15 at 13:32
  • 1
    possible duplicate of [Save itext pdf as blob without physical existence.](http://stackoverflow.com/questions/28492774/save-itext-pdf-as-blob-without-physical-existence) – Bruno Lowagie Mar 22 '15 at 14:19

1 Answers1

3

Your question is full of contradictions.

  1. You say that you want to create a PDF file (which is a binary file), but you are using JSP. However, if you would finish your course on JSP (instead of trying to run before you can walk), you would discover that is it not done to create binary files using JSP. Use plain Java or Java Servlets instead.
  2. You say that you want to store a PDF in a database, however, when I look at your code, I see response.setContentType("application/pdf"); which means that you want to send a PDF to a browser.
  3. You say that you want to store a PDF in a database and at the same time you try to send it to a browser, but instead of doing so, you save the PDF on the file system of the server: new FileOutputStream(filename). It's as if you can't make up your mind as to what you really want to do.
  4. Also: you add a title for the document after opening the document. You should add the title before opening the document instead.

As indicated in my comment, you should create your PDF in memory:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
Document document = new Document();
PdfWriter pd= PdfWriter.getInstance(document, baos);
document.open();
document.add(new Paragraph("Hello");
document.close();

Now you have the baos object from which you can obtain the full PDF as a byte[]:

byte[] pdf = baos.toByteArray();

This has been answered before on StackOverflow: Save itext pdf as blob without physical existence. Once you have the byte[], you can also send the PDF to the browser if that is an extra requirement. See my answer to Pdf file not loading properly created by the servlet to find out how it's done.

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165