1

$('#generate').click(function() {
  var pdf = new jsPDF('p', 'pt', 'a4');
  pdf.addHTML($('.pg-section').get(0), function() {
    pdf.save('Test.pdf');
  });
});
.pg-section {
  background: white;
}
.pg-section h3 {
  padding: 5px;
  background: #808080;
  text-align: center;
  font-size: 14px;
  color: #FFF;
  font-weight: bold;
  margin-bottom: 10px;
}

.pg-tbl {
  margin: 15px 0;
  border-collapse: collapse;
  border: 2px solid blue;
  width: 100%;
}

.pg-tbl th {
  background: #ccc;
  text-align: center;
}

.pg-tbl th,
.pg-tbl td {
  border: 2px solid blue;
  padding: 5px 4px;
  font-size: 10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<button id="generate">Generate PDF</button>
<div class="pg-section">
  <h3 class="h3">User Information</h3>
  <table id="tbl1" class="pg-tbl">
    <thead>
      <tr>
        <th>User ID</th>
        <th>First</th>
        <th>Last</th>
        <th>Age</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td id="user_id">5672</td>
        <td id="first">John</td>
        <td id="last">Kean</td>
        <td id="age">29</td>
      </tr>
    </tbody>
  </table>

  <h3 class="h3">Building Information</h3>
  <table id="tbl9" class="pg-tbl">
    <thead>
      <tr>
        <th rowspan="2">Total</th>
        <th colspan="2">Range</th>
      </tr>
      <tr>
        <th>High</th>
        <th>Low</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td id="total">45</td>
        <td id="low">13</td>
        <td id="high">5</td>
      </tr>
    </tbody>
  </table>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.0/jspdf.debug.js"></script>
<script language="javascript" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.min.js"></script>

In example above I use jspdf.debug.js and html2canvas.min.js to export div container to PDF file. After file is generated I noticed that pdf looks blurry. I'm wondering how I can fix that? Is there a way to make the pdf look like the original html?

espresso_coffee
  • 5,980
  • 11
  • 83
  • 193
  • Possible duplicate of https://stackoverflow.com/questions/25946275/exporting-pdf-with-jspdf-not-rendering-css – Neil VanLandingham Dec 21 '18 at 15:54
  • Possible duplicate of [Exporting PDF with jspdf not rendering CSS](https://stackoverflow.com/questions/25946275/exporting-pdf-with-jspdf-not-rendering-css) – Neil VanLandingham Dec 21 '18 at 15:54
  • @Weft I was able to get this to work with html2canvas. Here is example: `var pdf = new jsPDF('p', 'pt', 'a4'); pdf.addHTML($('.pg-section').get(0), function () { pdf.save('Test.pdf'); });` The one problem I have noticed that pdf file looks blurry. Is there a way to clear the quality of the pdf? – espresso_coffee Dec 21 '18 at 17:03
  • Maybe try adjusting the scale or width and height options : https://html2canvas.hertzen.com/configuration – Neil VanLandingham Dec 21 '18 at 18:26

2 Answers2

1

put the dpi: 200 or 400 you will get clear image

MKJ
  • 120
  • 8
1

I had the same problem using addHtml. Turns out, addHtml is deprecated according to jsPDF's documentation. I ended up just following the link they provided and using pdf.html(). Here is the code that I used to send the converted pdf as an email attachment. You can just use pdf.save() in your case. P.S. I used html2canvas 1.0.0-alpha.12

function emailHtml() {
    let pdf = new jsPDF('p', 'pt', 'a4');
    pdf.html(document.body, {
        callback: function (pdf) {
            let obj = {};
            obj.pdfContent = pdf.output('datauristring');
            var jsonData = JSON.stringify(obj);
            $.ajax({
                url: '/api/jspdf/html2pdf',
                type: 'POST',
                contentType: 'application/json',
                data: jsonData
            });
        }
    });
}
Weihui Guo
  • 3,669
  • 5
  • 34
  • 56