基于PrintNode JS API实现浏览器静默打印的技术求助
Implementing Silent Remote Printing with PrintNode-JS in Browsers
It looks like your current code is focused on listening to scale measurements, so let's refactor it to handle silent remote printing—the core functionality you're trying to implement. PrintNode's WebSocket API is the best approach here since it enables real-time communication with the PrintNode client, which is necessary for reliable silent printing.
Here's a corrected, focused script that handles authentication, fetches available printers, and sends a silent print job:
<script type='text/javascript'> var API_KEY = '<your_valid_api_key_here>'; // Replace with your actual PrintNode API key // Check if WebSocket is supported (preferred for real-time printing) if (PrintNode.WebSocket.isSupported()) { // Callback triggered once authenticated with PrintNode function authenticated(authData) { if (authData.error) { console.error("Authentication failed:", authData.error); return; } console.log("Authenticated successfully. Account ID:", authData.accountId); // Fetch all available printers linked to your account ws.printers({}, function(printers) { if (printers.length === 0) { console.error("No printers found. Make sure PrintNode client is running on a machine with printers."); return; } // Select the first printer (you can modify this to pick a specific one by ID/name) const targetPrinter = printers[0]; console.log("Using printer:", targetPrinter.name); // Define your print job (example: print a PDF from a URL) const printJob = { printerId: targetPrinter.id, title: "Silent Print Job", contentType: "pdf_uri", // Options: pdf_uri, pdf_base64, raw, etc. content: "https://example.com/your-document.pdf", // Replace with your document URL options: { silent: true // Enable silent printing (no user prompt) } }; // Send the print job ws.printJob(printJob, function(jobResponse) { if (jobResponse.error) { console.error("Failed to send print job:", jobResponse.error); } else { console.log("Print job sent successfully. Job ID:", jobResponse.id); } }); }); // Optional: Subscribe to print job status updates ws.subscribe("printjob", function(jobEvent) { console.log("Print job status update:", jobEvent); }); } // Handle errors during WebSocket connection/communication function errorCallback(err, data) { console.error("PrintNode error occurred:", err, data); } // Initialize WebSocket connection with your API key var ws = new PrintNode.WebSocket( { apiKey: API_KEY }, authenticated, errorCallback ); } else { // Fallback to HTTP API if WebSocket isn't supported (less ideal for silent printing) console.warn("WebSocket not supported. Using HTTP API fallback."); var httpOptions = { error: function(responseBody) { console.error("HTTP API error:", responseBody); } }; var http = new PrintNode.HTTP( new PrintNode.HTTP.ApiKey(API_KEY), httpOptions ); // Fetch printers via HTTP http.printers().then(function(printers) { if (printers.length === 0) { console.error("No printers found."); return; } const targetPrinter = printers[0]; const printJob = { printerId: targetPrinter.id, title: "Silent Print Job (HTTP)", contentType: "pdf_uri", content: "https://example.com/your-document.pdf", options: { silent: true } }; // Send print job via HTTP http.printJob(printJob).then(function(jobResponse) { console.log("Print job sent via HTTP. Job ID:", jobResponse.id); }).catch(function(err) { console.error("HTTP print job failed:", err); }); }).catch(function(err) { console.error("Failed to fetch printers via HTTP:", err); }); } </script>
Key Notes for Success:
- API Key: Replace
<your_valid_api_key_here>with your actual PrintNode API key (generate this in your PrintNode account settings). - PrintNode Client: Ensure the PrintNode client is installed and running on the target machine (the one with the printer you want to use). The client must be logged into your PrintNode account.
- Content Type: Adjust
contentTypeandcontentbased on your document type. For example, usepdf_base64if you're passing a base64-encoded PDF instead of a URL. - Silent Printing: The
silent: trueoption in the print job ensures no user prompt appears—this requires the PrintNode client to have permission to print silently (enabled by default if you're using your own API key). - CORS: If you're hosting this script on a different domain than PrintNode's API, ensure your PrintNode account allows CORS for your domain (check account settings).
内容的提问来源于stack exchange,提问作者Abhishek Sharma
相关产品推荐
相关产品推荐

