A coloring-page PNG can look fine in an image viewer and still print badly: the drawing is cropped, browser controls appear above it, or the result spills onto a second page.
This is a small web-development problem with a useful boundary. The image already exists. The application only needs to load it, preserve its proportions, and place it inside a physical page.
One possible input is a PNG downloaded from iColoring AI's text-to-coloring-page tool. A hand-drawn scan or any other suitable local image works too. The implementation below does not connect to that service or depend on its generation process.
The finished tool is one HTML file. It runs without a build step, backend, account, or image upload.
Define the paper geometry first
A4 portrait is 210 × 297 mm. With a 12 mm margin on every side, the printable rectangle is:
width = 210 - 12 - 12 = 186 mm
height = 297 - 12 - 12 = 273 mm
The screen preview represents the full sheet, including padding. In print mode, @page provides the margins and the sheet becomes the 186 × 273 mm content rectangle. Applying both sets of margins in print mode would shrink the artwork twice.
The image fills this rectangle using object-fit: contain. Its box can have the page's proportions while the image itself keeps its original aspect ratio.
The complete file
Save this as print-sheet.html and open it in a current browser:
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Local coloring-page print sheet</title>
<style>
* { box-sizing: border-box; }
body { margin: 0; background: #eee; font: 16px system-ui; }
.controls { max-width: 55rem; margin: auto; padding: 1rem; }
label, input, button { display: block; margin-bottom: .75rem; }
button { padding: .6rem 1rem; font: inherit; }
.preview { overflow: auto; padding: 0 1rem 1rem; }
.sheet {
width: 210mm; height: 297mm; padding: 12mm;
margin: auto; background: white;
display: grid; place-items: center;
}
#art { width: 100%; height: 100%; object-fit: contain; min-height: 0; }
#art[hidden] { display: none; }
@page { size: A4 portrait; margin: 12mm; }
@media print {
body { background: white; }
.controls { display: none; }
.preview { padding: 0; overflow: visible; }
.sheet {
width: 186mm; height: 273mm;
margin: 0; padding: 0; break-inside: avoid;
}
}
</style>
<div class="controls">
<h1>Coloring-page print sheet</h1>
<label for="file">Choose a PNG, JPEG, or WebP image (up to 10 MiB)</label>
<input id="file" type="file" accept="image/png,image/jpeg,image/webp">
<p id="status" role="status">Choose an image to preview it locally.</p>
<button id="print" type="button" disabled>Print / Save as PDF</button>
</div>
<main class="preview" aria-label="A4 print preview">
<div class="sheet">
<img id="art" alt="Selected coloring page" hidden>
</div>
</main>
<script>
const input = document.querySelector('#file');
const art = document.querySelector('#art');
const status = document.querySelector('#status');
const printButton = document.querySelector('#print');
let selection = 0;
input.addEventListener('change', async () => {
const current = ++selection;
printButton.disabled = true;
art.hidden = true;
art.removeAttribute('src');
const file = input.files[0];
if (!file) {
status.textContent = 'Choose an image to preview it locally.';
return;
}
const allowed = ['image/png', 'image/jpeg', 'image/webp'];
if (!allowed.includes(file.type) || file.size > 10 * 1024 * 1024) {
status.textContent = 'Choose a PNG, JPEG, or WebP up to 10 MiB.';
return;
}
status.textContent = 'Loading image...';
const url = URL.createObjectURL(file);
const candidate = new Image();
candidate.src = url;
try {
await candidate.decode();
if (current !== selection) return;
art.src = url;
await art.decode();
if (current !== selection) return;
art.hidden = false;
printButton.disabled = false;
status.textContent = `${candidate.naturalWidth} x ${candidate.naturalHeight} px. Ready to print.`;
} catch {
if (current === selection) {
art.hidden = true;
status.textContent = 'This file could not be decoded as an image.';
}
} finally {
URL.revokeObjectURL(url);
}
});
printButton.addEventListener('click', () => window.print());
</script>
</html>
Choose a local PNG, JPEG, or WebP. When the dimensions appear, select Print / Save as PDF. In the print dialog, choose A4 portrait, use 100% scale, and turn off headers and footers. Check the preview before printing: dialog settings can override CSS preferences.
This is a screenshot of the accompanying local demo. The flower is a synthetic test fixture, not a result generated by the linked service.
Why wait for decoding?
Selecting a file does not prove that it contains a readable image. A file named drawing.png can contain arbitrary bytes.
The input's accept attribute helps with the file picker, and the JavaScript MIME check narrows the allowed types. Neither replaces decoding. candidate.decode() gives the code an explicit success or failure point before enabling printing.
There are two image elements involved: the temporary candidate and the visible preview. Waiting for the preview's decode as well means the button is enabled only after that element is ready.
The object URL is released in finally, after decoding or failure. The selection counter also matters: if someone chooses a second file while the first is decoding, the earlier operation must not restore stale artwork or enable the button for the wrong selection.
Keep controls out of the document
The controls and the paper are separate elements. The print stylesheet hides .controls and removes the scrolling and padding used for the screen preview.
That separation avoids having to hide labels, status text, and buttons individually. Adding another screen-only control later will not accidentally put it on the printed sheet.
The preview intentionally keeps its physical dimensions and scrolls on small screens. A responsive thumbnail would be a reasonable enhancement, but it should not change the print geometry.
What was checked
The accompanying implementation was run in headless Chromium through Playwright 1.62.1 with a synthetic 1000 × 1400 PNG. The check confirmed that:
- Loading the image enables printing and displays its dimensions.
- A corrupt file with a PNG MIME type is rejected after decoding fails.
- An unsupported MIME type leaves the print button disabled.
- The demo makes no HTTP or HTTPS requests during the tested workflow.
- Printing produces one A4-sized PDF page, with controls absent.
These are checks of the local utility. They are not a benchmark of the image-generation service or a test of every browser and printer.
Paper size does not create image detail
CSS can reserve physical space, but it cannot restore missing source pixels. For example, a 1000-pixel-wide image displayed across 186 mm has roughly 137 pixels per printed inch. Whether that looks acceptable depends on the artwork, line thickness, and printer.
Likewise, contain preserves empty space already present inside an image. It fits the file's rectangle; it does not identify or crop the drawing automatically.
If a sheet looks soft or unexpectedly small, inspect the source image before changing the page margins. For an image that already comes as a correctly sized PDF, this extra layout step may not be needed at all.

Top comments (0)