Closed BKStephens closed 8 years ago
Calling printLabel()
immediately after definition not guarantee that iframe is fully loaded.
You can do some sort of check to be the document fully loaded (including iframe) and then call printLabel()
.
Here is simple example to move printLabel()
in body onLoad
:
<html>
<head>
</head>
<body onload='printLabel();'>
<iframe src='iframe.html'
name="checkin_label_203096115"
frameborder='0'
height='100'
width='100'
style='visibility: hidden; position:absolute; top:-200px; left:-200px;'>
</iframe>
<script type="text/javascript">
function printLabel() {
window.frames['checkin_label_203096115'].focus();
jsPrintSetup.setGlobalOption('DEBUG', true);
jsPrintSetup.setPrinter("PDFCreator");
jsPrintSetup.setOption('orientation', jsPrintSetup.kPortraitOrientation);
jsPrintSetup.setOption('headerStrLeft', '');
jsPrintSetup.setOption('headerStrCenter', '');
jsPrintSetup.setOption('headerStrRight', '');
jsPrintSetup.setOption('footerStrLeft', '');
jsPrintSetup.setOption('footerStrCenter', '');
jsPrintSetup.setOption('footerStrRight', '');
jsPrintSetup.setOption('printBGColors', 1);
jsPrintSetup.setPaperSizeUnit(jsPrintSetup.kPaperSizeMillimeters);
// jsPrintSetup.setOption('paperHeight',59);
// jsPrintSetup.setOption('paperWidth', 102);
jsPrintSetup.setOption('marginTop', 0);
jsPrintSetup.setOption('marginRight', 0);
jsPrintSetup.setOption('marginBottom', 0);
jsPrintSetup.setOption('marginLeft', 0);
jsPrintSetup.setOption('scaling', 100);
jsPrintSetup.clearSilentPrint();
jsPrintSetup.setOption('printSilent', true);
jsPrintSetup.printWindow(window.frames['checkin_label_203096115']);
}
</script>
</body>
</html>
Thank you for your response, @mitkola!
I tried
<html>
<head>
</head>
<body onload='printLabel();'>
<iframe src='iframe.html'
name="checkin_label_203096115"
frameborder='0'
height='100'
width='100'
style='visibility: hidden; position:absolute; top:-200px; left:-200px;'>
</iframe>
<script type="text/javascript">
function printLabel() {
window.frames['checkin_label_203096115'].focus();
jsPrintSetup.setGlobalOption('DEBUG', true);
// jsPrintSetup.setPrinter("PDFCreator");
jsPrintSetup.setOption('orientation', jsPrintSetup.kPortraitOrientation);
jsPrintSetup.setOption('headerStrLeft', '');
jsPrintSetup.setOption('headerStrCenter', '');
jsPrintSetup.setOption('headerStrRight', '');
jsPrintSetup.setOption('footerStrLeft', '');
jsPrintSetup.setOption('footerStrCenter', '');
jsPrintSetup.setOption('footerStrRight', '');
jsPrintSetup.setOption('printBGColors', 1);
jsPrintSetup.setPaperSizeUnit(jsPrintSetup.kPaperSizeMillimeters);
jsPrintSetup.setOption('paperHeight',59);
jsPrintSetup.setOption('paperWidth', 102);
jsPrintSetup.setOption('marginTop', 0);
jsPrintSetup.setOption('marginRight', 0);
jsPrintSetup.setOption('marginBottom', 0);
jsPrintSetup.setOption('marginLeft', 0);
jsPrintSetup.setOption('scaling', 100);
jsPrintSetup.clearSilentPrint();
jsPrintSetup.setOption('printSilent', true);
jsPrintSetup.printWindow(window.frames['checkin_label_203096115']);
}
</script>
</body>
</html>
and I get:
Error:
message:Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIWebBrowserPrint.print]
result:2147500037
name:NS_ERROR_FAILURE
filename:jar:file:///C:/Users/IEUser/AppData/Roaming/Mozilla/Firefox/Profiles/3l0bags5.default/extensions/jsprintsetup@edabg.com.xpi!/components/jsPrintSetup.js
lineNumber:1550
columnNumber:0
data:null
stack:jsPrintSetup.prototype.printWindow@jar:file:///C:/Users/IEUser/AppData/Roaming/Mozilla/Firefox/Profiles/3l0bags5.default/extensions/jsprintsetup@edabg.com.xpi!/components/jsPrintSetup.js:1550:4
printLabel@file:///C:/Users/IEUser/Desktop/test3_mitkola.html:36:9
onload@file:///C:/Users/IEUser/Desktop/test3_mitkola.html:1:1
location:JS frame :: jar:file:///C:/Users/IEUser/AppData/Roaming/Mozilla/Firefox/Profiles/3l0bags5.default/extensions/jsprintsetup@edabg.com.xpi!/components/jsPrintSetup.js :: jsPrintSetup.prototype.printWindow :: line 1550
unless I comment out:
jsPrintSetup.setOption('paperWidth', 102);
and then it prints "Hello World"
One thing to note is that not all printers are effected.
Can you print manually to this printer and set desired paper.
After that go to about:config
and get values of print.*
to see what are actual values of settings.
For Windows it is important paperData
also.
From preferences I see that paperData=123
for your label.
You can try to jsPrintSetup.setOption('paperData', 123)
or better use jsPrintSetup.definePaperSize
.
Enhanced Paper data handling
For example you can try with this code:
<html>
<head>
</head>
<body onload='printLabel();'>
<iframe src='iframe.html'
name="checkin_label_203096115"
frameborder='0'
height='100'
width='100'
style='visibility: hidden; position:absolute; top:-200px; left:-200px;'>
</iframe>
<script type="text/javascript">
function printLabel() {
window.frames['checkin_label_203096115'].focus();
jsPrintSetup.setGlobalOption('DEBUG', true);
jsPrintSetup.setPrinter("PDFCreator");
jsPrintSetup.setOption('orientation', jsPrintSetup.kPortraitOrientation);
jsPrintSetup.setOption('headerStrLeft', '');
jsPrintSetup.setOption('headerStrCenter', '');
jsPrintSetup.setOption('headerStrRight', '');
jsPrintSetup.setOption('footerStrLeft', '');
jsPrintSetup.setOption('footerStrCenter', '');
jsPrintSetup.setOption('footerStrRight', '');
jsPrintSetup.setOption('printBGColors', 1);
// jsPrintSetup.setPaperSizeUnit(jsPrintSetup.kPaperSizeMillimeters);
// jsPrintSetup.setOption('paperHeight',59);
// jsPrintSetup.setOption('paperWidth', 102);
// Define your paper size. Important part for Windows is paperData=123
// you can try also to set width=-1 and height=-1
// As I see in about:config ..paperWidth=-1 and ...paperHeight=-1
jsPrintSetup.definePaperSize(123, 123, 'cust_label_1', 'cust_label_1', 'Custom Label 1', 102, 59, jsPrintSetup.kPaperSizeMillimeters);
jsPrintSetup.setPaperSizeData(123);
jsPrintSetup.setOption('marginTop', 0);
jsPrintSetup.setOption('marginRight', 0);
jsPrintSetup.setOption('marginBottom', 0);
jsPrintSetup.setOption('marginLeft', 0);
jsPrintSetup.setOption('scaling', 100);
jsPrintSetup.clearSilentPrint();
jsPrintSetup.setOption('printSilent', true);
jsPrintSetup.printWindow(window.frames['checkin_label_203096115']);
}
</script>
</body>
</html>
I tried jsPrintSetup.setOption('paperData', 123)
along with jsPrintSetup.setOption('paperWidth', 102)
,
<html>
<head>
</head>
<body onload='printLabel();'>
<iframe src='iframe.html'
name="checkin_label_203096115"
frameborder='0'
height='100'
width='100'
style='visibility: hidden; position:absolute; top:-200px; left:-200px;'>
</iframe>
<script type="text/javascript">
function printLabel() {
window.frames['checkin_label_203096115'].focus();
jsPrintSetup.setGlobalOption('DEBUG', true);
jsPrintSetup.setOption('orientation', jsPrintSetup.kPortraitOrientation);
jsPrintSetup.setOption('headerStrLeft', '');
jsPrintSetup.setOption('headerStrCenter', '');
jsPrintSetup.setOption('headerStrRight', '');
jsPrintSetup.setOption('footerStrLeft', '');
jsPrintSetup.setOption('footerStrCenter', '');
jsPrintSetup.setOption('footerStrRight', '');
jsPrintSetup.setOption('printBGColors', 1);
jsPrintSetup.setPaperSizeUnit(jsPrintSetup.kPaperSizeMillimeters);
jsPrintSetup.setPaperSizeData(123);
jsPrintSetup.setOption('paperHeight',59);
jsPrintSetup.setOption('paperWidth', 102);
jsPrintSetup.setOption('marginTop', 0);
jsPrintSetup.setOption('marginRight', 0);
jsPrintSetup.setOption('marginBottom', 0);
jsPrintSetup.setOption('marginLeft', 0);
jsPrintSetup.setOption('scaling', 100);
jsPrintSetup.clearSilentPrint();
jsPrintSetup.setOption('printSilent', true);
jsPrintSetup.printWindow(window.frames['checkin_label_203096115']);
}
</script>
</body>
</html>
but I still got this error:
Error:
message:Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIWebBrowserPrint.print]
result:2147500037
name:NS_ERROR_FAILURE
filename:jar:file:///C:/Users/IEUser/AppData/Roaming/Mozilla/Firefox/Profiles/3l0bags5.default/extensions/jsprintsetup@edabg.com.xpi!/components/jsPrintSetup.js
lineNumber:1550
columnNumber:0
data:null
stack:jsPrintSetup.prototype.printWindow@jar:file:///C:/Users/IEUser/AppData/Roaming/Mozilla/Firefox/Profiles/3l0bags5.default/extensions/jsprintsetup@edabg.com.xpi!/components/jsPrintSetup.js:1550:4
printLabel@file:///C:/Users/IEUser/Desktop/test4.html:36:9
onload@file:///C:/Users/IEUser/Desktop/test4.html:1:1
location:JS frame :: jar:file:///C:/Users/IEUser/AppData/Roaming/Mozilla/Firefox/Profiles/3l0bags5.default/extensions/jsprintsetup@edabg.com.xpi!/components/jsPrintSetup.js :: jsPrintSetup.prototype.printWindow :: line 1550
I then tried jsPrintSetup.definePaperSize(123, 123, 'cust_label_1', 'cust_label_1', 'Custom Label 1', 102, 59, jsPrintSetup.kPaperSizeMillimeters);
and jsPrintSetup.setPaperSizeData(123);
without jsPrintSetup.setOption('paperWidth', 102)
<html>
<head>
</head>
<body onload='printLabel();'>
<iframe src='iframe.html'
name="checkin_label_203096115"
frameborder='0'
height='100'
width='100'
style='visibility: hidden; position:absolute; top:-200px; left:-200px;'>
</iframe>
<script type="text/javascript">
function printLabel() {
window.frames['checkin_label_203096115'].focus();
jsPrintSetup.setGlobalOption('DEBUG', true);
// jsPrintSetup.setPrinter("PDFCreator");
jsPrintSetup.setOption('orientation', jsPrintSetup.kPortraitOrientation);
jsPrintSetup.setOption('headerStrLeft', '');
jsPrintSetup.setOption('headerStrCenter', '');
jsPrintSetup.setOption('headerStrRight', '');
jsPrintSetup.setOption('footerStrLeft', '');
jsPrintSetup.setOption('footerStrCenter', '');
jsPrintSetup.setOption('footerStrRight', '');
jsPrintSetup.setOption('printBGColors', 1);
// jsPrintSetup.setPaperSizeUnit(jsPrintSetup.kPaperSizeMillimeters);
// jsPrintSetup.setOption('paperHeight',59);
// jsPrintSetup.setOption('paperWidth', 102);
// Define your paper size. Important part for Windows is paperData=123
// you can try also to set width=-1 and height=-1
// As I see in about:config ..paperWidth=-1 and ...paperHeight=-1
jsPrintSetup.definePaperSize(123, 123, 'cust_label_1', 'cust_label_1', 'Custom Label 1', 102, 59, jsPrintSetup.kPaperSizeMillimeters);
jsPrintSetup.setPaperSizeData(123);
jsPrintSetup.setOption('marginTop', 0);
jsPrintSetup.setOption('marginRight', 0);
jsPrintSetup.setOption('marginBottom', 0);
jsPrintSetup.setOption('marginLeft', 0);
jsPrintSetup.setOption('scaling', 100);
jsPrintSetup.clearSilentPrint();
jsPrintSetup.setOption('printSilent', true);
jsPrintSetup.printWindow(window.frames['checkin_label_203096115']);
}
</script>
</body>
</html>
and it works! We will refactor to use definePaperSize instead of "jsPrintSetup.setOption('paperWidth', ...".
Thank you for your help, and for your work on jsprintsetup!
This issue can be reproduced on Windows with Firefox 47.0 and DYMO LabelWriter 450 Turbo. test.html and iframe.html need to share the same parent directory in order for this to work. Open test.html to see "An error occured while printing" and
If I comment out "jsPrintSetup.setOption('paperWidth', 102);", it prints.
test.html
iframe.html