How to Convert HTML File to PDF File Using JavaScript?

Hi friends, today I will tell you through experts php tutorial how you will convert html file to pdf file by javascript. So let’s try to understand step to step.

Now you will be told in 2 steps how you can convert html file to pdf. First you create html file. And then after that create the code of javascript. So let’s understand all the steps. How do we do it?

Overview

Step 1:- First of all create HTML File (html-to-pdf-convert.html).
Step 2:- Create JavaScript code into Same File.

Step 1:- First of all create HTML File (html-to-pdf-convert.html).

html-to-pdf-convert.html

<!DOCTYPE html>
<html>
<head>
<title>How to Convert HTML to PDF Using JavaScript With Demo?</title>
</head>
<body>
<h1>How to Convert HTML to PDF Using JavaScript With Demo?</h1>
<button id="clickdownload">Download in PDF File</button>
<div id="content">
<table id="example1" class="display table table-bordered table-striped row-
border order-column" cellspacing="0" width="auto">
<thead style="background-color: green;">
<tr>
<th>No</th>
<th>Name</th>
<th>Code</th>
<th>User Id</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Jyoti</td>
<td>0421</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>

Step 2:- Create JavaScript code into Same File.

 

<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.5/jspdf.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.js"></script>
<script>
function onClick() {
html2canvas(document.body, {
onrendered: function(canvas) {
var img = canvas.toDataURL("image/png");
var doc = new jsPDF();
doc.addImage(img, 'JPEG', 20, 20);
doc.save('file.pdf');
}
});
};
var element = document.getElementById("clickdownload");
element.addEventListener("click", onClick);
</script>

You have been told this code separately. But the name of the file is same. You are given below by adding the code of html and javascript to the same file.

html-to-pdf-convert.html

<!DOCTYPE html>
<html>
<head>
<title>How to Convert HTML to PDF Using JavaScript With Demo?</title>
</head>
<body>
<h1>How to Convert HTML to PDF Using JavaScript With Demo?</h1>
<button id="clickdownload">Download in PDF File</button>
<div id="content">
<table id="example1" class="display table table-bordered table-striped row-
border order-column" cellspacing="0" width="auto">
<thead style="background-color: green;">
<tr>
<th>No</th>
<th>Name</th>
<th>Code</th>
<th>User Id</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Jyoti</td>
<td>0421</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.5/jspdf.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.js"></script>
<script>
function onClick() {
html2canvas(document.body, {
onrendered: function(canvas) {
var img = canvas.toDataURL("image/png");
var doc = new jsPDF();
doc.addImage(img, 'JPEG', 20, 20);
doc.save('file.pdf');
}
});
};
var element = document.getElementById("clickdownload");
element.addEventListener("click", onClick);
</script>
</body>
</html>

Cialis (Tadalafil) är den främsta konkurrenten till Viagra (Sildenafil) på marknaden för erektil dysfunktion. köpa Cialis i Sverige föredras av många på grund av sin längre varaktighet och anses vara det mest kostnadseffektiva varumärkesbaserade ED-läkemedlet som finns tillgängligt i Sverige. Cialis finns i två varianter: Cialis och Cialis Daily, och fyra olika doseringar: 2,5 mg, 5 mg, 10 mg och 20 mg, erbjuder Cialis också en rad olika alternativ för att passa patientens behov.