0

I want to save my gererated pdf file in h2 database as BLOB, It works fine when I save it locally (commented code). but when I tried to implement this solution https://stackoverflow.com/a/28495745/14994239 I got that error in the title.

here is the code to create pdf file.

public void pdfCreation(Port port) {
        //String filepath = "C:/Users/ASUS/Desktop/PdfFiles/simpleTable.pdf";

        try {

            //PdfWriter writer = new PdfWriter(filepath);
            //PdfDocument pdfdoc = new PdfDocument(writer);
            //Document document = new Document(pdfdoc);

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            Document document = new Document();
            PdfWriter pd= PdfWriter.getInstance(document, baos);

            float[] columnWidth = { 200f, 200f, 200f, 200f };
            Table table = new Table(columnWidth);
            table.addCell(new Cell().add(new Paragraph("id")));
            table.addCell(new Cell().add(new Paragraph("description")));
           
            table.addCell(new Cell().add(new Paragraph(new StringBuilder().append(port.getId()).toString())));
            table.addCell(new Cell().add(new Paragraph(port.getDescription())));

            document.add(table);
            document.close();

            byte[] pdf = baos.toByteArray();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

here is the imports

import com.itextpdf.io.source.ByteArrayOutputStream;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.List;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import java.io.FileNotFoundException;
import org.springframework.stereotype.Service;

import ma.company.myapp.domain.Port;

I am using 7.2.0 of com.itextpdf

UPDATE:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter writer = new PdfWriter(baos);
PdfDocument pdfdoc = new PdfDocument(writer);
Document document = new Document(pdfdoc);
hakima maarouf
  • 1,010
  • 1
  • 9
  • 26
  • 1
    the error message is quite clear. You can't call = new Document(); because that constructor does not exist. Use the code you commented out instead. https://api.itextpdf.com/iText7/java/7.2.0/index.html?com/itextpdf/layout/Document.html – Stultuske Mar 23 '22 at 13:22
  • @Stultuske thank you for the answer. Please should I write it like this (Update above) so that I can work with `ByteArrayOutputStream` ? – hakima maarouf Mar 23 '22 at 14:01
  • Try it and see if it works – Stultuske Mar 23 '22 at 14:08
  • 1
    @Stultuske It works and my pdf file saved successfully in h2 db. Thank you! – hakima maarouf Mar 23 '22 at 14:26
  • 1
    As explanation: Your code, except the imports, is for iText 5. Your imports are for iText 7. The latter is a complete re-design of the former, so that combination cannot work. Your code update on the other hand is applicable for iText 7. – mkl Mar 23 '22 at 14:54

0 Answers0