I have a generated PDF document using iText library, then I kept the document in memory using a ByteArrayOutputStream to print it but it's not printing anything. Any idea on why isn't it printing? You can find the code below and thanks in advance.
ByteArrayOutputStream byteArr = new ByteArrayOutputStream();
Document document = new Document();
PdfWriter.getInstance(document, byteArr);
document.open();
/*
* Adding data and tables to the document
*/
document.close();
DocFlavor docType = DocFlavor.BYTE_ARRAY.AUTOSENSE;
byte[] byteStream = byteArr.toByteArray();// fetch content in byte array;
// byteArr is the ByteArrayOutputStream object
// Tried using InputStream but did not work as well.
Doc documentToBePrinted = new SimpleDoc(byteStream, docType, null);
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
PrintService services = PrintServiceLookup.lookupDefaultPrintService();
DocPrintJob job = services.createPrintJob();
try {
job.print(documentToBePrinted, aset);
System.out.println("Donee");
}
catch (Exception pe)
{
pe.printStackTrace();
}
byteArr = null;
}
Probably the printer you are using doesn't support PDF directly. Try using PDFBox to print the document. I made a small maven project to test it and it works for me:
Main.java:
pom.xml: