How to print a PDF file directly from a node application?

I am creating a pdf from HTML in a nodejs application with phantomjs. This is good so far. Now I want to print this file directly on a network printer. I use the node -printer module and the print itself works. However, only in RAW format.

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);
   }
});

Some printers understand this when they send a PDF as RAW, but many of our printers (especially label printers, where the system is ultimately needed) do not understand PDF file formats, and I have to send them as EMF. I also tried TEXT, which did not work, so everything points to EMF.

node -imagemagick node -pdfium pdf emf, . Imagemagick , node -pdfium . , npm install pdfium. github , , , .

, PDF EMF imagemagick node -pdfium?

UPDATE

postscript ghostscript (pdf2ps) RAW , . , postscript level2:

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...
  }
);

()

%!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

[...]

ps RAW , . , error (gibberish = > ps ).

2

PostScript , RAW , , (). pdf , printername, require('child_process').exec(...). , .

+4

Source: https://habr.com/ru/post/1609804/


All Articles