Aspose-pdf-java utilizes a lot of CPU/RAM resources with generating and saving pdf document with 1 table 20K rows and 10 columns.
Here is an example of my code
private static final int ROWS_COUNT = 20000;
private static final int COLUMNS_COUNT = 10;
public static void main(String[] args) throws IOException {
savePdfAspose("/tmp/x.pdf");
savePdfAspose("/tmp/y.pdf");
}
public static void savePdfAspose(String path) {
Document document = new Document();
Page page = document.getPages().add();
Table table = new Table();
for (int i = 0; i < ROWS_COUNT; i++) {
Row currentRow = table.getRows().add();
for (int j = 0; j < COLUMNS_COUNT; j++) {
Cell cell = new Cell();
TextFragment textFragment = new TextFragment("test test test test");
cell.getParagraphs().add(textFragment);
currentRow.getCells().add(cell);
}
}
page.getParagraphs().add(table);
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
try (FileOutputStream outputStream = new FileOutputStream(path)) {
document.save(outputStream, pdfSaveOptions);
document.close();
} catch (IOException e) {
e.printStackTrace();
}
}
I call the savePdfAspose function 2 times and it tasks 20-30 seconds each time.
Profiling shows that document.save(outputStream, pdfSaveOptions); utilize a lot of CPU and RAM.
I have tastes the same document generation with Itext pdf generation library. Here is the corresponding code for Itext:
public static void savePdfItext(String path) {
try(FileOutputStream outputStream = new FileOutputStream(path)) {
com.lowagie.text.Document document = new com.lowagie.text.Document();
PdfWriter writer = PdfWriter.getInstance(document, outputStream);
document.open();
document.newPage();
PdfPTable table = new PdfPTable(COLUMNS_COUNT);
for (int i = 0; i < ROWS_COUNT; i++) {
for (int j = 0; j < COLUMNS_COUNT; j++) {
Anchor phrase = new Anchor("test test test test");
PdfPCell cell = new PdfPCell(phrase);
table.addCell(cell);
}
}
document.add(table);
document.close();
writer.flush();
} catch (DocumentException | IOException e) {
e.printStackTrace();
}
}
Itext generates the file 10 times faster and without noticeable CPU/RAM utilization.
Can you please suggest how can I optimize aspose code, or maybe aspose-pdf-java is not intended for that kind of usage?