如何设置 jpeg 图片下载的 dpi?

问题描述 投票:0回答:1
$("#download-pdf-template").on("click", function(e) {  
    overallwidthcover = parseFloat(Number(overallwidthcover).toFixed(3));
    overallcoverhegiht = parseFloat(Number(overallcoverhegiht).toFixed(3));

    var covertype = $(this).data('id');
    var pdfWidthInInches = overallwidthcover ? overallwidthcover : 12.80;
    var pdfHeightInInches = overallcoverhegiht ? overallcoverhegiht : 9.25;


    var fileName;
    var htmlContent;

    if (Number(covertype) == 1) {
        htmlContent = '#paperpBackDownload';
        fileName = 'paper_back_cover_template.jpeg';
    } else {
        htmlContent = '#caseboundbackDownload';
        fileName = 'casebound_cover_template.jpeg';
    }

    var elementHTML = document.querySelector(htmlContent);
     // Desired DPI and dimensions
     var dpi = 300;
    
     var widthInPixels = pdfWidthInInches * 72;
     var heightInPixels = pdfHeightInInches * 72;

    html2canvas(elementHTML, {
        useCORS: true,
        // scale: 2  
        scale: dpi / 72
    }).then(function(canvas) {
        var adjustedCanvas = document.createElement('canvas');
        adjustedCanvas.width = widthInPixels;
        adjustedCanvas.height = heightInPixels;
        var ctx = adjustedCanvas.getContext('2d');
        ctx.drawImage(canvas, 0, 0, widthInPixels, heightInPixels);

        var imgData = adjustedCanvas.toDataURL('image/jpeg', 1.0); // Convert to JPEG
       
        // Create a link element and simulate a click to download the image
        var link = document.createElement('a');
        link.href = imgData;
        link.download =  fileName;
        link.click();
    }).catch(function(error) {
        console.error('Error capturing image:', error);
    });
});

我有一个下载按钮,单击它会将 html 内容下载为 jpeg。一切看起来都很好,但是当我在 pdf 查看器中打开此图像时,其 dpi 为 72,我希望将其 dpi 设置为 300 我该怎么做?

jquery image jpeg html2canvas dpi
1个回答
0
投票

这个问题似乎是一个重复

顺便说一句,让我们谷歌一下一下DPI代表什么:DPI代表每英寸点数

我担心,由于这只是图像采集的扫描设置/属性,因此在下载过程中使用高度/宽度值不会改变图像的“分辨率”。

© www.soinside.com 2019 - 2024. All rights reserved.