user10186430
user10186430

Reputation:

How to export a div to PDF?

I am trying to export a div to PDF format.

When I click the button, a page opens to save the page in PDF or print.

This page should display the contents of the div, but what appears is [object HTMLDivElement]:

Image with Problem

HTML:

<div id="PDF" class="tab-pane fade in active"> 
    <div class="table-responsive panel">
       <table class="table">
          <tbody>
              <tr>
                  <td class="text-success"><i class="fa fa-user"></i> Name</td>
                  <td>@Model.Name</td>
              </tr>
              <tr>
                  <td class="text-success"><i class="fa fa-home"></i> Adress</td>
                  <td>@Model.Adress</td>
              </tr>
           </tbody>
        </table>
    </div>
</div>
<button type="button" class="btn btn-primary btn-circle btn-xl" id="btnPrint" data-toggle="tooltip" title="Export"> </button>

JavaScript:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
    $("#btnPrint").live("click", function () {
        var divContents = $("#PDF").html();
        var printWindow = window.open('', '', 'height=400,width=800');
        printWindow.document.write('<html><head><title>PDF</title>');
        printWindow.document.write('</head><body >');
        printWindow.document.write(PDF);
        printWindow.document.write('</body></html>');
        printWindow.document.close();
        printWindow.print();
    });
</script>

How to fix this?

Upvotes: 1

Views: 2861

Answers (2)

pdub
pdub

Reputation: 1

var divContents = $("#PDF").html();

...

printWindow.document.write(PDF);

I believe you're saving the content you want to print, but not using it:

printWindow.document.write(divContents);

Upvotes: 0

Kapil gopinath
Kapil gopinath

Reputation: 1053

Update your code by replacing with the below lines

var printWindow = window.open('', '_self', 'height=400,width=800');
printWindow.document.write(divContents);

Upvotes: 2

Related Questions