java web call printer to print pdf file

The company recently requested the development of a marking system, and it took some time to call the printer.

1. Compilation tools and environment: eclipse + jdk1.8 + tomcat8.5 + ireport5.0.1 + jasperreports4.5.1 + Firefox browser

2. There are two ways to call the printer:

Here is to generate a jasper file after designing the template with ireport, call the jasper file in the background and transfer the data to generate a pdf file, and put it in the temp folder under the project root directory. Path, for example: D: \ xxx \ apache-tomcat-8.5.53 \ webapps \ project name \ temp \ xxx.pdf, 
put the code directly below.

(1) Call the printer on the server side (Advantage: You can print with one key at a time. Disadvantage: you need to share the printer with the client to the server. The operation and maintenance colleagues say that if the server is powered off and restarted, the printer must be reconfigured, so this is not my final plan)

       String printName = "xxxx"; // The name of the printer 
            boolean isChoose = false ; // Whether there is a corresponding printer 
            File file = new File (pdfPath); 
            PDDocument document = PDDocument.load (file); // Read the pdf file 
            PrinterJob job = PrinterJob.getPrinterJob ();   // Create a print job
             // Traverse the names of all printers to get the specified printer 
            for (PrintService ps: PrinterJob.lookupPrintServices ()) { 
                String psName = ps.toString ();
                 if (psName.equals (printName)) { 
                    isChoose= true ; 
                    job.setPrintService (ps); 
                    break ; 
                } 
            } 
            if (isChoose) { 
                job.setPageable ( new PDFPageable (document)); 
                Paper paper = new Paper (); 
                paper.setSize ( 227, 142); // set Print paper size: length (mm) * 72 / 25.4 I am here 80mm * 50mm 
                paper.setImageableArea (0, 0, paper.getWidth (), // Set print position / coordinates 
                        paper.getHeight ()); 
                PageFormat pageFormat = new PageFormat ();
                pageFormat.setPaper (paper); 
                Book book = new Book (); 
                 // Set whether some attributes scale the number of printed sheets (document.getNumberOfPages () is the number of pages in the pdf file), etc., here select the actual size 
                book.append ( new PDFPrintable (document, Scaling.ACTUAL_SIZE), pageFormat, document.getNumberOfPages ()); 
                job.setPageable (book); 
                job.print (); // Start printing 
            }
             // Delete file 
            if (file.exists ()) { 
                file. delete (); 
            }

 

(2) Call the client (local) printer (js implements the browser to call the printer) (Advantage: directly call the local printer, no configuration is required. Disadvantages: a print prompt box will pop up, one more step of operation)

Call the background to generate a pdf file, and pass the file path to the foreground. Add the following code to the page. Here I display it on the page, which is equivalent to a print preview (I do n’t want to display it. I add display: none to the style. The printout is blank, there is time to debug later)

<iframe  style="font-weight:bold;width:100%;height:300px" id="printIframe"></iframe>

 Add the following code in the trigger method of clicking the print button

var arr = data.pdfPath.split ("project name \\"); <!-data.pdfPath is the pdf path returned from the foreground, we only need to get the path after the project 
name- > $ ("# printIframe ") .attr (" src ", arr [1]); <!-Load the src of the iframe, load the pdf file 
	in- > 
setTimeout (function () { document.getElementById ('printIframe'). contentWindow.print (); 
	}, 500);

 After clicking, a printing prompt will pop up, if it is the default printer, click print (the printer needs to set the paper size first)

The printer that called the client found on the Internet that the lodop plug-in can be used to achieve one-click printing, but the print content will automatically add a few words of the trial version, which needs to be purchased before it can be removed, or it can be removed after previewing, but still more One step operation, so I did not proceed to the next step.

Another method is to install the Adobe reader on the client side, java background calls to execute AcroRd32.exe to print directly , this can be achieved, but because the Adobe reader I installed opens the label pdf file only displays the QR code, other content is blank, Prompt the font problem, because of the time relationship, I gave up. If you need it, you can find the specific operation online.

 

Guess you like

Origin www.cnblogs.com/Mrshuang11/p/12759794.html