HTML2canvas generates Blurry images

前端 未结 8 1430
庸人自扰
庸人自扰 2020-12-01 06:45

I am using jsPDF and it uses html2canvas to generate an image from some html element and insert on the .pdf file. But there is a problem on html2canvas, it generates blurry

相关标签:
8条回答
  • 2020-12-01 07:02

    I had this problem because I was on a retina display. I solved in by using MisterLamb's solution here.

    $(window).load(function () {
    
        var scaleBy = 5;
        var w = 1000;
        var h = 1000;
        var div = document.querySelector('#screen');
        var canvas = document.createElement('canvas');
        canvas.width = w * scaleBy;
        canvas.height = h * scaleBy;
        canvas.style.width = w + 'px';
        canvas.style.height = h + 'px';
        var context = canvas.getContext('2d');
        context.scale(scaleBy, scaleBy);
    
        html2canvas(div, {
            canvas:canvas,
            onrendered: function (canvas) {
                theCanvas = canvas;
                document.body.appendChild(canvas);
    
                Canvas2Image.saveAsPNG(canvas);
                $(body).append(canvas);
            }
        });
    });
    

    HTML and PNG without scaling

    HTML and PNG with scaling

    0 讨论(0)
  • 2020-12-01 07:09

    I was facing this problem and i solved it by using domtoimage instead of html2canvas.

    This HTML2CANVAS solution was not working good for me i know the scale option does increase the target div's size before capturing it but it won't work if you have something inside that div which won't resize e.g in my case it was canvas for my editing tool.

    Anyway for this i opted for domtoimage and trust me i think that this is the best solution of them all.

    I didn't had to face any problem of html2canvas for example:

    need to be at the top of webpage so html2canvas can capture the shot completely and low dpi problem

    function print()
    {
        var node = document.getElementById('shirtDiv');
        var options = {
            quality: 0.95
        };
    
        domtoimage.toJpeg(node, options).then(function (dataUrl)
        {
            var doc = new jsPDF();
            doc.addImage(dataUrl, 'JPEG', -18, 20, 240, 134.12);
            doc.save('Test.pdf');
        });
    }
    

    Cdn for dom to image:

    https://cdnjs.com/libraries/dom-to-image

    Cdn for jspdf:

    https://cdnjs.com/libraries/jspdf

    0 讨论(0)
  • 2020-12-01 07:12

    I have found out my problem. Happens that my screen is a Retina Display, so when the canvas2html will render the HTML, due to the difference of pixel density on retina screen, the image is rendered blurred.

    Found out the solution here:

    https://github.com/cburgmer/rasterizeHTML.js/blob/master/examples/retina.html

    0 讨论(0)
  • 2020-12-01 07:20

    solution is very simple, after X hours of testing.

    Set your ALL div's 2x higher, your IMG 2x higher, and finally set html zoom on 0.5, or if you want better quality yet, set 3x higher (in this case the html zoom must be 0.33) or more, (the original image sizes are assumed to be larger).

    For example:

    HTML

    <body>
     <div class="pdf">
       <img src="image.jpg">
     </div>
    </body>
    

    CSS before

    body {
        background: #b2b2b2;
    }
    .pdf {
       background: #fff;
       /* A4 size */
       width: 842px;
       height: 595px;
     }
    img {
       width: 300px;
       height: 200px;
    }
    

    CSS after (only changes)

    html {
       zoom: 0.5;
    }
    
    .pdf {
       /* A4 size before . 2 */
       width: 1684; 
       height: 1190px; 
     }
    img { /* size before . 2 */
       width: 600px;
       height: 400px;
    }
    

    AND here is my result:

    PDF before PDF after

    0 讨论(0)
  • 2020-12-01 07:21

    If anyone is still looking for a solution to work for them I had success by setting scale: 5. Check it out here in their documentation. https://html2canvas.hertzen.com/configuration

    0 讨论(0)
  • 2020-12-01 07:24

    This is what fixed it for me. And it wasn't because I was using a retina display (because I don't have one):

    https://github.com/niklasvh/html2canvas/issues/576

    Just change the getBounds() method in html2canvas.js with this one:

     function getBounds (node) {
            if (node.getBoundingClientRect) {
                var clientRect = node.getBoundingClientRect();
                var width = node.offsetWidth == null ? clientRect.width : node.offsetWidth;
                return {
                    top   : Math.floor(clientRect.top),
                    bottom: Math.floor(clientRect.bottom || (clientRect.top + clientRect.height)),
                    right : Math.floor(clientRect.left + width),
                    left  : Math.floor(clientRect.left),
                    width : width,
                    height: node.offsetHeight == null ? clientRect.height : node.offsetHeight
                };
            }
            return {};
        }
    
    0 讨论(0)
提交回复
热议问题