nwjs / nw.js

Call all Node.js modules directly from DOM/WebWorker and enable a new way of writing applications with all Web technologies.
https://nwjs.io
MIT License
40.37k stars 3.89k forks source link

How to print a file? #4427

Closed shamun closed 8 years ago

shamun commented 8 years ago

I have a KIOSK where 5 printers connected. Now, i need to tell Node-Webkit to print on printer 1 or printer 5.

//"Brother HL-6180DW series"
//"OKI-MC342-A60F25"
//"Foxit PhantomPDF Printer"
//"Fax"
//"DYMO LabelWriter 450"

Step 1: PHP server which is doing barcode, A4 printing preparation task for example: http://www.printing.com/printPDF?commands

Step 2: KIOSK users push the button in Node-Webkit. it has to be with no popup, but silent printing. example: BARCODE Code39, Code128Auto or A4 page

Step 3: How can now node-webkit, Go to the server URL http://www.printing.com/printPDF?what-to-print=A4, get the content in local system (C:\page-from-server.pdf) and silently print that file to the A4 printer "Brother HL-6180DW series"

Please advise.

shamun commented 8 years ago

How can i do this with node-webkit? i just wrote it in Java

import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.printing.PDFPageable;

public class JPrint {

  public static boolean saveFile(URL url, String file) throws IOException {
    boolean download_status = false;

    System.out.println("[OK] - open");
    InputStream in = url.openStream();
    FileOutputStream fos = new FileOutputStream(new File(file));
    System.out.println("[OK] - reading file...");
    int length = -1;
    byte[] buffer = new byte[1024];

    while ((length = in.read(buffer)) > -1) {
        fos.write(buffer, 0, length);
    }
    fos.close();
    in.close();

    download_status = true;
    System.out.println("[OK] - downloaded");
    return download_status;
  }

  public static void main(String[] args) throws IOException, PrinterException {    
    String downloaded_filename = "C:/Users/tpt/Downloads/pdf.pdf";
    String download_pdf_from = "https://github.com/msysgit/msysgit/releases/download/Git-1.9.2-preview20140411/Git-1.9.2-preview20140411.exe";
    String downloaded_filename_open_as_pdf = "C:\\Users\\tpt\\Downloads\\pdf.pdf";
    String printerNameDesired = "DYMO LabelWriter 450"; // Brother HL-6180DW series

    // Get printers
    PrintService[] services = PrinterJob.lookupPrintServices();
    DocPrintJob docPrintJob = null;

    try{
      URL url = new URL(download_pdf_from);

      if(saveFile(url, downloaded_filename)) {
        try {
          PDDocument pdf = PDDocument.load(new File(downloaded_filename_open_as_pdf));
          PrinterJob job = PrinterJob.getPrinterJob();
          for (int i = 0; i < services.length; i++) {
           if (services[i].getName().equalsIgnoreCase(printerNameDesired)) {
             docPrintJob = services[i].createPrintJob();
           }
          }

          job.setPrintService(docPrintJob.getPrintService());
          job.setPageable(new PDFPageable(pdf));
          //docPrintJob = service[i].createPrintJob();
          job.print();

        } catch (Exception e) {
          System.out.println("[FAIL]" + e);
        }      
      } else {
        System.out.println("[FAIL] - download fail");
      }      
    } catch (Exception ae) {
      System.out.println("[FAIL]" + ae);
    }

  }
}
rogerwang commented 8 years ago

This is now supported. See #4911 #5036