HTML2canvas generates Blurry images

73,573

Solution 1

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

Solution 2

you can use scale options in html2canvas.

In the latest release, v1.0.0-alpha.1, you can use the scale option to increase the resolution (scale: 2 will double the resolution from the default 96dpi).

// Create a canvas with double-resolution.
html2canvas(element, {
    scale: 2,
    onrendered: myRenderFunction
});
// Create a canvas with 144 dpi (1.5x resolution).
html2canvas(element, {
    dpi: 144,
    onrendered: myRenderFunction
});

Solution 3

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

enter image description here

HTML and PNG with scaling

enter image description here

Solution 4

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

Solution 5

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

Share:
73,573

Related videos on Youtube

jgabrielfaria
Author by

jgabrielfaria

Front-end developer at Pagar.me.

Updated on July 09, 2022

Comments

  • jgabrielfaria
    jgabrielfaria almost 2 years

    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 images from the html. See example below:

    HTML content:

    http://puu.sh/7SZz4.png

    html2canvas generated image:

    http://puu.sh/7SZAT.png

    Is there any way to fix it or is there any better option to get the image form html?

    thanks!

    • Mikko Ohtamaa
      Mikko Ohtamaa about 10 years
      Have you checked whether this concerns all web browsers?
    • GameAlchemist
      GameAlchemist about 10 years
      i would try to set imageSmoothingEnabled to false (beware of vendor prefixes)
  • Mohammad Faizan khan
    Mohammad Faizan khan about 9 years
    i am too facing this blur image problem but i didnt get your answer can u elaborate more
  • Martin Peter
    Martin Peter about 9 years
    Canvas2Image cannot improve the image quality if html2canvas already generates a low quality image, which is the problem in the first place.
  • Shai UI
    Shai UI over 8 years
    note that although it makes it better than what it was it still doesn't completely make it perfect.
  • kashesandr
    kashesandr almost 8 years
    Please make the solution more evident :)
  • kashesandr
    kashesandr almost 8 years
    @YoYo actually no. There is no proper solution to print PDFs from all kind of HTML+SVG with only front-end stack. But you may configure the stuff to work with some simple web pages. You probably should use Phantomjs for PDF-printing functionality, its much more stable but requires an additional server. Also there are online services where you may provide a public url and get the pdf back. Also you're welcome to write the pdf-printing lib for front-end :)
  • gyochum
    gyochum over 7 years
    what version of the html2canvas library are you using for this fix?
  • Tanuj Dhaundiyal
    Tanuj Dhaundiyal about 7 years
    Works pefectly for me. Make sure you include this version of html2canvas script to make it work.
  • saadk
    saadk about 7 years
    this is not generating the way is should be.. i makes a 702 size into 800 in width, but when image generates, it shows smaller and cut from top and right side in image.
  • saadk
    saadk about 7 years
    @crclayton: also what is the difference between "canvas.width" and "canvas.style.width" ?
  • Caleb Pitman
    Caleb Pitman almost 7 years
    i have same problem as saadk.
  • Ricky sharma
    Ricky sharma over 5 years
    dpi option not using in latest release.so use scale for high quality image
  • Ishan Fernando
    Ishan Fernando almost 5 years
    I think the newer version keep quality while scaling. I used version 1 with scale config and work perfectly
  • CChoma
    CChoma over 3 years
    I've been trying to fix my blurry PDFs for hours. Due to the various possible combinations of jspdf and html2canvas, I was not able to find any js solution that worked. However, this CSS solution works perfectly for my needs. Thanks for such a simple answer.
  • adir1521
    adir1521 over 2 years
    your suggestion of domtoimage worked like a charm for me. html2canvas was a terrible pain. domtoimage solved the issue of pixels. Thanks a lot :)