I am trying to return an iText generated PDF from the server side to the client side to enable the user to store it. I am following How to convert iTextPDF Document to Byte Array (AceFunk)
private static ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
public static byte[] main(java.util.List<Transcript> listymAwards, String scoutName, String groupName) {
Document document = new Document(PageSize.A4, 0f, 0f, 0f, 0f);
try {
//PdfWriter.getInstance(document, new FileOutputStream(FILE));
PdfWriter.getInstance(document, byteArrayOutputStream); // Do this BEFORE document.open()
document.open();
addMetaData(document);
addImages(document);
addTitlePage(document, scoutName);
//Add the table of achievements
if (listymAwards == null || listymAwards.isEmpty()) {
//Nothing to do.
//System.out.println("Scout not found.");
}else{
Paragraph preface = new Paragraph();
PdfPTable table = new PdfPTable(3);
table.setWidths(new int[]{1, 3, 1});
table.setHeaderRows(1);
PdfPCell c1 = new PdfPCell(new Phrase("Section"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Award"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Date"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
table.setHeaderRows(1);
String storedName = null;
int noRows = 0;
String firstTable = "Yes";
DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
DateFormat df2 = new SimpleDateFormat("dd-MM-yyyy");
// We add three empty lines
addEmptyLine(preface, 1);
addEmptyLine(preface, 1);
addEmptyLine(preface, 1);
for (final Transcript scoutNamesDescription : listymAwards) {
if (firstTable.equals("Yes") && noRows > 30){ // Change this to number of rows required
noRows = 0;
firstTable = "No";
document.add(table);
document.newPage();
table.flushContent();
}else{
if (firstTable.equals("No") && noRows > 50){ // Change this to number of rows required
// We add three empty lines if not the first table
document.add(preface);
}
}
noRows++;
if (scoutNamesDescription.getSection().equals(storedName)){
table.addCell(" ");
}else{
storedName = scoutNamesDescription.getSection();
table.addCell(scoutNamesDescription.getSection());
}
table.addCell(scoutNamesDescription.getAwardName());
Date awardedDate = df1.parse(scoutNamesDescription.getAwardedDate());
String awardedString = df2.format(awardedDate);
table.addCell(awardedString);
}
//Print the remaining rows.
// We add three empty lines if not the first table
if (firstTable.equals("No")){
document.add(preface);
}else{
firstTable = "No";
}
document.add(table);
}
//Add signature
addSignaturePage(document, groupName);
document.close();
} catch (Exception e) {
e.printStackTrace();
}
byte[] pdfBytes = byteArrayOutputStream.toByteArray();
return pdfBytes;
}
This is returned to the server side:
byte[] pdfBytes = ScoutTranscript.main(listymAwards, scoutName, groupName);
System.out.println("Point 3");
return pdfBytes;
Which is then returned to the client side:
Window.open("data:application/pdf;base64,"+result,"_parent", "location=no");
Where I get the error message:
This site can’t be reached
The webpage at data:application/pdf;base64,[B@154 might be temporarily down or it may have moved permanently to a new web address.