https://github.com/usman-rizwan/nextjs-html2pdf
Next.js application for converting HTML to PDF using Puppeteer. Provides an internal API for PDF generation with a built-in test interface.
https://github.com/usman-rizwan/nextjs-html2pdf
nextjs15 puppeteer react19 taiwlindcss
Last synced: about 2 months ago
JSON representation
Next.js application for converting HTML to PDF using Puppeteer. Provides an internal API for PDF generation with a built-in test interface.
- Host: GitHub
- URL: https://github.com/usman-rizwan/nextjs-html2pdf
- Owner: usman-rizwan
- Created: 2025-07-31T08:40:24.000Z (2 months ago)
- Default Branch: master
- Last Pushed: 2025-07-31T11:54:57.000Z (2 months ago)
- Last Synced: 2025-07-31T15:31:18.418Z (2 months ago)
- Topics: nextjs15, puppeteer, react19, taiwlindcss
- Language: JavaScript
- Homepage: https://nextjs-html2pdf.vercel.app
- Size: 66.4 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Next.js HTML to PDF API
A Next.js application that provides an internal PDF generation API using Puppeteer, designed to replace external PDF generation services.
---
## ๐ Features
- **PDF Generation API**: Convert HTML to PDF using Puppeteer
- **RESTful Endpoint**: `POST /api/pdf` with JSON body containing HTML
- **Binary Response**: Returns PDF as `application/pdf` binary stream
- **Error Handling**: Comprehensive error handling and validation
- **Test Interface**: Built-in test page for API demonstration---
## ๐ฆ Installation
Install dependencies:
```bash
npm install
```---
## ๐ก API Usage
### Endpoint
```http
POST /api/pdf
```### Request Format
```json
{
"html": "Hello World
"
}
```### Response
- **Success**: PDF binary stream with `Content-Type: application/pdf`
- **Failure**: JSON error object with status code and message---
### ๐งช Example Usage (Node.js)
```javascript
const response = await fetch("https://next-html2pdf.vercel.app/api/pdf", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ html: htmlContent }),
});if (response.headers.get("Content-Type") !== "application/pdf") {
const data = await response.json();
console.error("PDF generation error:", data);
throw new Error("Could not generate PDF.");
}const pdfArrayBuffer = await response.arrayBuffer();
return pdfArrayBuffer;
```---
## ๐ Development
Start the development server:
```bash
npm run dev
```---
## ๐งฑ Project Structure
```
src/app/
โโโ api/
โ โโโ pdf/
โ โโโ route.js # PDF generation API endpoint
โโโ html-to-pdf/
โ โโโ page.js # Test interface for PDF generation
โโโ page.js # Main page with link to test
```---
## โ๏ธ API Implementation Highlights
1. **Input Validation**: Ensures HTML is provided and is a valid string
2. **Puppeteer Configuration**: Headless and secure, with optimized flags
3. **PDF Options**: A4 page, custom margins, background graphics enabled
4. **Error Handling**: Graceful failure with proper browser cleanup
5. **Content-Type Validation**: Ensures returned file is a valid PDF---
## โ Error Codes
| Code | Description |
|------|---------------------------------------------|
| 400 | Invalid input (missing or malformed HTML) |
| 405 | Method not allowed (only POST supported) |
| 500 | Internal server error (Puppeteer failure) |---
## ๐งช Testing Interface
A simple test page is included at `/html-to-pdf` where you can:
- Enter custom HTML
- Generate and preview PDFs
- Download the output
- View error messages if generation fails