Skip to content

Instantly share code, notes, and snippets.

@Maes95
Last active March 15, 2017 11:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Maes95/9fcf84a725fcafba05ecd0857518a859 to your computer and use it in GitHub Desktop.
Save Maes95/9fcf84a725fcafba05ecd0857518a859 to your computer and use it in GitHub Desktop.
Simple export HTML element to PDF
function exportToPDF(title, id, css){
setTimeout(function () {
var printWindow = window.open("", "", "width=1000, height=800");
printWindow.document.write('<html><head><title>'
+title
+'</title>'
+css
+'</head><body onload="window.print()">'
+ document.getElementById(id).innerHTML
+ '</body></html>');
printWindow.document.close();
}, 1000);
}
var css = "<link href='https://rawgit.com/Semantic-Org/Semantic-UI/next/dist/semantic.css' rel='stylesheet'></link>";
exportToPDF("MyTitle", "myDiv", css);
<html>
<body>
<div id="myDiv">
<div class="ui button">Click me</div>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment