Hello,

I need to know this one. How can we get printer status using JavaScript and also how can we give MS word document print command using JavaScript.

Dani AI

Generated

Short answer: a plain webpage script cannot reliably read printer hardware state or open-and-print arbitrary Word files on client machines. Browser JS only exposes window.print() for the currently loaded page; direct COM/Win32 access from a page is blocked by modern browser security (the old IE/ActiveX route is fragile and IE-only). Window.print (MDN). (developer.mozilla.org)

Given your LAN use-case and the validation requirement, two practical, supported architectures work well in production:

  • Central print server: users upload documents, the server validates page size/margins/page count (convert to PDF first), then submits jobs to the print queue (CUPS or Windows spooler). Use headless LibreOffice or a server library (or commercial engines like Aspose.Words) for conversion/inspection — and avoid automating desktop Office from unattended server code (Microsoft does not recommend Office interop on servers). [LibreOffice CLI docs] [Considerations for server-side Automation of Office]. (manpages.ubuntu.com)

  • Local helper/agent on student PCs: a small signed service or native helper (or browser extension + native messaging) exposes a local API the web app calls. The helper can query local printers via WMI/Win32_Printer, use Word automation locally to read page settings if needed, and submit print jobs — giving a dependable printer status (paper/ready/printing). For network printers you can also poll SNMP / Printer MIB (RFC 3805) or query a central IPP/CUPS server. [Chrome Native Messaging] [Win32_Printer] [Printer MIB]. (developer.chrome.com)

Practical POC command (server-side conversion):

soffice --headless --convert-to pdf --outdir /tmp mydoc.doc

Checklist: prefer .docx when possible (page size/margins are in OOXML w:pgSz), convert to PDF for deterministic printing, secure and sign any client helper, and do not rely on browser-only JS for hardware control. [OOXML w:pgSz] [CUPS IPP]. (datypic.com)

Notes tied to earlier replies: ’s idea of converting to a printable format is on point for simplifying printing; was right that in-browser JS is sandboxed — the reliable solutions are central conversion/print or a trusted local agent.

Recommended Answers

All 3 Replies

perhaps not a direct answer but to determine if the local pc

  • has an application that can read the particular version .doc file you intend to print
  • has a printer
  • has security settings that allows scripts to take over their hardware (MALWARE ALERT)

it would be better to output the .doc as html then printing is easy

Hi bob,

Thank you for your suggestion. But my client requirement is to print a word doc. He need a printer management system. The client has a Local Area Network in his infrastructure, where his students make word document and those documents are to be printed.

The doc should be validated before it is printed (doc should meet prerequisite page settings). When the document is page setting meets the prerequisite, the system should send the document to the printer...

That system also should get the status of the printer. Depending upon the printer status the system should change its functionality.

So, I need JScript that could:

1)Get printer status(out of paper, ready and printing)
2)Read doc page settings(number of pages, page size and margins)
3)Finally, give doc print command

perhaps not a direct answer but to determine if the local pc

  • has an application that can read the particular version .doc file you intend to print
  • has a printer
  • has security settings that allows scripts to take over their hardware (MALWARE ALERT)

it would be better to output the .doc as html then printing is easy

I don't think you can do that with JS. For security reason, JS should not be able to deeply touch inside client's PC. A website shouldn't be able to force your PC to print all your doc, or shutdown your PC.

You may try to use server side script such as PHP. PHP is able to communicate with COM and working with Word documents.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.