2011-02-25 19 views
7

Estoy tratando de configurar un servicio que se ejecuta por la noche para imprimir un montón de facturas y otros documentos de forma automática a un montón de impresoras. A partir de ahora puedo imprimir bien los documentos, pero necesito poder especificar una bandeja (una con el membrete de nuestra empresa y otra con papel blanco estándar). Todo lo que he intentado hasta ahora no ha funcionado en absoluto, especifico el atributo MediaTray en el conjunto PrintRequestAttribute, pero parece que no hace nada. ¿Alguien tuvo alguna experiencia con algo como esto?Cómo imprimir un pdf en una bandeja específica sin interacción del usuario en java

Mi código actual que estoy usando para las pruebas tiene este aspecto.

// Create a PDFFile from a File reference 
File f = new File("C:\\File.pdf"); 
FileInputStream fis = new FileInputStream(f); 
FileChannel fc = fis.getChannel(); 
ByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); 
PDFFile pdfFile = new PDFFile(bb); // Create PDF Print Page 
PDFPrintPage pages = new PDFPrintPage(pdfFile); 

// Create Print Job 
PrinterJob pjob = PrinterJob.getPrinterJob(); 
PageFormat pf = PrinterJob.getPrinterJob().defaultPage(); 
pjob.setJobName(f.getName()); 
Book book = new Book(); 
book.append(pages, pf, pdfFile.getNumPages()); 
pjob.setPageable(book); 
// Send print job to default printer 


PrintRequestAttributeSet aset=new HashPrintRequestAttributeSet(); 
aset.add(MediaTray.MIDDLE); //Used several of the tray options here 
pjob.print(aset); 
+0

posible duplicado de [Impresión con atributos (control de la bandeja, Duplex, etc ...) utilizando la biblioteca javax.print] (http://stackoverflow.com/questions/14328012/printing-with-attributestray-control -duplex-etc-using-javax-print-library) –

Respuesta

0

¿Qué es lo que realmente utiliza para imprimir el PDF? Enviar un PDF directamente a la impresora solo funciona si la impresora es compatible directamente con PDF. De lo contrario, necesita rasterizar con una biblioteca Java. Hay un artículo de blog que sugiere formas de imprimir PDF desde JAva al http://www.jpedal.org/PDFblog/2010/01/printing-pdf-files-from-java/

+0

Estoy usando [PDF Renderer] (http://java.net/projects/pdf-renderer/) para convertir el archivo PDF en PS para enviarlo a la impresora . – Galbrezu

2

Uso el informe de jaspe. Aquí está el código.

public void runReport() 
{ 
    JasperReport jasperReport; 
    JasperPrint jasperPrint; 
    try 
    { 
     jasperReport = JasperCompileManager.compileReport("C:/temp/jtest.jrxml");  
     jasperPrint = JasperFillManager.fillReport(jasperReport, new HashMap(), new JREmptyDataSource());  

     PrinterJob job = PrinterJob.getPrinterJob(); 
     /* Create an array of PrintServices */ 
     PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null); 

     PageFormat pf = PrinterJob.getPrinterJob().defaultPage(); 
     job.defaultPage(pf); 
     int selectedService = 0; 


     String theUserPrinterName = "\\\\office1\\printer1"; 
     AttributeSet attrSet = new HashPrintServiceAttributeSet(new PrinterName(theUserPrinterName, null)); 
     services = PrintServiceLookup.lookupPrintServices(null, attrSet); 
     try { 
      job.setPrintService(services[selectedService]); 
     } catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
     PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet(); 
     printRequestAttributeSet.add(MediaSizeName.NA_LETTER); 
     printRequestAttributeSet.add(new Copies(1)); 

     exporter = new JRPrintServiceExporter(); 
     exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 
     /* We set the selected service and pass it as a paramenter */ 
     exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE, services[selectedService]); 
     exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET, services[selectedService].getAttributes()); 
     exporter.setParameter(JRPrintServiceExporterParameter.PRINT_REQUEST_ATTRIBUTE_SET, printRequestAttributeSet); 
     exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG, Boolean.FALSE); 
     exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, Boolean.FALSE); 
     exporter.exportReport(); 
    } 
    catch (JRException e) 
    { 
     System.out.println("Caught exception!!!"); 
     e.printStackTrace(); 
     exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, Boolean.TRUE); 
     try { 
      exporter.exportReport(); 
     } 
     catch (JRException e2) 
     { 
      e2.printStackTrace(); 
     } 
    } 
Cuestiones relacionadas