cordova create C:\Kapsel_Projects\PrinterDemo com.mycompany.printer PrinterDemo
cd C:\Kapsel_Projects\PrinterDemo
cordova platform add android
cordova create ~/Documents/Kapsel_Projects/PrinterDemo com.mycompany.printer PrinterDemo
cd ~/Documents/Kapsel_Projects/PrinterDemo
cordova platform add ios
cordova plugin add de.appplant.cordova.plugin.printer --searchpath %KAPSEL_HOME%/plugins or cordova plugin add de.appplant.cordova.plugin.printer --searchpath $KAPSEL_HOME/plugins or cordova plugin add https://github.com/katzer/cordova-plugin-printer.git
<html
<head>
<title>Printer Demo</title>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script>
window.onerror = onError;
document.addEventListener("deviceready", init, false);
var printer = null;
function onError(msg, url, line) {
var idx = url.lastIndexOf("/");
var file = "unknown";
if (idx > -1) {
file = url.substring(idx + 1);
}
navigator.notification.alert("An error occurred in " + file + " (at line # " + line + "): " + msg);
return false; //suppressErrorAlert;
}
function init() {
printer = cordova.plugins.printer;
document.getElementById("thedate").value = new Date().toLocaleString();
}
function isPrinterAvailable() {
log("checking printer availability");
printer.isAvailable(callbackAvailable);
}
function callbackAvailable(isAvailable) {
if (isAvailable) {
log("Printer is available");
}
else {
log("Printer is not available");
}
}
function print(original) {
var page;
if (original) {
page = location.href; //notice the log lines are not shown
}
else {
var body = document.body;
page = body;
}
log("about to print " + page);
printer.print(page, {name:"Index.html Print Job"}, printJobFinishedOrCancelled);
}
function printOtherPage() {
var page = "http://weather.gc.ca/city/pages/on-82_metric_e.html";
log("about to print " + page);
printer.print(page, {name:"Forecast Print Job"}, printJobFinishedOrCancelled);
}
function printJobFinishedOrCancelled() {
log("Printing finished or cancelled");
}
//new in SP11
function printInTheBackground() {
//printer.printFiles(["pdf.pdf", "pict.jpg"], {name:"BGPrintJob"}, printJobFinishedOrCancelled);
printer.printFiles(["pdf.pdf"], {name:"BGPrintJob"}, printJobFinishedOrCancelled);
}
function log(line) {
var results = document.getElementById("printer_results");
results.innerHTML+= "<br>" + line;
}
</script>
</head>
<body>
<h1>Printer Sample</h1>
<button onclick="isPrinterAvailable()">Is Printer Available</button>
<button onclick="print(true)">Print Original Page</button>
<button onclick="print(false)">Print Current Page</button>
<button onclick="printOtherPage()">Print Other Page</button>
<button onclick="printInTheBackground()">Background Print</button>
<input type="text" id="thedate">
<div id="printer_results"></div>
</body>
</html>
cordova run android
or
cordova run ios
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
26 | |
25 | |
21 | |
12 | |
9 | |
8 | |
8 | |
8 | |
8 | |
8 |