A Node.js/Express API service to generate printable prescription PDFs from JSON data using EJS templates and Puppeteer for rendering.
- Accepts prescription data as a JSON payload via a POST request.
- Uses EJS templating (
src/templates/gp-prescription.ejs
) for flexible HTML layout. - Leverages Puppeteer to convert the rendered HTML into a PDF document.
- Returns the generated PDF directly in the API response.
- Configurable port via environment variable.
- Node.js (v16 or later recommended)
- npm
- Clone the repository:
git clone git@github.com:Aaleen110/prescription-pdf-generator.git cd prescription-pdf-generator
- Install dependencies:
npm install
node server.js
By default, the server runs on port 3000. You can specify a different port:
PORT=8080 node server.js
The server will log: Server is running on http://localhost:<PORT>
- URL:
/api/printPrescription
- Method:
POST
- Headers:
Content-Type: application/json
- Body (JSON): Send the prescription data object. See example below.
- Success Response:
- Code: 200
- Content-Type:
application/pdf
- Content-Disposition:
attachment; filename=prescription.pdf
- The response body contains the raw PDF data.
- Error Response:
- Code: 500
- Content:
{ "error": "Failed to generate PDF" }
(or similar error message)
curl -X POST http://localhost:3000/api/printPrescription \
-H "Content-Type: application/json" \
-o prescription.pdf \
-d '{
"patient": {
"first_name": "Test",
"last_name": "Patient",
"gender": "F",
"phone_number": "9876543210",
"age": { "years": 35 },
"weight": "70kg",
"height": "170cm"
},
"medicines": [
{
"name": "Sample Drug 1",
"dosage": "1 Tablet Twice Daily",
"advisedFrequency": "Twice Daily",
"advisedWhenToTake": "After Food",
"duration": "5 Day(s)",
"instructions": "Take with water"
},
{
"name": "Sample Drug 2",
"dosage": "10ml Once Daily",
"advisedFrequency": "Once Daily",
"advisedWhenToTake": "Before Bed",
"duration": "10 Day(s)"
}
],
"chief_complaints": "Headache, Cough",
"general_instruction": "Rest well and drink plenty of fluids.",
"rxNo": "RX12345"
}'
(Note: The -o prescription.pdf
flag saves the output directly to a file.)
The PDF layout is based on the EJS template located at src/templates/gp-prescription.ejs
. You can modify this file to change the appearance, add fields, or adjust styling.
- Input validation for the JSON payload.
- More robust error handling and logging.
- Authentication/Authorization for the endpoint.
- Embedding fonts directly for consistent rendering across environments.
- Unit and integration tests.
(Optional: Add a license, e.g., MIT) MIT License