Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active July 1, 2023 02:05
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 code-boxx/f863743b60f5774dab15825dd08612a5 to your computer and use it in GitHub Desktop.
Save code-boxx/f863743b60f5774dab15825dd08612a5 to your computer and use it in GitHub Desktop.
Javascript Print Page or Section

JAVASCRIPT PRINT PAGE OR SECTION

https://code-boxx.com/print-page-javascript/

ASSETS

demo

  • Gist does not allow PDF files, had to get creative. Save this as foo.pdf foo

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<!DOCTYPE html>
<html>
<head>
<title>Javascript Print</title>
<link rel="stylesheet" href="x-dummy.css">
</head>
<body>
<h1>Dummy Page</h1>
<img src="demo.png">
<p>Hello world.</p>
<input type="button" value="Print Entire Page" onclick="window.print()">
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Javascript Print Part</title>
<link rel="stylesheet" href="x-dummy.css">
</head>
<body>
<!-- (A) THE HTML -->
<!-- (A1) SECTION TO BE PRINTED -->
<div id="demoB">
<h1>Dummy Page</h1>
<img src="http://localhost/demo.png">
<p>Hello world.</p>
</div>
<!-- (A2) PRINT BUTTON -->
<input type="button" value="Print Above Section" onclick="printpart()">
<p>Rest of the page will not be printed.</p>
<!-- (B) THE JAVASCRIPT -->
<script>
function printpart () {
var printwin = window.open("");
printwin.document.body.innerHTML = document.getElementById("demoB").innerHTML;
printwin.stop();
printwin.print();
printwin.close();
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>PrintJS Lib</title>
<link rel="stylesheet" href="x-dummy.css">
<!-- (A) LOAD PRINTJS LIBRARY -->
<!-- DOCUMENTATION: https://printjs.crabbly.com/ -->
<script src="https://printjs-4de6.kxcdn.com/print.min.js"></script>
<link rel="stylesheet" href="https://printjs-4de6.kxcdn.com/print.min.css">
</head>
<body>
<!-- (B) SECTIONS FOR TEST PRINT -->
<div id="demoC">
<h1>Dummy Page</h1>
<img src="demo.png">
<p>Hello world.</p>
</div>
<div>Another section.</div>
<!-- (C) PRINT BUTTONS -->
<!-- (C1) PRINT "DEMOC" ONLY -->
<input type="button" value="Print section" onclick="printJS('demoC', 'html')">
<!-- (C2) PRINT IMAGE -->
<input type="button" value="Print image" onclick="printJS('demo.png', 'image')">
<!-- (C3) PRINT PDF FILE ON SERVER -->
<input type="button" value="Print PDF on server" onclick="printJS('foo.pdf', 'pdf')">
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>CSS Print</title>
<link rel="stylesheet" href="x-dummy.css">
<style>
@media only print {
body { visibility: hidden; }
#demoD { visibility: visible; }
}
</style>
</head>
<body>
<p>This will not be printed.</p>
<div id="demoD">
<h1>Dummy Page</h1>
<img src="demo.png">
<p>Hello world.</p>
</div>
<div>This will also not be printed.</div>
<div>Print this page and see for yourself.</div>
</body>
</html>
* {
font-family: Arial, Helvetica, sans-serif;
box-sizing: border-box;
}
#demoB, #demoC, #demoD { background: #ffe8e8; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment