edaross

Automatically bold the first half of all words entered

Feb 26th, 2023 (edited)
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.78 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <title>Bold Half Words (made by ChatGPT)</title>
  5.     <style>
  6.         body {
  7.             background-color: lightyellow;
  8.             font-family: sans-serif;
  9.         }
  10.     </style>
  11. </head>
  12. <body>
  13.     <h1>Bold Half Words</h1>
  14.     <label for="input">Input:</label>
  15.     <br>
  16.     <textarea id="input" rows="10" cols="50" oninput="boldHalf()"></textarea>
  17.     <br>
  18.     <button onclick="downloadPdf()">Download PDF</button>
  19.     <br>
  20.     <label for="output">Output:</label>
  21.     <br>
  22.     <div id="output"></div>
  23.     <script src="https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.9.2/html2pdf.bundle.min.js"></script>
  24.     <script>
  25.         function boldHalf() {
  26.             // Get input text and split into words
  27.             var inputText = document.getElementById("input").value;
  28.             var words = inputText.split(/\b/);
  29.  
  30.             // Create output text with first half of each word in bold
  31.             var outputText = "";
  32.             for (var i = 0; i < words.length; i++) {
  33.                 var word = words[i];
  34.                 if (/\w/.test(word)) {
  35.                     var halfLength = Math.floor(word.length / 2);
  36.                     outputText += "<b>" + word.slice(0, halfLength) + "</b>" + word.slice(halfLength);
  37.                 } else {
  38.                     outputText += word;
  39.                 }
  40.             }
  41.  
  42.             // Display output text
  43.             document.getElementById("output").innerHTML = outputText;
  44.         }
  45.  
  46.         function downloadPdf() {
  47.             // Get output text
  48.             var outputText = document.getElementById("output").innerHTML;
  49.  
  50.             // Convert output text to PDF
  51.             var element = document.createElement("div");
  52.             element.innerHTML = outputText;
  53.             html2pdf(element, {
  54.                 margin:       [10, 10, 10, 10],
  55.                 filename:     "bold_half_words.pdf",
  56.                 image:        { type: 'jpeg', quality: 0.98 },
  57.                 html2canvas:  { dpi: 192, letterRendering: true },
  58.                 jsPDF:        { unit: 'mm', format: 'a4', orientation: 'portrait' }
  59.             });
  60.         }
  61.     </script>
  62. </body>
  63. </html>
  64.  
Advertisement
Add Comment
Please, Sign In to add comment