skip to Main Content

I’m trying to export the div toto into HD images:

  • images should have HD resolution (I really don’t care if the files are heavy)
  • the div should be exported to multiple images with fixed height (defined in the script) and fixed width (div "toto" width)
  • final format should be jpg/jpeg.

The script works, except that the images are blurry, text is blurry… so images cannot be used for print.

Even putting dpi 600 doesn’t have any effect.

Actually both SCALE and DPI have no effect on last version of html2canvas.

I wrote a script with html2canvas but cannot improve it.

Here is my code :

document.getElementById('captureButton').addEventListener('click', function() {
    const mybook = document.getElementById('toto');

    const scale = 1;
    const segmentHeight = 800;

    const captureScreenshots = (canvas, totalHeight) => {
        const numSegments = Math.ceil(totalHeight / segmentHeight);

        for (let i = 0; i < numSegments; i++) {
            const segmentCanvas = document.createElement('canvas');
            const context = segmentCanvas.getContext('2d');

            const sourceY = i * segmentHeight;
            const destY = 0;

            segmentCanvas.width = canvas.width; // Keep the same width
            segmentCanvas.height = segmentHeight;

            context.drawImage(canvas, 0, sourceY, canvas.width, segmentHeight, 0, destY, canvas.width, segmentHeight);

            const dataURL = segmentCanvas.toDataURL('image/jpeg');

            const a = document.createElement('a');
            a.href = dataURL;
            a.download = `screenshot-${i}.jpeg`;
            a.click();
        }
    };

    html2canvas(mybook, {
        dpi: 600,
        scale: 5,
        useCORS: true,
        logging: true,
        letterRendering: true,
    }).then(function(canvas) {
        const totalHeight = canvas.height;

        if (totalHeight > segmentHeight) {
            captureScreenshots(canvas, totalHeight);
        } else {
            const dataURL = canvas.toDataURL('image/jpeg');

            const a = document.createElement('a');
            a.href = dataURL;
            a.download = 'screenshot.jpeg';
            a.click();
        }
    });
});

I don’t know if it’s even possible to get HD images with html2canvas. Maybe someone has an idea for an alternative solution ?

So far I tested html2canvas, I also tested domtoimage (but didn’t succeed to have it work).

2

Answers


  1. Chosen as BEST ANSWER

    I fail getting a better resolution. I did a test for a single image export script by scaling by 5 from the start and it works, the resolution is good. The problem is I am unable to apply this scaling to my script as I am not skilled at all, I'm a total newbie and write code by analogy as I never learn coding. If someone could help me add the scaling to my multiple images export I would really appreciate. I'm a hard worker, I spent many days on this script but I am sincerely limited.

    I'm happy to have found out that html2canvas can generate better resolution images but I am stuck right now to apply the fix.

    What I did for the single image is what I found throught many posts on the internet and it works :

        // Create a new canvas to capture the region
        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);

    So, the idea is to scale by 5 the region captured.

    Thanks for reading me.


  2. The quality of the generated JPEG images in your code depends on several factors, including the scale, the segment height, and the DPI setting. It’s important to keep in mind that the resolution and quality of the resulting images are also dependent on the content of the "toto" div and the rendering capabilities of the browser.

    Here are a few suggestions to potentially improve the quality of the exported JPEG images:

    1. Increase the Scale:
      You can increase the scale value in the html2canvas options to capture a higher resolution image. For example, you can set scale to 10 or higher for better quality.

    2. Adjust the DPI:
      Increasing the DPI to 600 is a good start, but you can try higher values, such as 1200 or more, if needed.

    3. Set letterRendering to false:
      The letterRendering option is meant to improve text rendering, but in some cases, it may lead to blurry text. You can try setting it to false to see if it improves the text quality.

    4. Use a Larger segmentHeight:
      The segmentHeight determines the height of each segment when capturing long content. You can increase it to ensure each segment is of a higher quality. However, this might lead to larger file sizes.

    5. Optimize the Content:
      Make sure the content inside the "toto" div has a high resolution to begin with. If the content is low resolution, increasing the scale may not yield significantly better results.

    6. Experiment with Different Formats:
      Instead of JPEG, you can try capturing the image in a different format, such as PNG, which might provide better quality at the cost of larger file sizes.

    Please keep in mind that generating very high-resolution images may result in large file sizes and slower rendering, so find a balance that works for your specific use case. Additionally, the quality of the output can also be influenced by the specific fonts, text rendering, and CSS properties used in your content.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search