Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8" />
- <title>Address Delivery Sticker</title>
- <!-- <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/cdn/beer.min.css" rel="stylesheet">
- <script type="module" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/cdn/beer.min.js"></script>
- <script type="module"
- src="https://cdn.jsdelivr.net/npm/[email protected]/dist/cdn/material-dynamic-colors.min.js"></script> -->
- <!-- <style>
- /* For screen */
- canvas {
- border: 1px solid black;
- width: 250mm;
- height: 180mm;
- }
- /* For printing */
- @media print {
- @page {
- size: landscape;
- margin: 0;
- }
- canvas {
- width: 250mm;
- /* physical printed width */
- height: 180mm;
- /* printed height */
- display: block;
- margin: auto;
- }
- }
- </style> -->
- <style>
- canvas {
- width: 960;
- height: 540;
- border: 1px solid black;
- }
- </style>
- </head>
- <body>
- <form id="form_">
- TO: <input type="text" name="to"> <br>
- Store Code: <input type="text" name="store_code"> <br>
- Store Name: <input type="text" name="store_name"> <br>
- File Name: <select name="file_name">
- <option value="DMO">DMO</option>
- <option value="DMC">DMC</option>
- <option value="DDR">DDR</option>
- <option value="SCB_SRD">SCB/SRD</option>
- <option value="BY_HAND_QUALITY_ISSUE_DMQ">BY HAND - QUALITY ISSUE (DMQ)</option>
- <option value="DMW">DMW</option>
- <option value="DMN">DMN</option>
- <option value="DMP">DMP</option>
- <option value="RETURNABLE_SKUs">RETURNABLE SKUs</option>
- </select> <br>
- <!-- DMO, DMC, DDR, SCB/SRD, BY HAND - QUALITY ISSUE (DMQ), DMW, DMN, DMP, RETURNABLE SKUs -->
- DM File Date: <input type="date" name="dm_file_date"> <br>
- Total Cartons: <input type="number" name="total_cartons"> <br>
- Remarks: <textarea name="remarks"></textarea> <br>
- <input type="submit" value="Generate">
- <div id="printArea" style="display:none"></div>
- <div id="debug_xy"></div>
- </form>
- <canvas id="canvas_"></canvas>
- <script>
- const form_ = document.getElementById("form_")
- const canvas_ = document.getElementById("canvas_")
- const printArea_ = document.getElementById("printArea")
- const ctx = canvas_.getContext("2d");
- canvas_.width = 960 //(3508 / 1123 / 842)
- canvas_.height = 540 //(2480 / 794 / 595)
- //premake_delivery_sticker(ctx);
- ctx.font = "bold 14px Arial"
- ctx.fillText("Address Delivery Sticker", 13, 24);
- ctx.font = "bold 34px Arial"
- ctx.strokeRect(10, 10, 200, 20)
- ctx.fillText("TO", 80, 110);
- ctx.strokeRect(10, 50, 180, 100)
- ctx.strokeRect(10, 50, 400, 200)
- let images_ = new Image();
- images_.src = "bg.png";
- images_.onload = function() {
- ctx.drawImage(images_, 0, 0, canvas_.width, canvas_.height);
- }
- //listen when the input form is changed
- form_.addEventListener('input', (e) => {
- // console.log("Input changed")
- // console.log(e.target.name)
- // console.log(e.target.value)
- //redraw canvas
- ctx.clearRect(0, 0, canvas_.width, canvas_.height);
- ctx.drawImage(images_, 0, 0, canvas_.width, canvas_.height);
- ctx.font = "bold 14px Arial"
- //draw the input values on the canvas
- const to_ = form_.to.value;
- const store_code_ = form_.store_code.value;
- const store_name_ = form_.store_name.value;
- const file_name_ = form_.file_name.value;
- const dm_file_date_ = form_.dm_file_date.value;
- const total_cartons_ = form_.total_cartons.value;
- const remarks_ = form_.remarks.value;
- ctx.textAlign = "center";
- ctx.textBaseline = "middle";
- ctx.font = "bold 34px Arial"
- ctx.fillText(to_, 372, 75); //X: 372, Y: 74.671875
- ctx.fillText(`${store_code_}`, 372, 140);
- ctx.fillText(`${store_name_}`, 372, 195);
- ctx.fillText(`${file_name_}`, 372, 260);
- //convert dm_file_date_ to format 10-Nov-2025
- let date_obj = new Date(dm_file_date_);
- let options = { year: 'numeric', month: 'short', day: '2-digit' };
- let formatted_date = date_obj.toLocaleDateString('en-GB', options).replace(/ /g, '-');
- ctx.fillText(`${formatted_date}`, 372, 335);
- // ctx.fillText(`${dm_file_date_}`, 20, 220);
- // X: 844, Y: 169.671875
- ctx.fillText(`${total_cartons_}`, 844, 170);
- ctx.fillText(`${remarks_}`, 20, 280);
- })
- canvas_.addEventListener('mousedown', (e) => {
- const rect = canvas_.getBoundingClientRect();
- const x = e.clientX - rect.left;
- const y = e.clientY - rect.top;
- document.getElementById("debug_xy").innerText = `X: ${x}, Y: ${y}`;
- })
- //generate sticker on submit
- form_.addEventListener('submit', (e) => {
- e.preventDefault();
- // console.log("Submitted")
- // console.log(form_.to.value)
- //print canvas
- const dataURL = canvas_.toDataURL("image/png");
- printArea_.innerHTML = `<img src="${dataURL}">`;
- printArea_.style.display = "block";
- form_.style.display = "none";
- window.print();
- // Hide again after printing
- printArea_.style.display = "none";
- form_.style.display = "block";
- })
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment