Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active April 20, 2024 23:28
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 code-boxx/cb979bb95d3dacd5c7e854f680a99766 to your computer and use it in GitHub Desktop.
Save code-boxx/cb979bb95d3dacd5c7e854f680a99766 to your computer and use it in GitHub Desktop.
Javascript Convert HTML To PDF

JAVASCRIPT HTML TO PDF

https://code-boxx.com/convert-html-pdf-in-javascript/

IMAGE

music

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>Entire Page To PDF</title>
<!-- (A) LOAD HTML2PDF -->
<!-- https://cdnjs.com/libraries/html2pdf.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.10.1/html2pdf.bundle.min.js"></script>
</head>
<body>
<!-- (B) DUMMY CONTENTS -->
<h1>Hello World!</h1>
<p>This is an example.</p>
<!-- (C) ENTIRE PAGE TO PDF -->
<script>
function toPDF () {
html2pdf()
.from(document.body)
.save();
}
</script>
<button onclick="toPDF()">To PDF</button>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Section To PDF</title>
<!-- (A) LOAD HTML2PDF -->
<!-- https://cdnjs.com/libraries/html2pdf.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.10.1/html2pdf.bundle.min.js"></script>
</head>
<body>
<!-- (B) DUMMY CONTENTS -->
<h1>Hello World!</h1>
<div id="demo">
<div><img src="music.png"></div>
<p>Convert this section to PDF only.</p>
</div>
<p>This is an example.</p>
<!-- (C) SECTION TO PDF -->
<script>
function toPDF () {
html2pdf()
// (C1) SET OPTIONS
.set({
margin: 1,
filename: "demo.pdf",
image: { type: "jpeg", quality: 0.8 },
enableLinks : true,
jsPDF: { format: "A4", orientation: "landscape" }
})
// (C2) GET CONTENT FROM SECTION & SAVE
.from(document.getElementById("demo"))
.save();
}
</script>
<button onclick="toPDF()">To PDF</button>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>HTML String To PDF</title>
<!-- (A) LOAD HTML2PDF -->
<!-- https://cdnjs.com/libraries/html2pdf.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.10.1/html2pdf.bundle.min.js"></script>
</head>
<body>
<!-- (B) PDF FROM HTML STRING -->
<script>
function toPDF () {
html2pdf()
.from(`<h1>Hello World!</h1><p>This is an example.</p>`)
.save();
}
</script>
<!-- (C) GO! -->
<button onclick="toPDF()">To PDF</button>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Entire Page To PDF</title>
<!-- (A) LOAD HTML2PDF -->
<!-- https://cdnjs.com/libraries/html2pdf.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.10.1/html2pdf.bundle.min.js"></script>
</head>
<body>
<!-- (B) FETCH PAGE TO PDF -->
<script>
function toPDF () {
fetch("1-entire-page.html")
.then(res => res.text())
.then(html => {
html2pdf()
.from(html)
.save();
})
.catch(err => console.error(err));
}
</script>
<button onclick="toPDF()">Fetch Page To PDF</button>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment