几个星期以来,我一直在努力创建功能,以使我能够打印我的制作画布。我试过的方法mentioned here没有运气。我可以进入打印对话框,然而,它是空白的标题和页面信息。谁能给我指明正确的方向?
我目前正在使用:
// Trying to print
function printCanvas()
{
var dataUrl = document.getElementById('c').toDataURL(); //attempt to save base64 string to server using this var
var windowContent = '<!DOCTYPE html>';
windowContent += '<html>'
windowContent += '<head><title>Print canvas</title></head>';
windowContent += '<body>'
windowContent += '<img src="' + dataUrl + '">';
windowContent += '</body>';
windowContent += '</html>';
var printWin = window.open('','','width=340,height=260');
printWin.document.open();
printWin.document.write(windowContent);
printWin.document.close();
printWin.focus();
printWin.print();
printWin.close();
}<button onclick="printCanvas()">Print</button>

发布于 2017-11-14 05:27:13
您需要在内容加载后执行打印操作。下面的代码应该可以工作
function printCanvas() {
var dataUrl = document.getElementById('c').toDataURL(); //attempt to save base64 string to server using this var
var windowContent = '<!DOCTYPE html>';
windowContent += '<html>'
windowContent += '<head><title>Print canvas</title></head>';
windowContent += '<body>'
windowContent += '<img src="' + dataUrl + '" onload=window.print();window.close();>';
windowContent += '</body>';
windowContent += '</html>';
var printWin = window.open('', '', 'width=340,height=260');
printWin.document.open();
printWin.document.write(windowContent);
}https://stackoverflow.com/questions/47272407
复制相似问题