bashoogzaad
bashoogzaad

Reputation: 4781

Java: Unable to set page size while printing a PDF with SimpleDoc

I am currently using this code to print a document (which is passed as a byte array). It is important to note that the document already has the correct size (which I set with iText):

private void printReceipt(ByteArrayOutputStream baos) throws PrinterException {

    //Retrieve byte array (which is in fact the pdf file)
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    Doc pdfDoc = new SimpleDoc(bais, DocFlavor.INPUT_STREAM.AUTOSENSE, null);

    //Select the right printer
    String printerNameDesired = "Star TSP143 (STR_T-001)";
    PrintService[] service = PrinterJob.lookupPrintServices(); // list of printers
    DocPrintJob docPrintJob = null;

    for (int i = 0; i < service.length; i++) {
        if (service[i].getName().equalsIgnoreCase(printerNameDesired )) {
            docPrintJob = service[i].createPrintJob();
            break;
        }
    }

    //Print the byte array
    try {
        final HashPrintRequestAttributeSet attrs = new HashPrintRequestAttributeSet();
        attrs.add(PrintQuality.HIGH);
        attrs.add(MediaSizeName.ISO_A8);
        docPrintJob.print(pdfDoc, attrs);
    } catch (PrintException ex) {
        ex.printStackTrace();
    }

    //Close the bytearray inputstream to prevent memory issues
    try {
        bais.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

    //Delete the pdfDoc to prevent memory issues
    pdfDoc =null;

}

This code works for me, the only problem is that the height of the document is not correct. I need to set a custom page size. However, I have to choose a MediaSizeName, and I can not set a custom page size myself. I would like to set the page formatting like this:

PageFormat pf = new PageFormat();
pf.setOrientation(LANDSCAPE);
Paper paper = pf.getPaper();
paper.setSize(1.102 * 72, 3.504 * 72);
paper.setImageableArea(0, 10, 1.0 * 72, 3.4 * 72);
pf.setPaper(paper);

I don't know how to add this PageFormat to my SimpleDoc. I can not convert my SimpleDoc to a Printable like this:

PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintService(docPrintJob.getPrintService());

Book book = new Book();//java.awt.print.Book
book.append(SimpleDocConvertedToPrintable, pf);
job.setPageable(book);

job.print();

Unfortunately, I can not get this to work. I also thought of setting the custom MediaSize from within the HashPrintRequestAttributeSet, but I could not find it anywhere.

So my question is: how can I create a custom MediaSize, or is there maybe a better and quicker way to print a PDF without saving it to a file?

Any help is greatly appreciated!

Upvotes: 0

Views: 2277

Answers (1)

bashoogzaad
bashoogzaad

Reputation: 4781

I have been able to fix this just by stretching the sizes of the document while I created it in iText. I think it has something to do with the DPI settings, but I could not find it anywhere online. The size in iText is set like this:

document.setPageSize(new Rectangle(660, 625));

The width (which is 660) is exactly enough to fit on my receipt (which has width = 80mm). The height is just based on trial on error. When the receipt gets bigger (in terms of more height), the printer magically prints a longer receipt. Can anyone maybe tell my why? The pagesize is set to a constant...

Upvotes: 1

Related Questions