Printing ByteArrayOutputStream object

2.7k Views Asked by At

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;


}
2

There are 2 best solutions below

1
On BEST ANSWER

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:

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.printing.PDFPageable;

import javax.print.*;
import java.awt.print.PrinterJob;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

class Main {
    public static void main(String[] args) throws DocumentException, IOException {
        ByteArrayOutputStream byteArr = new ByteArrayOutputStream();
        Document pdfDocument = new Document();
        PdfWriter.getInstance(pdfDocument, byteArr);
        pdfDocument.open();
        pdfDocument.add(new Paragraph("Hello World!"));
        pdfDocument.close();

        byte[] byteStream = byteArr.toByteArray();// fetch content in byte array;
        PrintService services = PrintServiceLookup.lookupDefaultPrintService();
        PrinterJob job = PrinterJob.getPrinterJob();
        PDDocument pdDocument = null;
        try {
            pdDocument = PDDocument.load(byteStream);
            job.setPageable(new PDFPageable(pdDocument));
            job.setPrintService(services);
            job.print();
            System.out.println("Done");

        } catch (Exception pe) {
            pe.printStackTrace();

        } finally {
            if (pdDocument != null) {
                pdDocument.close();
            }
        }


    }
}

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>it.fortytwoapps</groupId>
    <artifactId>so-50249273</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.13</version>
        </dependency>

        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>2.0.9</version>
        </dependency>
    </dependencies>
</project>
6
On

Without being able to run your code, this is not an easy question to answer.

Possible issue is that you are not closing the PdfDocument class. As a result, the underlying resources are not released, and the bytes are not flushed.

As a result, an invalid (sometimes even empty) PDF document is generated, which of course the printer will not (or can not) print.

If you can store the PDF, and it displays properly in a viewer, then you are most likely doing something wrong in the printing part of the application.