快速打印HTML5canvas

我想将canvas图像直接发送/打印到默认打印机。 这意味着快速打印。

任何人都可以提示。

Javascript或jQuery。

我搜索了很多,找到了一个完美的解决方案:)使用了onclick事件

function printCanvas() { var dataUrl = document.getElementById('anycanvas').toDataURL(); //attempt to save base64 string to server using this var var windowContent = ''; windowContent += '' windowContent += 'Print canvas'; windowContent += '' windowContent += ''; windowContent += ''; windowContent += ''; var printWin = window.open('','','width=340,height=260'); printWin.document.open(); printWin.document.write(windowContent); printWin.document.close(); printWin.focus(); printWin.print(); printWin.close(); } 

我发现第一次打印时,canvas是空白的。 我添加了一个事件监听器,等待加载图像/文档。 现在canvas已准备好每次打印。 这是为我工作的代码:

 const dataUrl = document.getElementById('the-pdf-canvas').toDataURL(); let windowContent = ''; windowContent += ''; windowContent += 'Print canvas'; windowContent += ''; windowContent += ''; windowContent += ''; windowContent += ''; 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); 

我快速搜索了这个并找到了这个链接 。 它提供了关于如何操作的breif教程,但基本上你得到一个带有: toDataURL()的引用URL,然后创建一个大小相同的img,将其src分配给url。

它可以更好地逐步完成你的html和css中的操作,所以如果你不理解我所说的话,只需查看链接 。

注意:我认为您无法与canvas进行交互以进行打印,但如果您在使用打印机驱动程序时遇到问题,那么这对您来说可能毫无用处。