2012-11-27 41 views
1

我正在使用html2Canvas.js將HTML元素轉換爲畫布,然後將畫布轉換爲圖像使得瀏覽器可以打印,這是打印HTML的正確方法嗎?我發現圖像是在任何打印機上獲得類似打印效果的最佳方式。如何使用JS打印HTML部分?

+0

有沒有什麼辦法可以預覽或修改HTML並保存狀態? –

+1

最好的做法是將au @ @ print'css'更簡單 – DoubleYo

+0

也許它可以幫助你:http://stackoverflow.com/questions/923885/capture-html-canvas-as-gif-jpg- PNG,PDF格式 –

回答

0

您可以使用JavaScript函數打印

<div onlick='window.print()'>Print</div> 
0

如果這個代碼添加到你的CSS:

.toPrint { 
    display:none; 
    width: 100%; 
    position: fixed; 
    top: 0; 
    left: 0; 
} 

@media print { 
    * { 
    display: none !important; 
    } 

    .toPrint { 
    display: block !important; 
    } 

} 

當你要打印視圖更新使用此代碼:

/** code borrowed from @MicrosoftGoogle */ 
var canvas = document.getElementById("mycanvas"); 
var img = canvas.toDataURL("image/png"); 

if (document.getElementsByClassName("toPrint")) { 
    document.getElementsByClassName("toPrint")[0].src = img; 
else 
    document.write('<img class="toPrint" src="'+img+'"/>');