Remote Driven Printing PDF

Keywords: Java snapshot Apache network Nginx

Recently received a demand: in order to improve business efficiency when printing PDF forms, packers do not need to download PDF manually and then operate printing manually, to achieve automatic drive printer to print PDF.

Idea: The PDF side exists on the server alone, and the printer is connected to the packer's computer. Install a print interface service on each packer's computer (remotely acquire PDF and save it locally, then directly drive the printer to print, print and delete the local files). When the business process comes to the need to print PDF, call the corresponding interface (each packer corresponds to a print interface) to print the PDF form.

First determine how to print PDF:

Refer to Apache's pdfbox-tools open source project to drive the printer to print local PDF files (remote PDF files are not supported).

        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox-tools</artifactId>
            <version>2.0.11</version>
        </dependency>      

Printing PDF requires only calling the main method of the PrintPDF class.

  public static void main(String[] args) {//test
        String printerName = "PDFCreator";//Printer name
        String pdfFilePath = "D:/pdf/39488.pdf";//pdf Route
        try {
            PrintPDF.main(new String[] { 
                "-silentPrint",// Silent printing
                // "-password","abcde",//pdf Open password
                "-printerName", printerName,// Specify the printer name
                "-orientation", "auto",// Printing direction, auto|landscape|portrait Three options
                pdfFilePath // Printing PDF Document Path
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Then write a print interface service. A simple Spring-Boot interface project downloads pdf from remote (erp services to get pdf files, directly drive printer printing) to print interface local, and then directly prints, prints and deletes local files. Then type it into a jar package and get pdfPrintSoa-0.0.1-SNAPSHOT.jar

//Print Interface Service
@RestController
@EnableAutoConfiguration
@RequestMapping("/")
public class PdfController {
    
    //From remote ( erp Service get pdf Document, Direct Drive Printer Printing) Download pdf To print interface local, then print directly, print finished, delete local files
    @RequestMapping("printPdfByUrl")
    @ResponseBody
    public boolean printPdfByUrl(@RequestBody PdfParam pdf) {
        String orderNo = null;
        try {
            String printerName = pdf.getPrinterName();//Printer name
            String url = pdf.getUrl();//pdf Long-range url
            orderNo = pdf.getOrderNo();
            System.out.println("printerName:" + printerName + ", orderNo:" + orderNo);
            if(null == printerName || url == null || orderNo == null) {
                System.out.println("Parameters cannot be null");
                return false;
            }
            //pdf Download to local temporary storage
            String pdfFilePath = "C:/pdf/" + orderNo + ".pdf";
            downloadNet(url,pdfFilePath);// Download network files
            if(!new File(pdfFilePath).exists()){
                System.out.println(pdfFilePath + " not exist!");
                return false;
            }
       PrintPDF.main(
new String[]{ "-silentPrint",//Silent printing // "-password","abcde",//pdf Open password "-printerName",printerName ,//Specify the printer name "-orientation","auto",//Printing direction, auto|landscape|portrait Three options pdfFilePath//Printing PDF Document Path }); deleteFile(pdfFilePath);//Delete printed files System.out.println("orderNo:" + orderNo + "pdf Face sheet printing success!"); return true; }catch(Exception e){ e.printStackTrace(); System.out.println("orderNo:" + orderNo + "pdf Face sheet printing failed!"); } return false; } // Download network files private static void downloadNet(String sourceFileUrl,String destFilePath){ int bytesum = 0; int byteread = 0; InputStream inStream = null; FileOutputStream fs = null; try { URL url = new URL(sourceFileUrl); URLConnection conn = url.openConnection(); inStream = conn.getInputStream(); fs = new FileOutputStream(destFilePath); byte[] buffer = new byte[1024*4]; while ((byteread = inStream.read(buffer)) != -1) { bytesum += byteread; System.out.println(bytesum); fs.write(buffer, 0, byteread); } } catch (Exception e) { e.printStackTrace(); } finally{ try { if(inStream != null){ inStream.close(); } if(fs != null){ fs.close(); } } catch (IOException e) { e.printStackTrace(); } } } private static void deleteFile(String filePath) { File file = new File(filePath); if (file.exists() && file.isFile()) { file.delete(); } } public static void main(String[] args) {//test String printerName = "PDFCreator";//Printer name //pdf Route String pdfFilePath = "D:/pdf/39488.pdf"; try { PrintPDF.main(new String[]{ "-silentPrint",//Silent printing // "-password","abcde",//pdf Open password "-printerName",printerName ,//Specify the printer name "-orientation","auto",//Printing direction, auto|landscape|portrait Three options pdfFilePath//Printing PDF Document Path }); } catch (PrinterException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }

nginx configuration, set up virtual directory through alias, so that you can access PDF directly and remotely through the URL (print interface service is such a remote download of PDF).

        location /static {
            alias   /home/pdf/;
        }    

For example, the domain name of nginx configuration is www.liu.com. Visiting http://www.liu.com/static/39488.pdf actually points to/home/pdf/394488.pdf.

 

Finally, write a DOS batch file (pdf printer start. bat) to the packer to start the print interface service.

PS: First, put the jar package pdfPrintSoa-0.0.1-SNAPSHOT.jar generated by the print interface project into the pdf clip, and the pdf clip into the c disk.

@echo off
cd c:\pdf 
java -jar pdfPrintSoa-0.0.1-SNAPSHOT.jar

After the printing interface service starts, when PDF needs to be printed, the corresponding printing interface service of the packer can be invoked through Http to realize the remote drive printing of PDF sheets.

Posted by overlordhu on Tue, 29 Jan 2019 16:00:14 -0800