npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@maheshpaul/serverless-pdf-generator

v0.1.2

Published

A PDF generator for serverless environments

Readme

Serverless PDF Generator

serverless-pdf-generator is a lightweight package that simplifies the process of generating PDFs from web pages in a serverless environment like Vercel. It utilizes Puppeteer and Chromium to render pages and generate high-quality PDFs.

Features

  • 📄 Convert any URL to a PDF easily.
  • 🚀 Optimized for serverless environments (Vercel, AWS Lambda, etc.).
  • ⚡ Supports Puppeteer and Chromium-min for minimal dependencies.
  • 🎯 Customizable PDF output (format, margins, background printing, etc.).

Installation

npm install @maheshpaul/serverless-pdf-generator

Usage

Next.js API Route Example

Create an API route at src/app/api/pdf/route.ts:

import { handlePDFRequest } from '@maheshpaul/serverless-pdf-generator';
import { NextRequest } from 'next/server';

export async function GET(request: NextRequest) {
    return handlePDFRequest(request, async () => {
        const url = new URL(request.url).searchParams.get('url');
        if (!url) throw new Error('Missing URL');
        return { url, filename: 'download.pdf' };
    }, {
        format: 'A4',
        printBackground: true,
        development: process.env.NODE_ENV === 'development'
    });
}

Next.js Client Component Example

Create a download button in a React client component:

'use client';

export default function DownloadPDF() {
  const handleDownload = async () => {
    try {
      const response = await fetch(`/api/pdf?url=${encodeURIComponent('https://google.com')}`);
      if (!response.ok) throw new Error('PDF generation failed');
      
      const blob = await response.blob();
      const url = URL.createObjectURL(blob);
      const a = document.createElement('a');
      a.href = url;
      a.download = 'google.pdf';
      document.body.appendChild(a);
      a.click();
      document.body.removeChild(a);
      URL.revokeObjectURL(url);
    } catch (error) {
      console.error('Download failed:', error);
      alert('Failed to download PDF');
    }
  };
  
  return (
    <button onClick={handleDownload} className="px-4 py-2 bg-blue-500 text-white rounded-md hover:bg-blue-600">
      Download PDF
    </button>
  );
}

React Server Component Example

If using a server-side approach in a React environment:

import React, { useState } from 'react';

const DownloadPDF = () => {
  const [loading, setLoading] = useState(false);

  const handleDownload = async () => {
    setLoading(true);
    try {
      const response = await fetch('/api/pdf?url=https://example.com');
      if (!response.ok) throw new Error('PDF generation failed');
      
      const blob = await response.blob();
      const url = URL.createObjectURL(blob);
      const a = document.createElement('a');
      a.href = url;
      a.download = 'example.pdf';
      document.body.appendChild(a);
      a.click();
      document.body.removeChild(a);
      URL.revokeObjectURL(url);
    } catch (error) {
      console.error(error);
      alert('Error downloading PDF');
    }
    setLoading(false);
  };

  return (
    <button onClick={handleDownload} disabled={loading} className="px-4 py-2 bg-green-500 text-white rounded-md hover:bg-green-600">
      {loading ? 'Generating...' : 'Download PDF'}
    </button>
  );
};

export default DownloadPDF;

API

generatePDF(options: PDFGeneratorOptions)

Generates a PDF from a given URL.

Parameters

interface PDFGeneratorOptions {
  url: string;
  format?: 'A4' | 'Letter' | 'Legal';
  margin?: {
    top?: string;
    right?: string;
    bottom?: string;
    left?: string;
  };
  printBackground?: boolean;
  development?: boolean;
}

Example

import { generatePDF } from '@maheshpaul/serverless-pdf-generator';

async function downloadPDF() {
  const pdfBuffer = await generatePDF({ url: 'https://example.com' });
  require('fs').writeFileSync('output.pdf', pdfBuffer);
}

handlePDFRequest(request: NextRequest, getData: () => Promise<{ url: string; filename?: string }>, options?: Omit<PDFGeneratorOptions, 'url'>)

Handles a Next.js API request for generating a PDF.

Example

export async function GET(request: NextRequest) {
    return handlePDFRequest(request, async () => {
        return { url: 'https://example.com', filename: 'example.pdf' };
    }, { format: 'A4' });
}

Deployment

This package is optimized for serverless environments like Vercel. If using locally, ensure you have set process.env.NODE_ENV === 'development' to automatically switch between local and serverless execution.

License

MIT License

Author

Created by Mahesh Paul. Contributions are welcome!