A Next.js application that converts web pages to PDF using Browserless and Puppeteer.
- Convert any webpage to PDF
- Memory-efficient streaming of PDF generation
- Robust image loading handling
- Clean and simple user interface
- Input validation and error handling
- Node.js 18 or later
- A Browserless.io account and API token
-
Clone the repository:
git clone <repository-url> cd url-to-pdf
-
Install dependencies:
npm install
-
Create a
.env.local
file in the root directory and add your Browserless token:BROWSERLESS_TOKEN=your_token_here
-
Start the development server:
npm run dev
The application will be available at http://localhost:3000
.
- Open the application in your browser
- Enter the URL of the webpage you want to convert to PDF
- Click "Generate PDF"
- The PDF will automatically download to your computer
The application exposes a single API endpoint:
POST /api/pdf
Request body:
{
"url": "https://example.com"
}
Response:
- Success: PDF file stream
- Error: JSON with error message
The API will return appropriate error messages for:
- Invalid URLs
- Missing or invalid request body
- Failed PDF generation
- Missing Browserless token