Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.
Articles

Auto-generating a PDF without relying on external APIs

Auto-generating a PDF without relying on external APIs can be achieved using various libraries in popular programming languages. Below are examples in Python and JavaScript, two commonly used languages for such tasks.

 


1. Python Approach: Using
FPDF

The FPDF library allows you to create PDFs easily, with no external dependencies.

Installation:

pip install fpdf

Code Example:

from fpdf import FPDF

class PDF(FPDF):
 def header(self):
 self.set_font('Arial', 'B', 12)
 self.cell(0, 10, 'Auto-Generated PDF Example', 0, 1, 'C')

 def footer(self):
 self.set_y(-15)
 self.set_font('Arial', 'I', 8)
 self.cell(0, 10, f'Page {self.page_no()}', 0, 0, 'C')

# Create a PDF object
pdf = PDF()
pdf.add_page()
pdf.set_font('Arial', size=12)

# Add content
pdf.cell(0, 10, 'Hello, this is an auto-generated PDF.', ln=True)
pdf.cell(0, 10, 'You can add text, images, and more!', ln=True)

# Save the PDF to a file
pdf.output('example.pdf')

print("PDF created successfully!")

2. JavaScript Approach: Using
jsPDF

The jsPDF library works in the browser or Node.js to generate PDFs.

Installation (for Node.js):

npm install jspdf

Code Example (Browser or Node.js):

const { jsPDF } = require('jspdf'); // In Node.js, use require()

// Create a new PDF document
const doc = new jsPDF();

// Add content
doc.setFont("helvetica", "bold");
doc.text("Auto-Generated PDF Example", 20, 20);
doc.setFont("helvetica", "normal");
doc.text("Hello, this is an auto-generated PDF.", 20, 30);
doc.text("You can add text, images, and more!", 20, 40);

// Save the PDF
doc.save("example.pdf");
console.log("PDF created successfully!");

Browser Version:

Include the

jsPDF
library via a CDN in your HTML:



 const { jsPDF } = window.jspdf;

 const doc = new jsPDF();
 doc.text("Hello, this is an auto-generated PDF.", 10, 10);
 doc.save("example.pdf");


3. Common Features You Can Add

Text Styling:

  • Font Style and Size: Modify font using
    setFont()
    and
    setFontSize()
    .

Adding Images:

  • Python (FPDF):
    pdf.image('path/to/image.jpg', x=10, y=10, w=100)
    
  • JavaScript (jsPDF):
    const imgData = 'data:image/jpeg;base64,...'; // Base64 image data
    doc.addImage(imgData, 'JPEG', 10, 10, 100, 100);
    

Table Generation:

Both libraries support generating tables or grids for data display. For example, you can manually position cells using loops.


Advantages of Local PDF Generation

  • No reliance on external services or internet.
  • Full control over content and formatting.
  • Secure as no data is sent to third-party servers.

 

caa December 03 2024 7 reads 0 comments Print

0 comments

Leave a Comment

Please Login to Post a Comment.
  • No Comments have been Posted.

Sign In
Not a member yet? Click here to register.
Forgot Password?
Users Online Now
Guests Online 3
Members Online 0

Total Members: 11
Newest Member: Jhilam