HTML to PDF || Html2Pdf

You can use this code snippet to download HTML page content as a PDF.

This can use any of the languages and the frameworks that HTML supported.

NOTE: There is a link that is inside the <head> as a script that is needed to handle HTML convert to pdf. And also you can change the pdf setting in the opt attribute section.

<!DOCTYPE html>

<html>

<head>

<meta charset=”ISO-8859–1">

<script src=”https://raw.githack.com/eKoopmans/html2pdf/master/dist/html2pdf.bundle.js"></script>

<title>Insert title here</title>

</head>

<body>

<script>

function downloadPdf() {

const element = document.getElementById(“testPdf”);

var opt = {

margin: 1,

filename: ‘myfile.pdf’,

image: { type: ‘jpeg’, quality: 0.98 },

html2canvas: { scale: 2 },

jsPDF: { unit: ‘in’, format: ‘letter’, orientation: ‘portrait’ }

};

html2pdf().set(opt).from(element).save();

}

</script>

<button class=”btn btn-danger” onclick=”downloadPdf()”>Download</button>

<div id=”testPdf”>

<h1>Hi… this is the div to be download…!</h1>

</div>

</body>

</html>

--

--

Janaka Chinthana Dissanayake
0 Followers

I am an undergraduate student of SLIIT following BSc.(HonS ) in IT specialization in Software Engineering