Are you looking for an easy way to generate PDF files using JavaScript? In this tutorial, weβll build a simple PDF Generator using HTML, CSS, and JavaScript with the jsPDF
library. This tool allows users to enter text and download it as a PDF file with just one click. π
π₯ Why Use a PDF Generator?
PDF generation is useful for various applications like:
β
Creating reports, invoices, or receipts π
β
Exporting notes or custom documents π
β
Making downloadable certificates π
By the end of this tutorial, youβll have a fully functional PDF generator that lets users convert text into PDFs effortlessly!
π οΈ Technologies Used
- HTML: For structuring the UI
- CSS: For styling the design
- JavaScript: For handling the logic
- jsPDF: A JavaScript library to generate PDF files
π Step-by-Step Guide
1οΈβ£ HTML β Structure of the PDF Generator
First, we create a simple interface with a text area and a button to download the PDF.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PDF Generator</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js"></script>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<h2>PDF Generator</h2>
<textarea id="textInput" placeholder="Enter text to convert into PDF..."></textarea>
<button onclick="generatePDF()">Download PDF</button>
</div>
<script src="script.js"></script>
</body>
</html>
2οΈβ£ CSS β Styling the User Interface
Now, letβs add some CSS to make our tool visually appealing.
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
text-align: center;
margin: 0;
padding: 20px;
}
.container {
width: 80%;
margin: auto;
background: white;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
textarea {
width: 100%;
height: 150px;
padding: 10px;
border-radius: 5px;
border: 1px solid #ddd;
font-family: Arial, sans-serif;
}
button {
padding: 10px 15px;
border: none;
background: #007bff;
color: white;
border-radius: 5px;
cursor: pointer;
margin-top: 10px;
transition: 0.3s;
}
button:hover {
background: #0056b3;
}
3οΈβ£ JavaScript β Generating and Downloading the PDF
Now, we use JavaScript and the jsPDF
library to generate the PDF.
function generatePDF() {
const { jsPDF } = window.jspdf;
let doc = new jsPDF();
let text = document.getElementById("textInput").value;
doc.text(text, 10, 10);
doc.save("download.pdf");
}
π― How It Works
β
Users enter text in the textarea
β
Clicking the Download PDF button generates a PDF
β
The jsPDF library converts the text into a PDF file
β
The PDF file is automatically downloaded
π₯ Video Tutorial
π Watch the full tutorial here:
π Code Repository
π Get the full source code on GitHub:
π GitHub Repository
π Next Steps
- Try adding custom fonts and images to the PDF
- Improve styling with a better UI
- Implement a dark mode feature
π Support & Subscribe
If you found this tutorial helpful, Like, Share, and Subscribe to Madras Academy for more web development tutorials!
π Stay updated by turning on notifications! π
#HTML #CSS #JavaScript #PDFGenerator #jsPDF #WebDevelopment #FrontendDevelopment #Coding #TechTutorial #InteractiveTools #PDFCreation ππ