I have a div that displays user-edited content ready to be sent to the Windows Print Dialog for printing. The content contains images and text at a width of 7016px, which is the width of A1 at 300dpi. I have a button to print at A1 and a button to print at A4. The A4 button changes the width to 2480px to fit onto A4 paper. But when the dialog opens, the image is huge and takes up about 7 pages of A4 paper. In order for the image to fit I need to manually change the scale to 10.
How can I send the div to the print dialog at the correct scale to fit onto the paper? Is there a way to do this?
Thank you 🙂
Here is my print function:
How can I send the div to the print dialog at the correct scale to fit onto the paper? Is there a way to do this?
Thank you 🙂
Here is my print function:
Code:
function printContent(size) {
const printContainer = document.getElementById("allContent");
if (!printContainer) {
console.error("Print container not found!");
return;
}
// Store the original page content
const originalContent = document.body.innerHTML;
// Set the correct width for A1 or A4 printing
if (size === "A1") {
printContainer.style.width = "7016px"; // A1 width at 300 DPI
printContainer.style.height = "auto"; // Flexible for roll printing
} else if (size === "A4") {
printContainer.style.width = "2480px";
printContainer.style.height = "3508px";
}
// Replace the body content with only the allContent div for printing
document.body.innerHTML = printContainer.outerHTML;
// Trigger the print dialog
window.print();
// Restore the original content after printing
document.body.innerHTML = originalContent;
location.reload(); // Reload to restore event listeners and styles
}