Der*_*rin 8 javascript jquery html5 canvas
我想将画布图像直接发送/打印到默认打印机.这意味着快速打印.
任何人都可以提示.
Javascript或jQuery.
Raz*_*aza 26
我搜索了很多,找到了一个完美的解决方案:)使用onclick事件
function printCanvas()
{
var dataUrl = document.getElementById('anycanvas').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();
}
Run Code Online (Sandbox Code Playgroud)
小智 16
我发现第一次打印时,画布是空白的.我添加了一个事件监听器,等待加载图像/文档.现在画布已准备好每次打印.这是为我工作的代码:
const dataUrl = document.getElementById('the-pdf-canvas').toDataURL();
let windowContent = '<!DOCTYPE html>';
windowContent += '<html>';
windowContent += '<head><title>Print canvas</title></head>';
windowContent += '<body>';
windowContent += '<img src="' + dataUrl + '">';
windowContent += '</body>';
windowContent += '</html>';
const printWin = window.open('', '', 'width=' + screen.availWidth + ',height=' + screen.availHeight);
printWin.document.open();
printWin.document.write(windowContent);
printWin.document.addEventListener('load', function() {
printWin.focus();
printWin.print();
printWin.document.close();
printWin.close();
}, true);
Run Code Online (Sandbox Code Playgroud)
使用printJS和这一行:
printJS({printable: document.querySelector("#your-canvas").toDataURL(), type: 'image', imageStyle: 'width:100%'});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
27294 次 |
| 最近记录: |