No description provided
This backend service provides functionalities for generating PDFs from HTML content and tracking visitor counts. It leverages technologies like Express.js, Puppeteer, and Firebase for its operations.
- Project Title & Badges
- Description
- Features
- Tech Stack
- Installation
- Usage
- Project Structure
- API Reference
- Contributing
- License
- Important Links
- Footer
The Readmecode-pdf project is a backend service primarily built with Node.js and Express.js. It is designed to generate PDF documents from HTML content using Puppeteer and to track visitor counts for various users, integrating with Firebase for data persistence. The service offers endpoints for PDF generation, visitor tracking, and badge customization.
- PDF Generation: Converts HTML content into PDF documents using Puppeteer. Supports caching for generated PDFs to improve performance.
- Visitor Count Tracking: Tracks visitor counts for specific user IDs, with options to store data in Firebase Firestore or fall back to in-memory storage.
- Dynamic SVG Badges: Generates customizable SVG badges to display visitor counts, with various styles and color options.
- Firebase Integration: Leverages Firebase for persistent storage of visitor counts and user configurations.
- CORS Configuration: Implements Cross-Origin Resource Sharing with a configurable list of allowed origins.
- Health Check Endpoint: Provides a
/healthendpoint to check the status of the service and its components. - Graceful Shutdown: Handles SIGTERM and SIGINT signals for a clean server shutdown.
- Environment Variable Support: Uses
dotenvto manage environment variables, including specific configurations for production and local development.
- Languages: JavaScript
- Frameworks/Libraries: Node.js, Express.js, React (implied by devDependencies), TypeScript (implied by devDependencies), Puppeteer, pdf-lib, Firebase, axios, body-parser, cheerio, compression, cors, dotenv, jsdom, node-fetch
- Development Tools: ESLint
-
Clone the repository:
git clone https://github.com/Sonucs12/Readmecode-pdf.git cd Readmecode-pdf -
Install dependencies:
npm install
This command will also run
npx puppeteer browsers install chromeas per thepostinstallscript inpackage.jsonto ensure Puppeteer has a compatible Chrome browser. -
Set up environment variables: Create a
.env.localfile in the root of the project and add your Firebase credentials and any other necessary environment variables:FIREBASE_API_KEY=YOUR_API_KEY FIREBASE_AUTH_DOMAIN=YOUR_AUTH_DOMAIN FIREBASE_PROJECT_ID=YOUR_PROJECT_ID FIREBASE_STORAGE_BUCKET=YOUR_STORAGE_BUCKET FIREBASE_MESSAGING_SENDER_ID=YOUR_MESSAGING_SENDER_ID FIREBASE_APP_ID=YOUR_APP_ID FIREBASE_MEASUREMENT_ID=YOUR_MEASUREMENT_ID PORT=3000
This project serves as a backend API. Here are the primary use cases:
-
Generating PDF Reports: You can send a POST request to the
/generate-pdfendpoint with HTML content to receive a PDF file.Example Request:
curl -X POST http://localhost:3000/generate-pdf \ -H "Content-Type: application/json" \ -d '{ "html": "<h1>Hello World!</h1><p>This is a test PDF.</p>", "brandName": "My App" }' \ -o output.pdf
-
Tracking Website Visitors: The service allows you to track unique visitors for your projects. A
userIdis required to identify the visitor.- Initialization: First, initialize the user and their preferred badge style via the
/initendpoint.curl -X POST http://localhost:3000/init \ -H "Content-Type: application/json" \ -d '{ "userId": "user123", "style": "style1", "timestamp": "2023-10-27T10:00:00Z", "bg": "gradient-blue", "textColor": "#ffffff" }'
- Increment Count: Send a POST request to
/incrementwith theuserIdto increment the visitor count.curl -X POST http://localhost:3000/increment \ -H "Content-Type: application/json" \ -d '{"userId": "user123"}'
- Get Badge: Retrieve a dynamic SVG badge representing the visitor count.
You can also customize the badge via query parameters:
curl http://localhost:3000/badge/user123
curl http://localhost:3000/badge/user123?style=shield1&bg=gradient-green&textColor=%23000000
- Initialization: First, initialize the user and their preferred badge style via the
-
Health Check: Check the status of the service and its components.
curl http://localhost:3000/health
This backend is designed to be integrated into larger applications. The primary use cases include:
- Generating dynamic reports or documents (e.g., invoices, certificates, summaries) that can be downloaded as PDFs.
- Adding visitor counters to personal portfolios, blogs, or open-source projects, visualized through customizable SVG badges.
- Providing an API for a frontend application that requires PDF generation or visitor analytics features.
Imagine a project where users can generate personalized resumes from a template. The frontend would send the resume's HTML content to the /generate-pdf endpoint, and the backend would return the PDF. Simultaneously, the frontend could increment a visitor count for that user's profile page by calling the /increment endpoint with the user's ID, and display a badge showing the total visits using the /badge/:userId endpoint.
Readmecode-pdf/
βββ .env.local # Environment variables (not in git)
βββ CHANGELOG.md # Project changelog
βββ Procfile # Process definition for deployment
βββ README.md # Project README file
βββ eslint.config.mjs # ESLint configuration
βββ firebaseUtils.js # Firebase Firestore utility functions
βββ package.json # Project metadata and dependencies
βββ pdfRoutes.js # Routes for PDF generation and management
βββ puppeteer.config.cjs # Puppeteer configuration
βββ server.js # Main Express server entry point
βββ svgTemplates.js # SVG badge template generation
βββ visitorRoutes.js # Routes for visitor tracking and badge generation
βββ pdf_cache/ # Directory for cached PDF files (generated at runtime)
-
POST
/generate-pdfGenerates a PDF from provided HTML content.- Request Body:
{ "html": "string", // HTML content to convert "brandName": "string" // Optional brand name for footer } - Response: PDF file stream.
- Request Body:
-
GET
/debug-pdfA debug endpoint to generate a sample PDF.- Response: PDF file stream.
-
POST
/initInitializes a user with badge preferences.- Request Body:
{ "userId": "string", "style": "string", // e.g., "style1", "shield1" "timestamp": "string", "bg": "string", // Optional: background color or gradient key "textColor": "string" // Optional: text color } - Response:
{ success: true, stored: {...} }
- Request Body:
-
POST
/incrementIncrements the visitor count for a givenuserId.- Request Body:
{ "userId": "string" } - Response:
{ success: true, count: number, storage: "firebase" | "memory" }
- Request Body:
-
GET
/badge/:userIdServes a dynamic SVG badge with the visitor count.- Query Parameters:
style: Badge style (e.g.,style1,shield1).bg: Background color or gradient key (e.g.,gradient-blue).textColor: Text color.
- Response: SVG image.
- Query Parameters:
-
POST
/badge/styleSets the preferred badge style for a user in Firestore.- Request Body:
{ "userId": "string", "style": "string" } - Response:
{ success: true }
- Request Body:
-
GET
/badge/stylesLists available badge styles.- Response:
{ styles: [{ key: "string" }, ...] }
- Response:
-
GET
/badge/gradientsLists available gradient options for badges.- Response:
{ gradients: [{ value: "string", label: "string", gradient: "string" }, ...] }
- Response:
- GET
/healthChecks the health status of the service.- Response:
{ "status": "OK", "timestamp": "string", "browserConnected": boolean, "memoryCacheSize": number }
- Response:
Contributions are welcome! Please follow these steps:
- Fork the repository.
- Create a new branch for your feature (
git checkout -b feature/AmazingFeature). - Commit your changes (
git commit -m 'Add some AmazingFeature'). - Push to the branch (
git push origin feature/AmazingFeature). - Open a Pull Request.
Please ensure you add tests for your changes and update the documentation if necessary.
This project is licensed under the MIT License - see the LICENSE file for details.
- Repository: https://github.com/Sonucs12/Readmecode-pdf
- Live Demo (Frontend): https://readmecodegen.vercel.app/ (If available, inferred from
allowedOriginsinserver.js)
Β© 2023 Sonucs12. All rights reserved.
Repository: Readmecode-pdf
Feel free to fork, star β, and report issues on the repository!
Generated by ReadmeCodeGen