/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cosmodas.validaciones;

import java.io.FileInputStream;
import java.io.IOException;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.AttributeSet;
import javax.print.attribute.HashAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.standard.PrinterName;

/**
 *
 * @author obdulio
 */
public class ImprimirDocumentos {

    private String impresor;
    private String documento;

    public String getImpresor() {
        return impresor;
    }

    public void setImpresor(String impresor) {
        this.impresor = impresor;
    }

    public String getDocumento() {
        return documento;
    }

    public void setDocumento(String documento) {
        this.documento = documento;
    }

    public ImprimirDocumentos() {
    }

    public String imprimirDocumento(String impre, String documento) {

        try {
            //Formato de Documento
            try (FileInputStream inputStream = new FileInputStream(documento)) {
                //Formato de Documento
                DocFlavor docFormat = DocFlavor.INPUT_STREAM.AUTOSENSE;
                //Lectura de Documento
                Doc document = new SimpleDoc(inputStream, docFormat, null);
                
                //Nombre de la impresora
                String printerName = impre;
                
                //Inclusion del nombre de impresora y sus atributos
                AttributeSet attributeSet = new HashAttributeSet();
                
                attributeSet = new HashAttributeSet();
                attributeSet.add(new PrinterName(printerName, null));
                //Soporte de color o no
                // attributeSet.add(ColorSupported.NOT_SUPPORTED);
                
                PrintService[] services = PrintServiceLookup.lookupPrintServices(docFormat, attributeSet);
                
                System.out.println("Imprimiendo en : " + services[0].getName());
                
                DocPrintJob printJob = services[0].createPrintJob();
                //Envio a la impresora
                printJob.print(document, new HashPrintRequestAttributeSet());
            }
        } catch (IOException | PrintException e) {
            
            System.err.println("Error al imprimir documento");
            return "Error al imprimir documento";
        }
 
        
        return "exito";
    }

}
