Skip to content

Instantly share code, notes, and snippets.

@MdKutubuddinSardar
Created March 5, 2018 13:51
Show Gist options
  • Save MdKutubuddinSardar/16ad61c7be5161b9d72144454b012392 to your computer and use it in GitHub Desktop.
Save MdKutubuddinSardar/16ad61c7be5161b9d72144454b012392 to your computer and use it in GitHub Desktop.
Generate pdf from HTML foem in div using Javascript
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<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 = $("#dvContainer").html();
var printWindow = window.open('', '', 'height=400,width=800');
printWindow.document.write('<html><head><title>DIV Contents</title>');
printWindow.document.write('</head><body >');
printWindow.document.write(divContents);
printWindow.document.write('</body></html>');
printWindow.document.close();
printWindow.print();
});
</script>
</head>
<body>
<form id="form1">
<div id="dvContainer">
This content needs to be printed.
</div>
<input type="button" value="Print Div Contents" id="btnPrint" />
</form>
</body>
</html>
@MdKutubuddinSardar
Copy link
Author

@MdKutubuddinSardar
Copy link
Author

This is the simple solution. This works for me.You can use the javascript print concept and simple save this as pdf

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