A lightweight Python service that retrieves image-based gallery pages and packages them into a downloadable PDF.
Built with Flask, Requests, Pillow, and fpdf2. Designed for serverless deployment on Vercel.
- Generate a PDF from a gallery ID in a single request.
- Preserve each page's aspect ratio.
- Return the PDF directly without temporary file hosting.
- Support optional upstream API authentication.
- Validate image sources and enforce download limits.
- Return structured JSON errors when an upstream request fails.
GET /api/nh_to_pdf?id=123456Returns a PDF attachment with the gallery ID and title in the filename.
GET /Returns a small JSON response indicating that the service is running.
The project uses Vercel's Python runtime.
- Import this repository into Vercel.
- Keep the project root as the deployment root.
- Deploy using the included
vercel.jsonconfiguration. - Add optional environment variables if your upstream API account requires authentication.
| Variable | Required | Description |
|---|---|---|
NHENTAI_API_KEY |
No | Optional API key for authenticated upstream requests. |
NHENTAI_USER_AGENT |
No | Custom User-Agent used for upstream API and image requests. |
Configure variables in Vercel → Project Settings → Environment Variables, then redeploy. Keep credentials out of source control.
To keep serverless execution predictable, each conversion is subject to these limits:
- Gallery IDs must be numeric.
- Maximum of 300 pages per gallery.
- Maximum of 20 MB per image.
- Maximum of 180 MB total source-image data.
- Maximum of 60 megapixels per image.
- Bounded network request timeouts.
The service rejects unsupported or corrupt images and reports upstream API or CDN failures as JSON responses.
Requires Python 3.10 or newer.
python -m pip install -r requirements.txt
python server.pyThe development server listens on port 3000 by default. Set the PORT environment variable to use another port.
Gallery metadata is retrieved through the upstream v2 API. Page images are fetched from the provider's image CDN and converted to PDF in memory with fpdf2.
The archive download endpoint offered by the upstream provider is not used because this service returns PDF output. The PDF is generated during the request and is not stored between invocations.
Availability depends on the upstream API and image CDN, including their rate limits and hosting-provider access policies.