Pernah kepikiran gak sih, gimana caranya bikin sistem yang bisa langsung generate PDF (kayak invoice, laporan, atau slip gaji) terus otomatis dikirim ke email user? 🤔
Nah, di NestJS ini gampang banget kok, tinggal mix antara library buat bikin PDF sama mailer buat kirim email.
🔧 Tools Wajib
Sebelum eksekusi, kenalan dulu sama tools yang bakal dipakai:
📝 Buat PDF
- pdfkit → bikin PDF langsung dari NodeJS. Simple & cepat.
- puppeteer → render HTML ke PDF (cocok kalo lo udah punya template HTML/CSS kece).
- pdfmake → lebih ke style declarative, gampang buat layout.
📩 Kirim Email
- @nestjs-modules/mailer → gampang banget dipakai di NestJS (dibungkus dari nodemailer).
- nodemailer → bisa dipakai juga, tapi lebih raw.
🚀 Step by Step
Langsung aja ke contoh paling simpel: generate PDF pakai pdfkit lalu kirim lewat email.
1️⃣ Service Buat Generate PDF
// pdf.service.ts
import { Injectable } from '@nestjs/common';
import * as PDFDocument from 'pdfkit';
import { join } from 'path';
import { writeFileSync } from 'fs';
@Injectable()
export class PdfService {
async generatePdf(filename: string): Promise<string> {
const doc = new PDFDocument();
const filePath = join(__dirname, `../../storage/${filename}`);
doc.pipe(writeFileSync(filePath, { flag: 'w' }) as any);
doc.fontSize(16).text('Hello from NestJS PDF!', { align: 'center' });
doc.text('PDF ini dibuat otomatis di backend NestJS 🚀', { align: 'left' });
doc.end();
return filePath;
}
}
📌 Intinya: service ini bikin file PDF dan nyimpen sementara di folder storage.
2️⃣ Service Buat Kirim Email
// mail.service.ts
import { Injectable } from '@nestjs/common';
import { MailerService } from '@nestjs-modules/mailer';
@Injectable()
export class AppMailService {
constructor(private readonly mailerService: MailerService) {}
async sendMailWithPdf(to: string, filePath: string) {
await this.mailerService.sendMail({
to,
subject: 'PDF Kamu Udah Jadi 🎉',
text: 'Cek lampiran ya!',
attachments: [
{
filename: 'report.pdf',
path: filePath,
},
],
});
}
}
📌 Jadi nanti email masuk ke inbox user, ada PDF langsung nempel di attachment.
3️⃣ Controller Buat Ngetes
// app.controller.ts
import { Controller, Get } from '@nestjs/common';
import { PdfService } from './pdf.service';
import { AppMailService } from './mail.service';
@Controller()
export class AppController {
constructor(
private readonly pdfService: PdfService,
private readonly mailService: AppMailService,
) {}
@Get('send-pdf')
async sendPdf() {
const filePath = await this.pdfService.generatePdf('report.pdf');
await this.mailService.sendMailWithPdf('user@example.com', filePath);
return { message: 'Email dengan PDF udah dikirim! 🚀' };
}
}
📌 Endpoint GET /send-pdf ini bisa langsung lo hit, terus PDF bakal dikirim ke email target.
💡 Tips Santai
- Kalo pengen PDF yang desainnya rapi kayak invoice → coba puppeteer, tinggal render HTML pake CSS.
- Di serverless (kayak vercel/functions) → lebih aman langsung stream PDF ke buffer, jangan tulis file di disk.
- Jangan lupa config SMTP di mailer (bisa Gmail, SendGrid, Mailgun, etc.).
🎯 Kesimpulan
Dengan NestJS, bikin fitur auto-generate PDF + auto-email attachment itu gampang banget.
Lo bisa pake buat:
- Invoice otomatis 💵
- Slip gaji 💼
- Laporan transaksi 📊
Semua tinggal hit API → PDF jadi → email masuk ke inbox user.
Simple, clean, dan mantap buat production 🚀.

