Printing a .PDF file using different printers in Java

I am trying to write integration for a corporate system.

There is a web server that is used by many clients from two different places. There are two network printers installed on this server.

What I want to do is print PDF documents on these printers. I want the program to send a document to the printer where it was requested.

I can determine the place where the request was made. However, I cannot set the default printer at runtime.

Since this is a web server that runs in the background, I cannot fill out the printer dialog box and let the user select a printer. I should be able to install a printer to be used programmatically.

Currently, I can see registered printers in the system using PrinterJob.lookupPrintServices();, and I can configure the service using the requested printer, but this does not change the default printer, and the system continues to print to the default printer.

Please give me your ideas on how to achieve it.

+5
source share
1 answer

The more research on the Internet I solved my problem. I give it here to those who may need it;

I made a solution from this website:

http://webmoli.com/2008/11/03/java-print-pdf/

. , -, PdfRenderer.jar- : , PrintPdf.java, , , .

:

/**
* Sets the printer service to be used for printing
*
* @param argPrintServiceName
* @throws PrinterException
*/
public void setPrintService(String argPrintServiceName) throws PrinterException {
PrintService[] printServices = PrinterJob.lookupPrintServices();
int i;
for (i = 0; i < printServices.length; i++) {
if (printServices[i].getName().equalsIgnoreCase(argPrintServiceName)) {
printerJob.setPrintService(printServices[i]);
break;
}
}
if (i == printServices.length) {
throw new PrinterException("Invalid print service name: " + argPrintServiceName);
}
}

. printerJob , . , PrintService :

 public static PrintService setPrintService(String argPrintServiceName) throws PrinterException {
        PrintService psr = null;
    PrintService[] printServices = PrinterJob.lookupPrintServices();
    int i;
    for (i = 0; i < printServices.length; i++) {
    if (printServices[i].getName().equalsIgnoreCase(argPrintServiceName)) {
        psr = printServices[i];          
    break;
    }
    }
    if (i == printServices.length) {
    throw new PrinterException("Invalid print service name: " + argPrintServiceName);
    }
    return psr;
    }

:

PrintService ps = setPrintService("Printer Name Here");

;

:

PrintPdf printPDFFile = new PrintPdf(fis, "Test Print PDF");

PrintPdf printPDFFile = new PrintPdf(fis, "Test Print PDF", ps);

, :

public PrintPdf(byte[] content, String jobName, PrintService ps) throws  
        IOException, PrinterException 
        {
        initialize(content, jobName, ps);
    } 



 public PrintPdf(InputStream inputStream, String jobName, PrintService ps)
    {
     *
     *
     initialize(pdfContent, jobName, ps);
    }

pjob: pjob.setPrintService(ps);

private void initialize(byte[] pdfContent, String jobName, PrintService ps) throws      
 IOException, PrinterException 
 {
  *
  *
        pjob = PrinterJob.getPrinterJob();
        pjob.setPrintService(ps);
  *
  *
  ...
 }

, -pdf.

+8

All Articles