$("#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 我该怎么做?