Skip to content

krishtewatia/PDF_Generator_Using_Java

Repository files navigation

PDF Generator Project

πŸ“‹ Project Overview

Project Title: PDF Generator Tool
Description: A Java-based application that converts text files and images to PDF format, demonstrating core Object-Oriented Programming concepts including inheritance, polymorphism, abstraction, and encapsulation.

Key Features:

  • Convert text files to properly formatted PDF documents
  • Convert images to PDF with embedded image information
  • Interactive console-based user interface
  • Proper PDF file generation that works with standard PDF viewers
  • Educational demonstration of OOP principles

πŸ—οΈ Project Structure

Java Project/
β”œβ”€β”€ src/
β”‚   └── com/
β”‚       └── pdfgenerator/
β”‚           β”œβ”€β”€ PDFGenerator.java          # Abstract base class
β”‚           β”œβ”€β”€ TextToPDFGenerator.java    # Text to PDF converter
β”‚           β”œβ”€β”€ ImageToPDFGenerator.java   # Image to PDF converter
β”‚           └── PDFGeneratorApp.java       # Main application class
β”œβ”€β”€ sample-files/
β”‚   β”œβ”€β”€ sample.txt                         # Sample text for testing
β”‚   └── README_for_images.txt             # Instructions for image files
β”œβ”€β”€ output/                               # Generated PDF files appear here
β”œβ”€β”€ lib/                                  # (Reserved for future libraries)
β”œβ”€β”€ README.md                            # This file
β”œβ”€β”€ VIVA_PREPARATION.md                  # Detailed viva preparation guide
└── COMPILE_AND_RUN.md                   # Step-by-step execution guide

🎯 Object-Oriented Programming Concepts Demonstrated

1. Inheritance

  • PDFGenerator serves as the abstract parent class
  • TextToPDFGenerator and ImageToPDFGenerator extend the base class
  • Child classes inherit common methods like validateInputFile() and status display methods
  • Demonstrates "IS-A" relationship (TextToPDFGenerator IS-A PDFGenerator)

2. Polymorphism

  • Same method generatePDF() behaves differently in each subclass
  • PDFGenerator reference can point to any concrete implementation
  • Runtime method resolution based on actual object type
  • Demonstrates method overriding and dynamic binding

3. Abstraction

  • PDFGenerator contains abstract method generatePDF()
  • Hides implementation details from user
  • Provides clean interface for PDF generation
  • Each subclass must implement the abstract method

4. Encapsulation

  • Private methods for internal PDF creation logic
  • Protected fields accessible only to subclasses
  • Public methods provide controlled access to functionality
  • Data hiding and access control principles

πŸš€ How to Compile and Run

Prerequisites

  • Java Development Kit (JDK) 8 or higher
  • Windows Command Prompt or PowerShell
  • Text editor (VS Code, Notepad++, etc.) for viewing files

Step 1: Navigate to Project Directory

cd "C:\Users\HP\Downloads\Java Project"

Step 2: Compile All Java Files

javac -cp . src\com\pdfgenerator\*.java -d .

What this does:

  • javac: Java compiler command
  • -cp .: Sets classpath to current directory
  • src\com\pdfgenerator\*.java: Compile all Java files in package
  • -d .: Output compiled classes to current directory

Step 3: Run the Application

java com.pdfgenerator.PDFGeneratorApp

Step 4: Use the Application

You'll see a menu like this:

========================================
    Welcome to PDF Generator Tool
========================================

╔══════════════════════════════════════╗
β•‘            MAIN MENU                 β•‘
╠══════════════════════════════════════╣
β•‘ 1. Convert Text File to PDF         β•‘
β•‘ 2. Convert Image File to PDF        β•‘
β•‘ 3. Demo with Sample Files           β•‘
β•‘ 4. View Output Directory            β•‘
β•‘ 5. Exit                             β•‘
β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•
Enter your choice (1-5):

πŸ“ Usage Examples

Option 1: Convert Text File to PDF

  1. Choose option 1
  2. Enter path: sample-files\sample.txt
  3. PDF will be generated in output/ folder

Option 2: Convert Image to PDF

  1. Add an image file (JPG/PNG) to sample-files/ folder
  2. Choose option 2
  3. Enter the image file path
  4. PDF with image information will be created

Option 3: Run Demo (Recommended for Testing)

  1. Choose option 3
  2. Automatically processes sample files
  3. Shows both text and image conversion

Option 4: View Generated Files

  1. Choose option 4
  2. See list of all generated PDF files
  3. File sizes and names displayed

πŸ”§ Technical Implementation Details

PDF File Structure Created

Our application generates proper PDF files with:

  1. PDF Header: %PDF-1.4 (version identifier)
  2. Binary Marker: Ensures compatibility with PDF readers
  3. Object Catalog: Root object defining document structure
  4. Pages Object: Container for all pages in document
  5. Page Object: Individual page with dimensions and content
  6. Font Object: Text rendering font (Helvetica)
  7. Content Stream: Actual text/image content data
  8. Cross-Reference Table: File navigation information
  9. Trailer: Document metadata and structure
  10. End Marker: %%EOF (end of file)

Text Processing Features

  • Line Wrapping: Automatically wraps long lines to fit page width
  • Character Escaping: Handles special PDF characters safely
  • Font Formatting: Uses standard Helvetica font at 12pt
  • Margin Control: 50-point margins on all sides
  • Multi-line Support: Processes line breaks correctly

Image Processing Features

  • Format Support: JPG, PNG, GIF, BMP image files
  • Scaling Calculation: Automatically scales images to fit A4 page
  • Aspect Ratio: Maintains original image proportions
  • Positioning: Centers images on PDF page
  • Metadata: Includes original dimensions and file information

πŸ“š File I/O Operations Demonstrated

Input Operations

  • BufferedReader: Efficient text file reading
  • FileReader: Character stream reading
  • ImageIO: Image file loading and processing
  • File Validation: Existence and readability checks

Output Operations

  • FileOutputStream: Binary PDF file writing
  • BufferedWriter: Efficient text output
  • OutputStreamWriter: Character encoding control
  • Directory Management: Automatic folder creation

πŸŽ“ Educational Value

Perfect for Academic Demonstration

  • Real-World Application: Solves actual problem (file conversion)
  • Industry Relevance: PDF generation is common in enterprise applications
  • Scalable Design: Easy to extend with additional features
  • Clean Code: Well-commented and organized for learning

Concepts Suitable for Examination

  • Object-oriented design principles
  • File handling and I/O operations
  • Exception handling and error management
  • User interface design (console-based)
  • Software architecture and modularity

πŸ” Troubleshooting

Compilation Issues

Problem: javac not recognized
Solution: Ensure JDK is installed and added to PATH

Problem: Class not found errors
Solution: Make sure you're in the project root directory

Runtime Issues

Problem: File not found when converting
Solution: Use absolute paths or ensure files exist in specified location

Problem: Permission denied creating output
Solution: Check write permissions on output directory

PDF Viewing Issues

Problem: Generated PDF won't open
Solution: Try different PDF viewer (Adobe Reader, browser, etc.)


πŸš€ Future Enhancements

Possible Extensions

  1. GUI Interface: Swing or JavaFX-based user interface
  2. iText Integration: Professional PDF library for advanced features
  3. Multi-page Support: Handle documents longer than one page
  4. Image Embedding: True image data embedding in PDF
  5. Font Options: Multiple font types and sizes
  6. Batch Processing: Convert multiple files simultaneously
  7. PDF Merging: Combine multiple PDFs into one
  8. Encryption: Password protection for generated PDFs

Production Considerations

  • Use established libraries (iText, Apache PDFBox) for commercial use
  • Add comprehensive error handling and logging
  • Implement unit tests for all components
  • Add configuration files for customization
  • Consider memory optimization for large files

πŸ“ž Support and Documentation

For questions about this project:

  1. Review the VIVA_PREPARATION.md file for detailed examination guidance
  2. Check COMPILE_AND_RUN.md for step-by-step execution instructions
  3. Examine source code comments for implementation details
  4. Test with sample files to understand functionality

πŸ“„ License and Usage

This project is created for educational purposes and academic demonstration. Feel free to use, modify, and extend for learning objectives.

Created: October 2025
Language: Java 8+
Platform: Cross-platform (Windows, Mac, Linux)
Dependencies: Standard Java libraries only


Happy coding and good luck with your viva examination! πŸŽ‰

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages