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);