Javascript PDFMake.js Example to Convert Dynamic Raw HTML Template to PDF Document in Browser Using Bootstrap 4 Full Project For Beginners

 

 

 

 

index.html

 

 

<!doctype html>
<html lang='en'>
<head>
  <meta charset='utf-8'>
  <title>PDFMake Convert Raw HTML to PDF Document</title>
  <!-- pdfmake files: -->
  <script src='https://cdn.jsdelivr.net/npm/pdfmake@latest/build/pdfmake.min.js'></script>
  <script src='https://cdn.jsdelivr.net/npm/pdfmake@latest/build/vfs_fonts.min.js'></script>
  <!-- html-to-pdfmake file: -->
  <script src="https://cdn.jsdelivr.net/npm/html-to-pdfmake/browser.js"></script>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/css/bootstrap.min.css" rel="stylesheet">

</head>
<body>
  <div class="container">
    <h1 class="text-center">
      Raw HTML to PDF Document
    </h1>
    <form id="form">
      <div class="form-group">
        <label for="html">Enter Raw HTML:</label>
        <textarea class="form-control" id="html" cols="30" rows="10"></textarea>
      </div>
      <button class="btn btn-danger btn-block">Convert to PDF</button>
    </form>
  </div>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

  <script>
    $("#form").submit(function(e){
      e.preventDefault()

      var text = $("#html").val()
      
      var val = htmlToPdfmake(text); 

      var dd = {content:val};

      pdfMake.createPdf(dd).download();

    })
    
  </script>
</body>
</html>

Leave a Reply