J
jon
I'm trying to use a hidden iframe to print the contents of one div
seamlessly. Currently I can create the hidden iframe, copy the
contents of the div to the iframe, and print it. I even have a method
that initially copies all the original page's styles onto the new
iframe to maintain look and feel.
So far things work, and the div (along with look and feel from styles)
are successfully copied to the iframe and printing just the hidden
iframe works.
The problem I'm facing is that the printed page doesn't have any of the
styles applied that I copied across, even though they are visible in
the iframe when viewing it.
Anyone have any idea why this might be?
Below is the code, copied and pasted, for reference.
var printObj = document.createElement('iframe');
printObj.id = baseObj.id + 'BoxPrint';
printObj.style.width = document.body.clientWidth;
printObj.style.height = document.body.clientHeight;
printObj.style.display = 'none';
document.body.appendChild(printObj);
function printDiv(divObj){
// copy all parent styles to iframe for proper "look and feel"
copyStylesAcrossWin(top,this.printObj.contentWindow);
// now copy non-styled content in
printObj.contentWindow.document.body.innerHTML = divObj.innerHTML;
// just for debugging
printObj.style.display='';
alert(printObj.contentWindow.document.body.outerHTML);
}
function copyStylesAcrossWin(win1, win2){
if(win1 == null || win2 == null){ return; }
var linkNodeArr = win1.document.getElementsByTagName('link');
var headNodeObj = win2.document.getElementsByTagName('head').item(0);
for (var i = 0; i < linkNodeArr.length; i++) {
if (linkNodeArr.rel != null && linkNodeArr.rel == 'stylesheet')
{
var styleObj = win2.document.createElement('link');
var attribArr = linkNodeArr.attributes;
for (var j = 0; j < attribArr.length; j++){
styleObj.setAttribute(attribArr[j].nodeName,
attribArr[j].nodeValue);
}
headNodeObj.appendChild(styleObj);
}
}
}
seamlessly. Currently I can create the hidden iframe, copy the
contents of the div to the iframe, and print it. I even have a method
that initially copies all the original page's styles onto the new
iframe to maintain look and feel.
So far things work, and the div (along with look and feel from styles)
are successfully copied to the iframe and printing just the hidden
iframe works.
The problem I'm facing is that the printed page doesn't have any of the
styles applied that I copied across, even though they are visible in
the iframe when viewing it.
Anyone have any idea why this might be?
Below is the code, copied and pasted, for reference.
var printObj = document.createElement('iframe');
printObj.id = baseObj.id + 'BoxPrint';
printObj.style.width = document.body.clientWidth;
printObj.style.height = document.body.clientHeight;
printObj.style.display = 'none';
document.body.appendChild(printObj);
function printDiv(divObj){
// copy all parent styles to iframe for proper "look and feel"
copyStylesAcrossWin(top,this.printObj.contentWindow);
// now copy non-styled content in
printObj.contentWindow.document.body.innerHTML = divObj.innerHTML;
// just for debugging
printObj.style.display='';
alert(printObj.contentWindow.document.body.outerHTML);
}
function copyStylesAcrossWin(win1, win2){
if(win1 == null || win2 == null){ return; }
var linkNodeArr = win1.document.getElementsByTagName('link');
var headNodeObj = win2.document.getElementsByTagName('head').item(0);
for (var i = 0; i < linkNodeArr.length; i++) {
if (linkNodeArr.rel != null && linkNodeArr.rel == 'stylesheet')
{
var styleObj = win2.document.createElement('link');
var attribArr = linkNodeArr.attributes;
for (var j = 0; j < attribArr.length; j++){
styleObj.setAttribute(attribArr[j].nodeName,
attribArr[j].nodeValue);
}
headNodeObj.appendChild(styleObj);
}
}
}