Direct printing in Java

Asked

Viewed 882 times

1

While running, the error below is displayed when sending a string printer-friendly.

sun.print.PrintJobFlavorException: invalid flavor
at sun.print.Win32PrintJob.print(Unknown Source)

Complete code

    PrintService[] printServices = PrintServiceLookup.lookupPrintServices(DocFlavor.SERVICE_FORMATTED.PRINTABLE, null);

    PrintService printService = printServices[5];
    System.out.println(printService.getName());
    DocFlavor docFlavor = DocFlavor.STRING.TEXT_PLAIN;


    StringBuilder value = new StringBuilder();

    value.append("\n\n");
    value.append("\n\tRelação dos Usuários:");

    List<Usuario> usuarios = TapiocariaFacade.listarUsuario();

    int y = 70;

    for(Usuario usuario : usuarios) {

        value.append("\n\tNome: " + usuario.getSenha());
        value.append("\n\tEndereço: " + usuario.getId());
        value.append("\n\tEmail: " + usuario.getEmail());
    }
    System.out.println(value.toString());


    Doc doc = new SimpleDoc(value.toString(), docFlavor, null);

    DocPrintJob docPrintJob = printService.createPrintJob();
    try {
        docPrintJob.print(doc, null);
    } catch (PrintException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

1 answer

2


Enter a different type for your Flavor for compatibility

DocFlavor docFlavor = DocFlavor.INPUT_STREAM.AUTOSENSE;

You have created an array with the printers but no printer is available at position 5

PrintService[] printServices = PrintServiceLookup.lookupPrintServices(DocFlavor.SERVICE_FORMATTED.PRINTABLE, null);

PrintService printService = printServices[5];

You can try with

PrintService printServices = PrintServiceLookup.lookupDefaultPrintService();

Or else

PrintService[] printServices = PrintServiceLookup.lookupPrintServices(DocFlavor.SERVICE_FORMATTED.PRINTABLE, null);

if (printServices.length==0) {
    JOptionPane.showMessageDialog(null, "No Printer Selected");
        }
else if (printServices.length > 0) {
        DocPrintJob pj = printServices[0].createPrintJob();
        {

And a better suggestion

    PrintService printServices= null;
    DocFlavor docFlavor = DocFlavor.STRING.TEXT_PLAIN;
    PrintRequestAttributeSet attr_set =
            new HashPrintRequestAttributeSet();

    attr_set.add(new Copies(1));           
    attr_set.add(Sides.ONE_SIDED);
    PrintService[] service = PrintServiceLookup.lookupPrintServices(docFlavor, attr_set);

    for (int i = 0; i < printServices.length; i++) {
        System.out.println(printServices[i].getName());
        if (printServices[i].getName().equals(nomeDaSuaImpressora)) {
            ps = service[i];
        }
    }
  • 3

    Welcome to Stack Overflow! Even if this link is a good suggestion, this reply will not be valid if one day the link ceases to work. So, because it’s important for the community to have content right here, you’d better respond with more complete answers. Can you elaborate more your answer? A summary of the content of the link would help a lot! Learn more on this item in our Community FAQ: Answers that only contain links are good?

  • 1

    Ismael good morning, thanks for answering but come on, first the printer at position 5 it exists because by the sysout appears your name, I will change the Flavor to see. Later I put the result.

  • 1

    Ismael, it worked out like this: DocFlavor docFlavor = DocFlavor.INPUT_STREAM.AUTOSENSE; and creating a text file: FileInputStream fis = new FileInputStream("D:/fff.txt"); Only that in my case I didn’t always want to create a txt but to pass a direct string, some idea?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.