Skip to content

Instantly share code, notes, and snippets.

@hd9
Last active November 1, 2018 06:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hd9/b3f30966c0edaf472023cd71c48f7565 to your computer and use it in GitHub Desktop.
Save hd9/b3f30966c0edaf472023cd71c48f7565 to your computer and use it in GitHub Desktop.
A simple js-pdf exporter
<html>
<head>
<script src="https://code.jquery.com/jquery-1.12.3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/0.9.0rc1/jspdf.min.js"></script>
<script>
$(document).ready(function(){
$('#cmd').click(function () {
doc.fromHTML($('#content').html(), 15, 15, {
'width': 170,
'elementHandlers': specialElementHandlers
});
doc.save('sample-file.pdf');
});
});
var doc = new jsPDF();
var specialElementHandlers = {
'#editor': function (element, renderer) {
return true;
}
};
</script>
</head>
<body>
<div id="content">
<h1>Lorem Ipsum</h1>
<p>Minus soluta non animi laboriosam voluptas pariatur. Sed eos temporibus culpa dolores iure nulla. Et ut nihil consectetur libero molestiae aut. Reiciendis et impedit officiis veniam quisquam rerum.</p>
<p>Aperiam ipsam qui ipsam ut quaerat ut reiciendis quis. Ea rerum ducimus rerum nam fugit modi. Qui molestiae nesciunt distinctio aliquid nam. Est culpa tenetur tenetur eos ut nisi quia qui. Recusandae debitis sed voluptatem. Et quia quisquam adipisci numquam dolores est sit ipsum.</p>
<p>Quaerat sint harum provident nihil totam sunt. Quia dolorem aut iste doloremque et suscipit animi. Distinctio unde doloremque aut et sequi.</p>
</div>
<p>More info can be found at <a href="https://blog.hildenco.com/2018/02/a-simple-js-to-pdf-exporter.html">https://blog.hildenco.com/2018/02/a-simple-js-to-pdf-exporter.html</a></p>
<div id="editor"></div>
<button id="cmd">Generate PDF</button>
</body>
</html>
@hd9
Copy link
Author

hd9 commented Nov 1, 2018

How to generate pdf files from JavaScript pages and Html?

For more information, visit this post:

https://blog.hildenco.com/2018/02/a-simple-js-to-pdf-exporter.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment