I am using php mysql in my html page. Already I have use many div in my page. I have create a print button, and it print my specific div but now I want to use a download button and it download my specific div data as a PDF
.
How can it possible?
I am using php mysql in my html page. Already I have use many div in my page. I have create a print button, and it print my specific div but now I want to use a download button and it download my specific div data as a PDF
.
How can it possible?
jsPDF would be a greate choice for handling this kind of functionality. It's relatively easy and quick to use. Here's a jsfiddle example:
https://jsfiddle.net/nfnneil/aajsr6vn/2/
I used doc.fromHTML($('#saveThis').html(), 10, 10, {})
and I think that deserves some explanation. Firstly, doc is a jsPDF object that we are using. Arguments goes as follows: HTML content for pdf
, padding-x in pdf
, padding-y in pdf
, and then, finally the settings {}
.
HTML
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/0.9.0rc1/jspdf.min.js"></script>
<div id="downloadThis">
Rich html <b>features</b> here.
</div>
<div id="editor"></div>
<button id="download">generate PDF</button>
JavaScript
$('#download').click(function () {
var doc = new jsPDF();
doc.fromHTML($('#downloadThis').html(), 10, 10, {
'elementHandlers': {}
});
doc.save('download.pdf');
});
<div id="content">
<h3>Hello, this is a H3 tag</h3>
<p>a pararaph</p>
</div>
<div id="editor"></div>
<button id="cmd">generate PDF</button>
<script>
var doc = new jsPDF();
var specialElementHandlers = {
'#editor': function (element, renderer) {
return true;
}
};
$('#cmd').click(function () {
doc.fromHTML($('#content').html(), 15, 15, {
'width': 170,
'elementHandlers': specialElementHandlers
});
doc.save('sample-file.pdf');
});
</script>
For downloading ....^