Skip to content

A Node.js/Express API service to generate printable prescription PDFs from JSON data using EJS and Puppeteer

Notifications You must be signed in to change notification settings

Aaleen110/prescription-pdf-generator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Prescription PDF Generator

A Node.js/Express API service to generate printable prescription PDFs from JSON data using EJS templates and Puppeteer for rendering.

Features

  • 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.

Tech Stack

Prerequisites

  • Node.js (v16 or later recommended)
  • npm

Installation

  1. Clone the repository:
    git clone git@github.com:Aaleen110/prescription-pdf-generator.git
    cd prescription-pdf-generator
  2. Install dependencies:
    npm install

Running the Server

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>

API Endpoint

Generate Prescription PDF

  • 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)

Example curl Request

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.)

Template Customization

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.

Future Improvements

  • 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.

License

(Optional: Add a license, e.g., MIT) MIT License

About

A Node.js/Express API service to generate printable prescription PDFs from JSON data using EJS and Puppeteer

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published