4

Goal

I want to print a file via a PDF printer which isn't the default printer. I was able to temporary change the normal printer to the PDF printer.

Problem

But I don't know how to print a .doc, .txt or .xls via Jscript. Also, I can't find a way to save the default printer name so I can switch back after I've printed the file.

Jscript code

var objShell = new ActiveXObject("Shell.Application");
var objFSO = new ActiveXObject("Scripting.FileSystemObject");    

try {
  var PDFCreatorQueue = new ActiveXObject("PDFCreatorBeta.JobQueue");
  PDFCreatorQueue.Initialize();

  var sourceFile   = WScript.Arguments(0)
  var sourceFolder = objFSO.GetParentFolderName(sourceFile)
  var sourceName   = objFSO.GetBaseName(sourceFile)
  var targetFile   = sourceFolder + "\\" + sourceName + ".pdf"  

  //HERE GOES THE COMMAND TO SAVE THE CURRENT DEFAULT PRINTER NAME TO A TEMP VARIABLE
  objNet.SetDefaultPrinter("PDFCreator");
  //HERE GOES THE PRINT COMMAND WHICH I DON'T KNOW
 // HERE GOES THE COMMAND TO CHANGE BACK TO THE OLD DEFAULT PRINTER

  if(!PDFCreatorQueue.WaitForJob(3)) {
    WScript.Echo("The print job did not reach the queue within " + 3 + " seconds"); 
  }
  else {
    var job = PDFCreatorQueue.NextJob;  
    job.SetProfileByGUID("DefaultGuid");
    job.ConvertTo(targetFile);

    if(!job.IsFinished || !job.IsSuccessful) {
        WScript.Echo("Could not convert the file: " + targetFile);
    }
  }  
  PDFCreatorQueue.ReleaseCom();
}
catch(e) {
  WScript.Echo(e.message);
  PDFCreatorQueue.ReleaseCom();
}
Sindhoo Oad
  • 1,194
  • 2
  • 13
  • 29
nixda
  • 2,654
  • 12
  • 49
  • 82
  • I thought there were security measures in place to prevent an activeX object from running an executable? Might be wrong. – Craig Barben Dec 15 '14 at 07:02
  • Did you try `\"/nPDFCreator\"`? – Chronial Dec 15 '14 at 07:04
  • There shouldn't be a comma in those arguments. But as the docu says, PrintUiEntry does allow you to configure printers, not print stuff. – Chronial Dec 15 '14 at 07:19
  • @Chronial Is it possible to temporarily change the default printer, print the document and change the default back? If yes, how? – nixda Dec 15 '14 at 08:02
  • Did you have a look at the documentation you linked to? – Chronial Dec 15 '14 at 17:52
  • 1
    This is not a JScript issue. You can not just “print a file” – you need an application that can do that. This is the same in every programming language / framework. You might want to have a look at the word COM api if you want to print `.doc` files. – Chronial Dec 16 '14 at 01:09

2 Answers2

6

Use the ShellFolderItem.InvokeVerbEx() function. The JScript example code in the MSDN article shows how to use it. Make the first argument "print" and the second argument the name of the printer. So you can remove the code that tinkers with the default printer.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Seems promising. However, I tried the MSDN example and this method applies to all files in a folder. How do I limit this to a single file/argument? – nixda Dec 22 '14 at 09:22
  • Me again. Can you please have a look at [my code snippet?](http://pastie.org/private/yevo5brewqwqpzemjufja). It doesn't show any error, but its also not printing anything :( – nixda Dec 22 '14 at 12:15
  • I can't guess at a problem. Try the "open" verb and verify that this starts Word. Try it without the printer name argument. – Hans Passant Dec 22 '14 at 12:27
  • OK, I see my problem. While doc or docx is working with this method, other file types like txt or png are not. Despite that Windows is able to print them. So it seems there isn't a universal method. Anyway, you'll get the reward for not giving me up :) – nixda Dec 22 '14 at 12:44
0

Printing web page from js is quite easy, you could use window.print() method over an iFrame ( this works only with file format wich can be displaied into a web page so it doesn't work with .doc extension)

<iframe id="textfile" src="text.txt"></iframe>
<button onclick="print()">Print</button>
<script type="text/javascript">
function print() {
    var iframe = document.getElementById('textfile');
    iframe.contentWindow.print();
}
</script>

These will show you a message box to select what printer you want to use a so on. What are you asking for seems to be silent printing but it isn't standarized over all the broswer.

P.S. I think that isn't a good idea to use the printer to save this file to pdf, I think taht you could look at jsPDF (a js tools to create pdf) or you should consider to make the pdf generation serverside.

Matteo Corti
  • 484
  • 2
  • 13