Render HTML to an image

后端 未结 17 1769
栀梦
栀梦 2020-11-22 16:06

Is there a way to render html to image like PNG? I know that it is possible with canvas but I would like to render standard html element like div for example.

相关标签:
17条回答
  • 2020-11-22 16:12

    I know this is quite an old question which already has a lot of answers, yet I still spent hours trying to actually do what I wanted:

    • given an html file, generate a (png) image with transparent background from the command line

    Using Chrome headless (version 74.0.3729.157 as of this response), it is actually easy:

    "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" --headless --screenshot --window-size=256,256 --default-background-color=0 button.html
    

    Explanation of the command:

    • you run Chrome from the command line (here shown for the Mac, but assuming similar on Windows or Linux)
    • --headless runs Chrome without opening it and exits after the command completes
    • --screenshot will capture a screenshot (note that it generates a file called screenshot.png in the folder where the command is run)
    • --window-size allow to only capture a portion of the screen (format is --window-size=width,height)
    • --default-background-color=0 is the magic trick that tells Chrome to use a transparent background, not the default white color
    • finally you provide the html file (as a url either local or remote...)
    0 讨论(0)
  • 2020-11-22 16:13

    You can use an HTML to PDF tool like wkhtmltopdf. And then you can use a PDF to image tool like imagemagick. Admittedly this is server side and a very convoluted process...

    0 讨论(0)
  • 2020-11-22 16:18

    Yes. HTML2Canvas exists to render HTML onto <canvas> (which you can then use as an image).

    NOTE: There is a known issue, that this will not work with SVG

    0 讨论(0)
  • 2020-11-22 16:18

    The only library that I got to work for Chrome, Firefox and MS Edge was rasterizeHTML. It outputs better quality that HTML2Canvas and is still supported unlike HTML2Canvas.

    Getting Element and Downloading as PNG

    var node= document.getElementById("elementId");
    var canvas = document.createElement("canvas");
    canvas.height = node.offsetHeight;
    canvas.width = node.offsetWidth;
    var name = "test.png"
    
    rasterizeHTML.drawHTML(node.outerHTML, canvas)
         .then(function (renderResult) {
                if (navigator.msSaveBlob) {
                    window.navigator.msSaveBlob(canvas.msToBlob(), name);
                } else {
                    const a = document.createElement("a");
                    document.body.appendChild(a);
                    a.style = "display: none";
                    a.href = canvas.toDataURL();
                    a.download = name;
                    a.click();
                    document.body.removeChild(a);
                }
         });
    
    0 讨论(0)
  • 2020-11-22 16:19

    Use html2canvas just include plugin and call method to convert HTML to Canvas then download as image PNG

            html2canvas(document.getElementById("image-wrap")).then(function(canvas) {
                var link = document.createElement("a");
                document.body.appendChild(link);
                link.download = "manpower_efficiency.jpg";
                link.href = canvas.toDataURL();
                link.target = '_blank';
                link.click();
            });
    

    Source: http://www.freakyjolly.com/convert-html-document-into-image-jpg-png-from-canvas/

    0 讨论(0)
  • 2020-11-22 16:20

    Use this code, it will surely work:

    <script type="text/javascript">
     $(document).ready(function () {
    	 setTimeout(function(){
    		 downloadImage();
    	 },1000)
     });
     
     function downloadImage(){
    	 html2canvas(document.querySelector("#dvContainer")).then(canvas => {
    		a = document.createElement('a'); 
    		document.body.appendChild(a); 
    		a.download = "test.png"; 
    		a.href =  canvas.toDataURL();
    		a.click();
    	});	 
     }
    </script>

    Just do not forget to include Html2CanvasJS file in your program. https://html2canvas.hertzen.com/dist/html2canvas.js

    0 讨论(0)
提交回复
热议问题