An open API service indexing awesome lists of open source software.

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.

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