How to print a file using jscript

purpose

I want to print a file through a PDF printer, which is not the default printer. I was able to temporarily change my regular printer to a PDF printer.

Problem

But I do not know how to print .doc, .txt or .xls through Jscript. Also, I can’t find a way to keep the default printer name, so I can go back after I print 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();
}
+4
source share
2 answers

ShellFolderItem.InvokeVerbEx(). JScript MSDN , . "", - . , , .

+6

- js , window.print() iFrame ( , -, .doc)

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

, . , , , , .

P.S. , pdf, , jsPDF ( js pdf), , PDF .

0

All Articles