Open In App

Generate PDF File Using jsPDF Library

Last Updated : 07 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The PDF Generation Project in JavaScript aims to create a seamless tool for generating PDFs dynamically. It allows users to easily convert data into well-formatted PDF documents, enhancing productivity and user experience with quick and efficient document creation.

What Will You Learn?

We’ll build an application that allows users to

  • Input content in a text field or upload data.
  • Dynamically generate a PDF document based on the entered content.
  • Customize the PDF format, including text, images, and layout.
  • Download or preview the generated PDF.
  • Provide a clean and user-friendly interface for seamless document creation.

The application will ensure a smooth, interactive experience with instant feedback and easy PDF generation.

Project Preview

Screenshot-2025-01-15-122743
How to generate PDF file using jsPDF library

Generate PDF File - HTML and CSS Code

HTML
<html>
<head>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #f4f4f9;
        }
        .container {
            text-align: center;
            padding: 20px;
            background-color: #fff;
            border-radius: 8px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        }
        button {
            background-color: #4CAF50;
            color: white;
            border: none;
            padding: 10px 20px;
            font-size: 16px;
            cursor: pointer;
            border-radius: 5px;
            transition: background-color 0.3s;
        }
        button:hover {
            background-color: #45a049;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Generate PDF with jsPDF</h1>
        <button onclick="generatePDF()">Generate PDF</button>
    </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js"></script>
</body>
</html>

In this code

  • The page uses CSS Flexbox to center a .container div, giving it a white background, rounded corners, and a shadow effect for a clean look.
  • A button labeled "Generate PDF" is styled with a green background and hover effect, set to trigger the generatePDF() function when clicked.
  • The jsPDF library is included via a CDN, enabling JavaScript-based PDF generation.

Generate PDF File - Javascript code

JavaScript
function generatePDF() {
    const { jsPDF } = window.jspdf;
    const doc = new jsPDF();       

    doc.text("Hello, this is a PDF generated with jsPDF!", 100, 100);  

    doc.save("sample.pdf");
}
  • The function extracts jsPDF from window.jspdf and creates a new PDF document using new jsPDF().
  • Adds text "Hello, this is a PDF generated with jsPDF!" at coordinates (100, 100) in the document.
  • Saves and downloads the generated PDF as "sample.pdf" using doc.save().

Complete code

HTML
<html>
<head>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #f4f4f9;
        }
        .container {
            text-align: center;
            padding: 20px;
            background-color: #fff;
            border-radius: 8px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        }
        button {
            background-color: #4CAF50;
            color: white;
            border: none;
            padding: 10px 20px;
            font-size: 16px;
            cursor: pointer;
            border-radius: 5px;
            transition: background-color 0.3s;
        }
        button:hover {
            background-color: #45a049;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Generate PDF with jsPDF</h1>
        <button onclick="generatePDF()">Generate PDF</button>
    </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js"></script>
    <script>
        function generatePDF() {
            const { jsPDF } = window.jspdf;  
            const doc = new jsPDF();        
            doc.text("Hello, this is a PDF generated with jsPDF!", 100, 100); 
            doc.save("sample.pdf");  
        }
    </script>
</body>
</html>

Next Article

Similar Reads