{"id":25480120,"url":"https://github.com/maheshpaulj/serverless-pdf-generator","last_synced_at":"2026-04-13T03:07:10.115Z","repository":{"id":277806585,"uuid":"933547503","full_name":"maheshpaulj/serverless-pdf-generator","owner":"maheshpaulj","description":"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.","archived":false,"fork":false,"pushed_at":"2025-02-16T08:21:52.000Z","size":24,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-16T09:21:37.250Z","etag":null,"topics":["nextjs","node-module","node-package","nodemodules","npm","pdf-generation","react"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/@maheshpaul/serverless-pdf-generator","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/maheshpaulj.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-02-16T08:18:38.000Z","updated_at":"2025-02-16T08:21:55.000Z","dependencies_parsed_at":"2025-02-16T09:21:42.486Z","dependency_job_id":"d0b6ae62-7636-4132-9673-65276656d554","html_url":"https://github.com/maheshpaulj/serverless-pdf-generator","commit_stats":null,"previous_names":["maheshpaulj/serverless-pdf-generator"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maheshpaulj%2Fserverless-pdf-generator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maheshpaulj%2Fserverless-pdf-generator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maheshpaulj%2Fserverless-pdf-generator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maheshpaulj%2Fserverless-pdf-generator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/maheshpaulj","download_url":"https://codeload.github.com/maheshpaulj/serverless-pdf-generator/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239495904,"owners_count":19648398,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["nextjs","node-module","node-package","nodemodules","npm","pdf-generation","react"],"created_at":"2025-02-18T15:27:31.192Z","updated_at":"2026-04-13T03:07:10.097Z","avatar_url":"https://github.com/maheshpaulj.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Serverless PDF Generator\n\n`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.\n\n## Features\n\n- 📄 Convert any URL to a PDF easily.\n- 🚀 Optimized for serverless environments (Vercel, AWS Lambda, etc).\n- ⚡ Supports Puppeteer and Chromium-min for minimal dependencies.\n- 🎯 Customizable PDF output (format, margins, background printing, etc.).\n\n## Installation\n\n```sh\nnpm install @maheshpaul/serverless-pdf-generator\n```\n\n## Usage\n\n### Next.js API Route Example\n\nCreate an API route at `src/app/api/pdf/route.ts`:\n\n```ts\nimport { handlePDFRequest } from '@maheshpaul/serverless-pdf-generator';\nimport { NextRequest } from 'next/server';\n\nexport async function GET(request: NextRequest) {\n    return handlePDFRequest(request, async () =\u003e {\n        const url = new URL(request.url).searchParams.get('url');\n        if (!url) throw new Error('Missing URL');\n        return { url, filename: 'download.pdf' };\n    }, {\n        format: 'A4',\n        printBackground: true,\n        development: process.env.NODE_ENV === 'development'\n    });\n}\n```\n\n### Next.js Client Component Example\n\nCreate a download button in a React client component:\n\n```tsx\n'use client';\n\nexport default function DownloadPDF() {\n  const handleDownload = async () =\u003e {\n    try {\n      const response = await fetch(`/api/pdf?url=${encodeURIComponent('https://google.com')}`);\n      if (!response.ok) throw new Error('PDF generation failed');\n      \n      const blob = await response.blob();\n      const url = URL.createObjectURL(blob);\n      const a = document.createElement('a');\n      a.href = url;\n      a.download = 'google.pdf';\n      document.body.appendChild(a);\n      a.click();\n      document.body.removeChild(a);\n      URL.revokeObjectURL(url);\n    } catch (error) {\n      console.error('Download failed:', error);\n      alert('Failed to download PDF');\n    }\n  };\n  \n  return (\n    \u003cbutton onClick={handleDownload} className=\"px-4 py-2 bg-blue-500 text-white rounded-md hover:bg-blue-600\"\u003e\n      Download PDF\n    \u003c/button\u003e\n  );\n}\n```\n\n### React Server Component Example\n\nIf using a server-side approach in a React environment:\n\n```tsx\nimport React, { useState } from 'react';\n\nconst DownloadPDF = () =\u003e {\n  const [loading, setLoading] = useState(false);\n\n  const handleDownload = async () =\u003e {\n    setLoading(true);\n    try {\n      const response = await fetch('/api/pdf?url=https://example.com');\n      if (!response.ok) throw new Error('PDF generation failed');\n      \n      const blob = await response.blob();\n      const url = URL.createObjectURL(blob);\n      const a = document.createElement('a');\n      a.href = url;\n      a.download = 'example.pdf';\n      document.body.appendChild(a);\n      a.click();\n      document.body.removeChild(a);\n      URL.revokeObjectURL(url);\n    } catch (error) {\n      console.error(error);\n      alert('Error downloading PDF');\n    }\n    setLoading(false);\n  };\n\n  return (\n    \u003cbutton onClick={handleDownload} disabled={loading} className=\"px-4 py-2 bg-green-500 text-white rounded-md hover:bg-green-600\"\u003e\n      {loading ? 'Generating...' : 'Download PDF'}\n    \u003c/button\u003e\n  );\n};\n\nexport default DownloadPDF;\n```\n\n## API\n\n### `generatePDF(options: PDFGeneratorOptions)`\n\nGenerates a PDF from a given URL.\n\n#### Parameters\n\n```ts\ninterface PDFGeneratorOptions {\n  url: string;\n  format?: 'A4' | 'Letter' | 'Legal';\n  margin?: {\n    top?: string;\n    right?: string;\n    bottom?: string;\n    left?: string;\n  };\n  printBackground?: boolean;\n  development?: boolean;\n}\n```\n\n#### Example\n\n```ts\nimport { generatePDF } from '@maheshpaul/serverless-pdf-generator';\n\nasync function downloadPDF() {\n  const pdfBuffer = await generatePDF({ url: 'https://example.com' });\n  require('fs').writeFileSync('output.pdf', pdfBuffer);\n}\n```\n\n### `handlePDFRequest(request: NextRequest, getData: () =\u003e Promise\u003c{ url: string; filename?: string }\u003e, options?: Omit\u003cPDFGeneratorOptions, 'url'\u003e)`\n\nHandles a Next.js API request for generating a PDF.\n\n#### Example\n\n```ts\nexport async function GET(request: NextRequest) {\n    return handlePDFRequest(request, async () =\u003e {\n        return { url: 'https://example.com', filename: 'example.pdf' };\n    }, { format: 'A4' });\n}\n```\n\n## Deployment\n\nThis 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.\n\n\n## License\n\nMIT License\n\n## Author\n\nCreated by [Mahesh Paul](https://github.com/maheshpaulj). Contributions are welcome!","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaheshpaulj%2Fserverless-pdf-generator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmaheshpaulj%2Fserverless-pdf-generator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaheshpaulj%2Fserverless-pdf-generator/lists"}