2014å¹´ã«ããªã£ã¦Webãã¼ã¸ãã¾ã¨ãã«å°å·ããæ¹æ³ãç¥ããªã俺ãã¡ã¯ï¼ãããã¯ãã¨ããã¦ã§ããã¨ã³ã¸ãã¢ã®éãã®è¨é²ï¼
åç¥ãwindow.printã¨ããé¢æ°ãJavaScriptããå¼ã³åºãã°ããã§ããå°å·ãã¤ã¢ãã°ã表示ããã¦ãä»ç®ã®åã«è¡¨ç¤ºããã¦ããã¦ã§ãããã¼ã¸ãå°å·ãããã¨ãåºæ¥ãããã ã
ããã¨ãããããã®æ©è½ããã®ã¾ã¾ä½¿ãã¨ãããããä½ããè²ã ãããããmedia="print"ãªã¹ã¿ã¤ã«ã·ã¼ãã§æå®ããbackground-colorããåæ ãããªããããã©ã¦ã¶ã®å°å·ãªãã·ã§ã³ã§åºæ¥ããã¨ããåãããããåã®ç°å¢ã§ã¯ã¡ããã¨è²ãåºãªãã£ãã
ããã¯ä¸ä½ã©ããªã£ã¦ãããã ï¼2014å¹´ã«ããªã£ã¦ã俺éã¯ã¦ã§ãããã¼ã¸ããèªåããã¾ç®ã®åã«ãã¦ãããã®ã¾ã¾ã®å§¿ã§å°å·ãããã¨ããåºæ¥ãªãã®ãï¼HTML5æè¡ã®ã«ã³ãã¡ã¬ã³ã¹ãéããããWebSocketã§ãã£ãããä½ã£ãããã¦ãå ´åãããªãï¼äºä»¶ã¯ã«ã³ãã¡ã¬ã³ã¹ã§èµ·ãã¦ããããããªãã俺ã®å®¶ã®ããªã³ã¿ã¼ã®åã§èµ·ãã¦ãããã ï¼
ã²ã³ããç»åãããç»åã使ãããã
ã¨ããããã§åã¯ã¢ãã¨é©ãã¯ã¼ã¯ã¢ã©ã¦ã³ããæ¢ãããã®æ ã«åºãããã ããä½ã¨ãªãã¾ã¨ããªå°å·æ©è½ãæä¾ãã¦ããããªGoogleã¨ããä¼ç¤¾ã®ãGoogleã«ã¬ã³ãã¼ã¨ãããµã¼ãã¹ã«ã¤ãã¦ããã£ã¨èª¿ã¹ã¦ã¿ãã
ã©ããããGoogleã«ã¬ã³ãã¼ã¯ãä»è¦ã¦ããã¦ã§ãããã¼ã¸ãããã®ã¾ã¾å°å·ããã®ã§ã¯ãªããä¸æ¦ç»åãçæãã¦ããã®ç»åã表示ãããã¼ã¸ãå°å·ãããã¨ã§åé¿ãã¦ããããã ãã³ã¼ãããã©è¦ããã¨ããã"Print"ãã¿ã³ãæ¼ããéã«iframeã§ã¢ã¬ã³ã¬ãã¦ããç¯ãããã
ãããããã ï¼ç»åã¯å°å·ã§ãã®ã¾ã¾è²ãè¼ãã®ã ãç»åãªããã¹ã¿ã¤ã«ã·ã¼ããã©ãã®ããã®ã¨æ©ããã¨ããªããæã社ãããã§è¡ããã
ç»åãã©ããã£ã¦ä½ããï¼
ã§ãã ãç»åã¯ã©ããã£ã¦ä½ãï¼Googleã¯ããããã«ã¬ã³ãã¼ã®ãã¼ã¿ãç»åã¨ãã¦ã¬ã³ããªã³ã°ãããããªå é¨APIãæã£ã¦ããã®ã ãããããã¯ééããªãã¯ãã¹ãã©ã¦ã¶ãªã½ãªã¥ã¼ã·ã§ã³ã ãããèªåã§ãããªãã®ãç¨æããã®ã¯é¢åãããã¦ä»æ¹ããªãã
è¦ã¯ãä»è¦ã¦ãããã¼ã¸ã®DOMããä½ããã®å½¢ã§imgã®srcã«ã¶ã£è¾¼ããURLã«å¤æã§ããã°ããããã ããããã¡ãã£ã¨è¿é ã ãcanvasã§ãããã
html2canvasã¨ããã©ã¤ãã©ãªãããã試ããæãããç¨åº¦ã¾ã¨ãã«åãããã ãããã使ããã
html2canvasã使ã£ã¦ãDOM -> canvas -> Data URIã¨ããå¤æãè¡ã
html2canvasã使ãã¨ã以ä¸ã®ãããªæãã§ãããã®ã§ã¯ãªããã
- html2canvasã§ãç¾å¨ã®ãã¼ã¸ã®DOMï¼ã¾ãã¯ãã®ä¸é¨ï¼ãcanvas elementã«å¤æãã
- canvas elementã®toDataURL()ã§Data URIãå¾ã
- same originãã¤ä¸å¯è¦ãªiframeãéãï¼ã¾ãã¯äºãéãã¦ããï¼ããã®ä¸ã®image elementã®srcã«è¦ªframeããå ã»ã©ã®Data URIãã¶ã£è¾¼ã
- iframe.contentWindow.print()ãå¼ã³åºã
å®ç§ãããªãããæãè»ã¯ã
å®éã®ã³ã¼ã
ãªãã¨ãªããChrome 33.0.1750.70 betaã§åããã¨ã¯ç¢ºèªããã
function printWithCanvas() { html2canvas(document.getElementsByTagName("main")[0], { onrendered: function(canvas) { var iframe = document.getElementById("print-iframe"); var iframeWindow = iframe.contentWindow; var imageElement = iframeWindow.document.getElementById("canvas-image"); if ( typeof canvas.toBlob === "function" ) { var blobURL = URL.createObjectURL(canvas.toBlob()); imageElement.src = blobURL; } else { var dataURL = canvas.toDataURL(); imageElement.src = dataURL; } iframeWindow.print(); } }); };
iframeã®ä¸å³ã¯ã以ä¸ã®ãããªæãã®HTMLã«ãã¦ããã
<!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title></title> <style type="text/css"> #canvas-image { width: 100%; height: 100%; } </style> <script type="text/javascript"> </script> </head> <body> <img id="canvas-image" src="" alt="" /> </body> </html>
HTMLCanvasElementã®toBlobãããå ´åã¯ããã使ããæ¯è¼çè¯å¿çãã©ã¦ã¶ã¼ã§ããã¨ããã®Firefoxã«ã¯æ¢ã«æè¼ããã¦ããããã ãcanvasãimageã¨ãã¦æ±ãéã«Data URIã«ããããã·ãªã¢ã©ã¤ãºããªãã¦ã¯ãããªãã®ã¯ã¨ã£ã¦ãéå¹çãªã®ã§ããã®APIã¯ã¨ã£ã¨ã¨canvasã®è¼ã£ã¦ããå ¨ã¦ã®ãã©ã¦ã¶ã¼ã§å®è£ ããã¦ã»ããã
canvasãã¾ã¨ãã«åããªãã¬ã¬ã·ã¼ãªãã©ã¦ã¶ã§ã¯ãuuCanvasã使ãã°ããã ãããã¾ã ã試ãã¦ããªãã...
ã¾ã¨ã
- window.printã§ãã®ã¾ã¾ãã¼ã¸ãå°å·ããã®ã¯å³ãã
- html2canvasã¯ãããªãã«åã
- ãã©ã¦ã¶ã¼ã¯ãã¹ãããHTMLCanvasElementã®toBlobãå®è£ ãã¹ã
ä»åã¯é§ã足ã§èããã¤å®è£ ãã¾ãããããã£ã¨ããæ¹æ³ãããã°æãã¦é ãããã§ãã