title: How to Print the html output into Pdf using javascript
body: <p><a href="https://i.stack.imgur.com/2al6U.png" rel="nofollow noreferrer">Here is my output image</a></p>
  <p>Here is my code</p>
  <blockquote>
  
      
          
          
          <br />
                 
      
      
          let input = document.querySelector('input');
          var textarea = document.getElementsByClassName('area')[0];
          var txtreplace = document.getElementsByClassName('demo')[0];
           window.addEventListener('load',
              function () {
                  input.addEventListener('change', () => {
                      let files = input.files;
                     if (files.length == 0) return;
                     const file = files[0];
                      let reader = new FileReader();
                     const fruits = [];      
                    reader.onload = (e) => {
                          const file = e.target.result;
                          const lines = file.split(/\r\n|\n/);
                          //console.log(lines.length);
                          textarea.value = lines.join('\n');
                          lines.forEach(function (arrayItem) {
                              //console.log(arrayItem.replace("\u001bE", "<b>"));
                              var arr1 = arrayItem.replace("\u001bE", "");
                              var arr2 = arr1.replace("\u001bF", "</b>");
                             
                              fruits.push(arr2);
                   
                          });
                          console.log(fruits.length);
                          if (parseInt(fruits.length) > 0) {
                              console.log(fruits);
                              txtreplace.innerHTML = fruits;
                          }
                    
                      };
  <pre><code>                reader.onerror = (e) =&gt; alert(e.target.error.name);
  
                  reader.readAsText(file);
  
              });
          }, false);
        
      function printDiv() {
          var divContents = document.getElementsByClassName(&quot;demo&quot;).innerHTML;
          var a = window.open('', '', 'height=500, width=500');
          a.document.write('&lt;html&gt;');
          a.document.write('&lt;body &gt; &lt;h1&gt;Print PDF &lt;br&gt;');
          a.document.write(divContents);
          a.document.write('&lt;/body&gt;&lt;/html&gt;');
          a.document.close();
          a.print();
      }
  
  &lt;/script&gt; &lt;/body&gt;
  </code></pre>
  </blockquote>