user3166804
user3166804

Reputation: 23

Can't find a Print Service using java in Windows

I am trying to locate a print service that can handle a job, i am using the PrintService API in Java. This is my code:

private PrintService[] services = null;

services = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.PDF, null);

System.out.println("We found : " + services.length + " service(s)");

The output was always:

We found : 0 service(s)

I don't know why it can't find a service although I have a printer installed in my computer! noted that:

Upvotes: 1

Views: 4007

Answers (2)

mxsb
mxsb

Reputation: 216

It seems like there is a problem with the PDF capability under Windows. I ran into the same problem and haven't found a solution yet. Other people have found a workaround, but this seems to be illegal by now (see https://community.oracle.com/thread/2046162).

EDIT

I worked around this problem by converting the PDF to a PNG image.

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

import static java.awt.image.BufferedImage.TYPE_INT_RGB;
import static javax.imageio.ImageIO.write;
import static org.apache.pdfbox.pdmodel.PDDocument.load;

public class PdfToImageConverter {

    public static String GIF = "gif";
    public static String JPG = "jpg";
    public static String PNG = "png";

    public static byte[] convertPdfTo(final String imageType, final byte[] pdfContent) throws IOException {
        final PDDocument document = load(new ByteArrayInputStream(pdfContent));
        final List<PDPage> allPages = document.getDocumentCatalog().getAllPages();
        final PDPage pdPage = allPages.get(0);
        final BufferedImage image = pdPage.convertToImage(TYPE_INT_RGB, 300);
        final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        write(image, "png", outputStream);
        outputStream.flush();

        final byte[] imageInByte = outputStream.toByteArray();

        outputStream.close();

        return imageInByte;
    }

}

I added MediaSizeName.ISO_A4 as PrintRequestAttribute to the PrintJob, this solution works for me.

Upvotes: 0

user3166804
user3166804

Reputation: 23

There was no PrintService found corresponding to the specified DocFlavor: 'PDF'
Because when i tried to find out which are the DocFlavor supported by my printer:

PrintService[] prnSvc = PrintServiceLookup.lookupPrintServices(null, null);

    DocFlavor[] docFalvor = prnSvc[0].getSupportedDocFlavors();
    for (int i = 0; i < docFalvor.length; i++) {
        System.out.println(docFalvor[i].getMimeType());
    }

I got just:

image/gif
image/gif
image/gif
image/jpeg
image/jpeg
image/jpeg
image/png
image/png
image/png
application/x-java-jvm-local-objectref
application/x-java-jvm-local-objectref
application/octet-stream
application/octet-stream
application/octet-stream

Similar posts: Printer services Not found? and Java Print program with Specfications issues?

Upvotes: 1

Related Questions