title: How to Print PDF file directly from node application?
body: <p>I am creating a pdf-document from HTML in my nodejs application with phantomjs. This works totally well so far. Now I want to directly print this file to a network printer. I am using the node-printer module and the printing itself works. However only in RAW format. </p>
  
  <pre><code>var format = 'RAW';
  var printerName = '\\\\PRNTSRV\\Kyo Copy Printer';
  var data = fs.readFileSync('./tmp/phantom.tmp.pdf');
  
  printer.printDirect({
     data: data,
     printer: printerName,
     type: format,
     success: function(id) {
         console.log('printing done with id', id);
         res.json({
            success: true,
            message: 'printed at ' + printerName
         });
     },
     error: function(err) {
         console.log('error on printing: ' + err);
     }
  });
  </code></pre>
  
  <p>Some of the printer understand it when they get send the pdf document as RAW, however a lot of our printer (especially the label printers where the system finally is needed) don't understand the PDF file formats, and I have to send them as EMF. I tried TEXT as well that did not work, so it all points to EMF.</p>
  
  <p>I tried node-imagemagick and node-pdfium to convert the pdf to emf but I couldn't get both to run. Imagemagick produced empty documents and I did not get node-pdfium to compile. It breaks everytime while I use <em>npm install pdfium</em>. There is an open issue on the github page for this type of error, but no response since april, so I guess I've to find another alternative.</p>
  
  <p>Now my question, how can I convert the PDF file to EMF format without using either imagemagick nor node-pdfium?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I am now generating a postscript file with ghostscript (pdf2ps) and try to send it as RAW to several printer, but this does not work either. I am generating the postscript like this, which works fine and produces a valid level2 postscript file:</p>
  
  <pre><code>var exec = require('child_process').exec;
  exec(
    '"' + config.gs_binary + '" ' 
    +  '-dNOPAUSE -dBATCH -sDEVICE=ps2write '
    +  '-sOutputFile=' + outFile '
    +  inputFile
  , function(err, stdout,stderr) {
      console.log(err, stdout, stderr);
  
      // continue with sending to printer with RAW and printer.printDirect and    
      // using the ps file instead of the pdf file...
    }
  );
  </code></pre>
  
  <p>the content of the postscript file (excerpt)</p>
  
  <pre><code>%!PS-Adobe-3.0
  %%BoundingBox: 0 0 216 111
  %%HiResBoundingBox: 0 0 216.00 111.00
  %%Creator: GPL Ghostscript 915 (ps2write)
  %%LanguageLevel: 2
  %%CreationDate: D:20151002103717+02'00'
  %%Pages: 1
  %%EndComments
  %%BeginProlog
  /DSC_OPDFREAD true def
  /SetPageSize true def
  /EPS2Write false def
  currentdict/DSC_OPDFREAD known{
  currentdict/DSC_OPDFREAD get
  }{
  false
  }ifelse
  10 dict begin
  
  [...]
  </code></pre>
  
  <p>After sending the ps document as RAW to the printer, it still recognizes it but the results vary from printer to printer. e.g. they deny to print, they show a protocolerror or they print lots of pages of gibberish (gibberish => not the ps content as text).</p>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>Since I did not get the PostScript-files send by RAW to the printers at all I did another approach that worked (for now). I generate the pdf as usual and then send it to an external binary together with the printername that handles the printing part with <code>require('child_process').exec(...)</code>. Let's see when I will encounter the first limitations. </p>