How to save generated PDF files to MySQL database using Java?

Adi

I have a Java class which is generating PDF file using iText library. Now as per my need I have to save this generated PDF file to MySQL database table but I have no idea how to do it.

My concerns are:

  1. what datatype should I use in MySQL column of PDF table to save PDF file
  2. which query will insert generated PDF file to database

At present I am generating PDF file and storing it into hard-coded file path of my local disk.

Here is my PDF generation code in Java:

OutputStream file = new FileOutputStream(new File("D://timer.pdf"));
Document document = new Document();
PdfWriter.getInstance(document, file);

//Inserting Table in PDF
PdfPTable table = new PdfPTable(3);

PdfPCell cell = new PdfPCell(new Paragraph("Java4s.com"));

cell.setColspan(3);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setPadding(10.0f);
cell.setBackgroundColor(new BaseColor(140, 221, 8));

table.addCell(cell);

table.addCell("Name");
table.addCell("Address");
table.addCell("Country");
table.addCell("Java4s");
table.addCell("NC");
table.addCell("United States");
table.setSpacingBefore(30.0f);  // Space Before table starts, like margin-top in CSS
table.setSpacingAfter(30.0f);   // Space After table starts, like margin-Bottom in CSS

//Inserting List in PDF
List list = new List(true, 30);
list.add(new ListItem("Java4s"));
list.add(new ListItem("Php4s"));
list.add(new ListItem("Some Thing..."));

//Text formating in PDF
Chunk chunk = new Chunk("Welecome To Java4s Programming Blog...");
chunk.setUnderline(+1f, -2f);//1st co-ordinate is for line width,2nd is space between
Chunk chunk1 = new Chunk("Php4s.com");
chunk1.setUnderline(+4f, -8f);
chunk1.setBackground(new BaseColor(17, 46, 193));

//Now Insert Every Thing Into PDF Document
document.open();//PDF document opened........                  
document.add(Chunk.NEWLINE);   //Something like in HTML :-)
document.add(new Paragraph("Dear Java4s.com"));
document.add(new Paragraph("Document Generated On - " + newDate().toString()));
document.add(table);
document.add(list);            //In the new page we are going to add list
document.close();

file.close();

System.out.println("Pdf created successfully..");


Please help me.
Thanks in advance.

Pratik Shelar
  1. Datatype that you can use is BLOB.
  2. Convert the PDF file and persist the byte[] array in database.

    private byte[] getByteArrayFromFile(final Document handledDocument) throws IOException {
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final InputStream in = new FileInputStream(handledDocument);
        final byte[] buffer = new byte[500];
    
        int read = -1;
        while ((read = in.read(buffer)) > 0) {
            baos.write(buffer, 0, read);
        }
        in.close();
    
        return baos.toByteArray();
    }
    
  3. To insert it into DB If you are using any ORM tools you just have to map the column as blob and the tool will handle it for you. In case you are not using it then you can create a prepared statement. Statement has a method called setBlob() which will be useful. Consider the below example and create a normal insert query with blob column.

    String sql = "INSERT INTO testtable(stringcolumn, blobcolumn) VALUES(?,?)";
    
    PreparedStatement statement = conn.getConnection().prepareStatement(sql);
    statement.setLong(1, version);
    ByteArrayInputStream bais = new ByteArrayInputStream(getByteArrayFromFile(document));
    statement.setBlob(2, bais);          
    statement.execute();
    
    conn.commit();
    

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to save generated PDF files to MySQL database using Java?

From Dev

how to save doc and pdf file into mysql database from android application?

From Dev

Rename duplicate files using java and mysql database

From Dev

I generated the multiple pdf from dompdf but how can I save these files locally

From Dev

How to save generated pdf in server in rails

From Dev

how to search in a list generated from database using mysql+php

From Dev

How to save Dynamically Generated table rows to database

From Dev

save generated pdf on server

From Dev

How to save blob files in database

From Dev

How to Find LaTeX generated PDF files?

From Dev

How to save an object in Entity Framework while using a database generated option for one of its columns?

From Dev

How save all actions from users in MySQL Database - Java Application

From Dev

generate and save multiple pdf files using mPDF

From Dev

How to Store PDF in mysql database using node js and express

From Dev

Generated pdf using itextpdf in java servlet

From Dev

How to display .pdf files from mysql using php

From Dev

How to connect java using eclipse with MySQL database

From Dev

How to save an image from HTML to a DATABASE using JAVA

From Dev

How to save data from phpmyadmin database to pdf?

From Dev

How to save auto generated multiple textInput Array value in mysql using Yii2

From Dev

How to save only zip files and remove other folders using java?

From Dev

How to restore mysql database using files on data folder?

From Dev

How to retrieve questions and answers from mysql database by using php files

From Dev

How to extract possibly corrupt image blobs from a mysql database in PHP and save to files

From Dev

Save pdf to bytearray using itext in Java

From Dev

Save pdf to bytearray using itext in Java

From Dev

How to save the selected checkbox items in Jtable to mysql using java?

From Dev

Save a fingerprint template into a database mysql - java

From Dev

Save a fingerprint template into a database mysql - java

Related Related

  1. 1

    How to save generated PDF files to MySQL database using Java?

  2. 2

    how to save doc and pdf file into mysql database from android application?

  3. 3

    Rename duplicate files using java and mysql database

  4. 4

    I generated the multiple pdf from dompdf but how can I save these files locally

  5. 5

    How to save generated pdf in server in rails

  6. 6

    how to search in a list generated from database using mysql+php

  7. 7

    How to save Dynamically Generated table rows to database

  8. 8

    save generated pdf on server

  9. 9

    How to save blob files in database

  10. 10

    How to Find LaTeX generated PDF files?

  11. 11

    How to save an object in Entity Framework while using a database generated option for one of its columns?

  12. 12

    How save all actions from users in MySQL Database - Java Application

  13. 13

    generate and save multiple pdf files using mPDF

  14. 14

    How to Store PDF in mysql database using node js and express

  15. 15

    Generated pdf using itextpdf in java servlet

  16. 16

    How to display .pdf files from mysql using php

  17. 17

    How to connect java using eclipse with MySQL database

  18. 18

    How to save an image from HTML to a DATABASE using JAVA

  19. 19

    How to save data from phpmyadmin database to pdf?

  20. 20

    How to save auto generated multiple textInput Array value in mysql using Yii2

  21. 21

    How to save only zip files and remove other folders using java?

  22. 22

    How to restore mysql database using files on data folder?

  23. 23

    How to retrieve questions and answers from mysql database by using php files

  24. 24

    How to extract possibly corrupt image blobs from a mysql database in PHP and save to files

  25. 25

    Save pdf to bytearray using itext in Java

  26. 26

    Save pdf to bytearray using itext in Java

  27. 27

    How to save the selected checkbox items in Jtable to mysql using java?

  28. 28

    Save a fingerprint template into a database mysql - java

  29. 29

    Save a fingerprint template into a database mysql - java

HotTag

Archive