JavaScript合并加载PDF的Canvas时如何避免画质损失与变暗?
问题:Canvas合并后文档画质损失、背景变暗
我尝试合并两个Canvas,每个Canvas均包含相同的PDF文档,仅签名栏不同,目标是合并两者以整合签名栏至同一文档。但合并后,结果文档出现画质损失,且背景变暗。
合并用代码
async function combinePages(numPages) { console.log("Combining ..."+numPages); let canvas1, canvas2; // Declare canvas1 and canvas2 variables here // Iterate through each page for (let i = 1; i <= numPages; i++) { let element = document.getElementById(`canvasContainer3_${i}`); if (!element) { // Create a new canvas container element let container = document.createElement("div"); container.id = `canvasContainer3_${i}`; // Append the container to the document body document.body.appendChild(container); } // Get the canvas elements for the current page canvas1 = document.getElementById('canvas1_'+i); canvas2 = document.getElementById('canvas2_'+i); // Create a new canvas element for the combined page let combinedCanvas = ""; combinedCanvas = document.createElement("canvas"); combinedCanvas.setAttribute('id', 'canvas3_'+i); let canvas1Width = canvas1.width; let canvas1Height = canvas1.height; let canvas2Width = canvas2.width; let canvas2Height = canvas2.height; // Set the size of the combined canvas combinedCanvas.width = Math.max(canvas1Width, canvas2Width); combinedCanvas.height = Math.max(canvas1Height, canvas2Height); let context = combinedCanvas.getContext("2d"); context.drawImage(canvas1, 0, 0); // Draw the contents of the second canvas onto the combined canvas context.globalCompositeOperation = 'multiply'; context.drawImage(canvas2, 0, 0); // Add the combined canvas to the page let container = document.getElementById(`canvasContainer3_${i}`); container.appendChild(combinedCanvas); document.getElementById("wrapper").appendChild(container); // Hide canvas1 and canvas2 //canvas1.style.display = "none"; // canvas2.style.display = "none"; } return 'finish combination'; }
合并相关信息
- 原始PDF示例:

- 合并流程:PDF 1与PDF 2合并,结果再与PDF 3合并,最终结果与PDF 4合并
- 合并后结果:

合并后的文档白色背景变为灰色,整体颜色偏暗。
内容的提问来源于stack exchange,提问作者srecce
相关产品推荐
相关产品推荐

