예제 #1
0
  public void print(String printer, byte[] bytes, String type) throws ReportException {
    try {

      PrintService printService;
      DocPrintJob job;
      DocFlavor fl;
      Doc doc;

      DocAttributeSet das = new HashDocAttributeSet();
      Object printObject;
      if (type == null || type.length() == 0) {
        fl = DocFlavor.BYTE_ARRAY.AUTOSENSE;
        printObject = bytes;
      } else if (type.equals(DocumentTypes.APPLICATION_PDF.toString())) {
        fl = DocFlavor.BYTE_ARRAY.AUTOSENSE;
        printObject = bytes;
      } else if (type.equals(DocumentTypes.TEXT_XML.toString())) {
        fl = DocFlavor.BYTE_ARRAY.AUTOSENSE;
        printObject = bytes;
      } else {
        log.info("Unknown type: " + type);
        fl = DocFlavor.INPUT_STREAM.AUTOSENSE;
        printObject = new ByteArrayInputStream(bytes);
      }

      if (printer != null && printer.equals(NO_PRINTER)) {
        log.info("Won't print. Printer: " + printer);
        return;
      }
      if (printer == null || printer.length() == 0 || printer.equals(DEFAULT_PRINTER)) {

        printService = PrintServiceLookup.lookupDefaultPrintService();

      } else {
        try {
          printService = getNamedPrintService(fl, printer);
        } catch (Exception ex) {
          log.log(Level.INFO, ex.getMessage(), ex);
          throw new IllegalArgumentException("Printer cannot be selected");
        }
      }

      if (printService == null) {
        log.info("printer not found: " + printer);
        throw new ReportException(ReportExceptionKey.PRINT_FAILED, printer);
      }

      doc = new SimpleDoc(printObject, fl, das);
      job = printService.createPrintJob();
      PrintJobWatcher watcher = null;

      if (fl instanceof DocFlavor.INPUT_STREAM) {
        watcher = new PrintJobWatcher(job);
      }

      job.print(doc, null);

      if (watcher != null) {
        watcher.waitForDone();
        ((InputStream) printObject).close();
      }

    } catch (PrintException ex) {
      log.log(Level.INFO, ex.getMessage(), ex);
      throw new ReportException(ReportExceptionKey.PRINT_FAILED, printer);
    } catch (Throwable ex) {
      log.log(Level.INFO, ex.getMessage(), ex);
      throw new ReportException(ReportExceptionKey.PRINT_FAILED, printer);
    }
  }
  /*
   * Metodo que imprime directamente en una impresra byte
   */
  public void printExtra(CexAgenda turno) throws IOException {
    // FileInputStream inputStream = null;
    InputStream tex = null;
    SimpleDateFormat sf = new SimpleDateFormat("dd/MM/yyyy");
    String fecha = sf.format(turno.getCagFecha());
    SimpleDateFormat sfHora = new SimpleDateFormat("HH:mm");
    String hora = "";
    String te2;
    if (turno.getCagHora() != null) {
      hora = sfHora.format(turno.getCagHora());
    }
    String impresora = "SIN IMPRESORA";
    PrintService impresora11 = null;
    try {
      // inputStream = new FileInputStream("e:/archivo.txt");

      String cadena =
          "************************************* \n\n"
                      + "RESPONSABLE: "
                      + turno.getCagNombreUsuario()
                      + " \n"
                      + "FECHA: "
                      + fecha
                      + " \n"
                      + turno.getCagNombreInstitucion()
                      + "\n"
                      + turno
                          .getCexHorariosDia()
                          .getCexHorario()
                          .getCexEspecialidadMedico()
                          .getCexEspecialidad()
                          .getCesNombre()
                      + "\n"
                      + "NÚMERO CARPETA: "
                      + turno.getHosReferencia()
                  != null
              ? turno.getHosReferencia().getCexHistoriaInstitucion().getCicNumeroCarpeta()
              : turno.getCexHistoriaInstitucion().getCicNumeroCarpeta()
                          + "\n"
                          + "CÉDULA: "
                          + turno.getHosReferencia()
                      != null
                  ? turno
                      .getHosReferencia()
                      .getCexHistoriaInstitucion()
                      .getCexHistoriaClinica()
                      .getChcCedula()
                  : turno.getCexHistoriaInstitucion().getCexHistoriaClinica().getChcCedula()
                              + "\n"
                              + "TURNO: "
                              + turno.getCagSecuencia()
                              + "\t HORA: "
                              + hora
                              + " \n"
                              + "MEDICO: "
                              + turno
                                  .getCexHorariosDia()
                                  .getCexHorario()
                                  .getCexEspecialidadMedico()
                                  .getAdmMedico()
                                  .getAmeNombreFull()
                              + "\n"
                              + "PACIENTE: "
                              + turno.getHosReferencia()
                          != null
                      ? turno
                          .getHosReferencia()
                          .getCexHistoriaInstitucion()
                          .getCexHistoriaClinica()
                          .getChcNombreFull()
                      : turno.getCexHistoriaInstitucion().getCexHistoriaClinica().getChcNombreFull()
                          + "\n"
                          + "*******   TURNO        EXTRA    ********\n"
                          + "\n"
                          + "\n"
                          + "SE RECOMIENDA ESTAR 30 MIN ANTES\n"
                          + " DE LA HORA ESPECIFICADA\n"
                          + "\n";
      te2 = converteCaracter(cadena);
      tex = new ByteArrayInputStream(te2.getBytes("UTF-8"));

    } catch (UnsupportedEncodingException e) {
      log.error("printExtra() {} ", e.getMessage());
    }
    if (tex == null) {
      return;
    }

    DocFlavor docFormat = DocFlavor.INPUT_STREAM.AUTOSENSE;
    Doc document = new SimpleDoc(tex, docFormat, null);

    PrintRequestAttributeSet attributeSet = new HashPrintRequestAttributeSet();

    for (AdmDatosGlobales datoGlobal : listaImpresorasturnos) {
      if (user.getIp().equals(datoGlobal.getClave())) {
        impresora = datoGlobal.getValor();
        break;
      }
    }
    // PrintService impresora11 = PrintServiceLookup.lookupDefaultPrintService();
    PrintService[] printService = PrintServiceLookup.lookupPrintServices(null, null);
    for (PrintService printService1 : printService) {
      if (printService1.getName().equals(impresora)) {
        impresora11 = printService1;
      }
    }

    if (impresora11 != null) {
      DocPrintJob printJob = impresora11.createPrintJob();
      try {
        printJob.print(document, attributeSet);
      } catch (PrintException e) {
        log.error("printExtra() {} ", e.getMessage());
      }
    } else {
      log.error("No existen impresoras instaladas");
    }

    tex.close();
  }