I am trying to create a pdf file using the wkhtmltopdf utility.

Problem: pass command in cmd incorrectly (slashes "\" are deleted)

Question: How to pass the command? Tried to screen, apparently doing wrong.

(Below is the code of the executable file)

"use strict";//http://jsman.ru/express/ var http = require('http'), fs = require('fs'), url = require('url'), port = 8080, host = '127.0.0.1', express = require('express'), cmd = require('node-cmd'), path = require('path'), app = express(); app.set('views', __dirname + '/views'); app.set('view engine', 'jade'); app.get('/', function(req, res){res.send(200, "Hello, World")}); app.get('/pdf', function(req, res){ cmd.get( 'cd "C:\Program Files\wkhtmltopdf\bin" | wkhtmltopdf www.yandex.ru C:\Users\amstel\Desktop\yandex.pdf', function(data, err, stderr){ if (!err) { console.log('Done\n',data) } else { console.log('Error\n', err) } } ); }) var server = app.listen(port, host); console.log("Express server running on http://%s:%s", host, port); 

The result of this code:

enter image description here

Figure 2 (entering two commands does not work at once):

enter image description here

  • You say you tried to screen. How exactly did you try? Every `\` must be replaced with `\\`. - Yaant
  • @Yaant I try to enter it into cmd from the keyboard (see Figure 2) - in the first version it did not work (when I enter two commands separated by "|", it works only in the second case, if they are entered in turn) - pravosleva
  • First, you do not need here | , and & , and secondly, most likely, you don’t even need to do this, try executing the program right away, without cd , just indicating the full path: C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf www.yandex.ru C:\\Users\amstel\\Desktop\\yandex.pdf (Especially since there are surprises with cd if the program is suddenly on another logical drive.) - Yaant
  • Earned!) - pravosleva

1 answer 1

First, in JavaScript, backslash ( \ ) is used to escape special characters. In order to add this character to the string, it must also be escaped. In other words, in lines instead of \ you should use \\ . Like this:

 var p = 'C:\\Program Files\\wkhtmltopdf\\bin'; 

Secondly, if in the Windows command line, instead of the operator | need to use && . Like this:

 var cmd = 'cd "C:\\Program Files\\wkhtmltopdf\\bin" && wkhtmltopdf www.yandex.ru C:\\out.pdf' 

Moreover, instead of calling two commands in the console, you can easily do one:

 var cmd = '"C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf" www.yandex.ru C:\\out.pdf'; 
  • OK thanks! - pravosleva