I have managed to convert my HTML document into PDF using the jsPDF library by MrRio. the problem is my content is a form. The pdf file shows the field names but I am unable to get their input.

<div id="editor">
    <form id="content" class="text-center border border-light p-5" action="" method="POST">
        <!-- Full Name -->
        <div class="form-group row">
            <label class="col-sm-2 col-form-label">Full Name</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" id="inputFullName" placeholder="Full Name" required>
            </div>
        </div>
        <!-- Adress -->
        <div class="form-group row">
            <label class="col-sm-2 col-form-label">Adress</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" id="inputAdress" placeholder="Adress" required>
            </div>
        </div>
        <!-- E-mail -->
        <div class="form-group row">
            <label class="col-sm-2 col-form-label">Email</label>
            <div class="col-sm-10">
                <input type="email" class="form-control" id="inputEmail" placeholder="Email" required>
            </div>
        </div>
        <!-- Password -->
        <div class="form-group row">
            <label class="col-sm-2 col-form-label">Password</label>
            <div class="col-sm-10">
                <input type="email" class="form-control" id="inputPassword" placeholder="Password" required>
            </div>
        </div>
    </form>
</div>

Here is the javascript code:

$(function () {
    var specialElementHandlers = {
        '#editor': function (element,renderer) {
            return true;
        }
    };

    $('#cmd').click(function () {
        var doc = new jsPDF();
        doc.fromHTML(
            $('#content').html(), 15, 15, 
            { 'width': 170, 'elementHandlers': specialElementHandlers }, 
            function(){ doc.save('sample-file.pdf'); }
        );
});   

I need to get the form with the user input.

I have tried the HTML to canvas and then to pdf and then I tried this. Apparently, there are limitations to the implementation of this method as it doesn't support many elements or classes. If anyone could provide insights or useful help.

1 Answers

3
Bentaiba Miled Basma On Best Solutions

You can do that by querying the values introduced by the user either by using jquery or pure javascript.

This is a snippet of the solution:

<button id="cmd" class="btn btn-info my-4 btn-block" type="submit" onclick=" $(function () { 
   var doc = new jsPDF()
   doc.text('Full Name: \t\t\t'+document.querySelector(`#inputFullName`).value, 10, 10)
   doc.text('Adresse: \t\t\t'+document.querySelector(`#inputAdress`).value, 10, 20)
   //here do the same for the rest of your inputs
   doc.save('a4.pdf')
        });">Send</button>
<button class="btn btn-info my-4 btn-block" type="reset">Reset</button>