### Install and Start Server Source: https://www.npmjs.com/package/%40priom7/json2pdf/v/1.0.0?activeTab=readme Clone the repository, install dependencies, and start the server. The API will be available at http://localhost:3000/api/generate-pdf. ```bash git clone https://github.com/Priom7/JSON-to-PDF-Generator.git cd JSON-to-PDF-Generator # Install dependencies npm install # Start the server npm start ``` -------------------------------- ### Install JSON to PDF Package Source: https://www.npmjs.com/package/%40priom7/json2pdf/v/1.0.0?activeTab=readme Install the @priom7/json2pdf package using npm. ```bash npm i @priom7/json2pdf ``` -------------------------------- ### Dockerfile for Deployment Source: https://www.npmjs.com/package/%40priom7/json2pdf/v/1.0.0?activeTab=readme A Dockerfile to containerize the Node.js application. It sets up the environment, installs dependencies, copies the application code, and exposes the necessary port. ```dockerfile FROM node:16-alpine WORKDIR /usr/src/app COPY package*.json ./ RUN npm install COPY . . EXPOSE 3000 CMD ["node", "server.js"] ``` -------------------------------- ### API Endpoint and Request Body Example Source: https://www.npmjs.com/package/%40priom7/json2pdf/v/1.0.0?activeTab=readme Defines the POST endpoint for PDF generation and provides an example of the expected JSON request body structure for creating a physics examination paper. ```http POST /api/generate-pdf ``` ```json { "title": "Physics Examination Paper", "subject": "Physics", "date": "April 15, 2025", "duration": "3 hours", "totalMarks": 100, "instructions": [ "All questions are compulsory", "There is no negative marking" ], "questions": [ { "type": "mcq", "text": "A particle moves along the x-axis...", "options": ["300 J", "900 J", "1000 J", "3000 J"], "correctOption": 3, "solution": "Work done = ∫F(x)dx from x=0 to x=10..." } ] } ``` -------------------------------- ### Request Body Example for PDF Generation Source: https://www.npmjs.com/package/%40priom7/json2pdf An example of the JSON structure required for the /api/generate-pdf endpoint, including paper title, subject, date, instructions, and a list of questions with their types and details. ```json { "title": "Physics Examination Paper", "subject": "Physics", "date": "April 15, 2025", "duration": "3 hours", "totalMarks": 100, "instructions": [ "All questions are compulsory", "There is no negative marking" ], "questions": [ { "type": "mcq", "text": "A particle moves along the x-axis...", "options": ["300 J", "900 J", "1000 J", "3000 J"], "correctOption": 3, "solution": "Work done = ∫F(x)dx from x=0 to x=10..." } ] } ``` -------------------------------- ### Contributing to the Repository Source: https://www.npmjs.com/package/%40priom7/json2pdf?activeTab=readme Follow these steps to contribute to the project: fork the repo, create a feature branch, commit changes, push to your branch, and open a pull request. ```bash git checkout -b feature/your-feature git commit -m "Add your feature" git push origin feature/your-feature ``` -------------------------------- ### Run Test Script for PDF Generation Source: https://www.npmjs.com/package/%40priom7/json2pdf/v/1.0.0?activeTab=readme Execute the test script to generate a PDF from sample data. Ensure `sample-data.json` exists in the same directory. ```bash node test.js ``` -------------------------------- ### Docker Build and Run Commands Source: https://www.npmjs.com/package/%40priom7/json2pdf/v/1.0.0?activeTab=readme Commands to build the Docker image and run the container. This allows for easy deployment and scaling of the PDF generation service. ```bash docker build -t pdf-generator . docker run -p 3000:3000 pdf-generator ``` -------------------------------- ### Image Support Configuration Source: https://www.npmjs.com/package/%40priom7/json2pdf/v/1.0.0?activeTab=readme Shows how to include image support in the JSON data by specifying the image path, dimensions, and position. ```json "images": [ { "path": "path/to/image.png", "width": 300, "height": 200, "position": "center" } ] ``` -------------------------------- ### Enable Debug Logs for Server Source: https://www.npmjs.com/package/%40priom7/json2pdf/v/1.0.0?activeTab=readme To troubleshoot issues, enable debug logs by setting the DEBUG environment variable to true when running the server. ```bash DEBUG=true node server.js ``` -------------------------------- ### AWS Elastic Beanstalk Deployment Source: https://www.npmjs.com/package/%40priom7/json2pdf/v/1.0.0?activeTab=readme Basic commands for deploying the application to AWS Elastic Beanstalk. This includes initializing the EB environment, creating a new environment, and deploying the application. ```bash eb init eb create pdf-generator-env eb deploy ``` -------------------------------- ### Test PDF Generation Script Source: https://www.npmjs.com/package/%40priom7/json2pdf/v/1.0.0?activeTab=readme This script reads data from `sample-data.json`, generates a PDF using the `generatePDF` function, and saves it as `output-test.pdf`. It includes error handling for missing data files. ```javascript const fs = require('fs'); const path = require('path'); const { generatePDF } = require('./pdfGenerator'); async function testPDFGeneration() { const dataPath = path.join(__dirname, 'sample-data.json'); if (!fs.existsSync(dataPath)) return console.error('sample-data.json not found'); const data = JSON.parse(fs.readFileSync(dataPath, 'utf8')); const pdf = await generatePDF(data); fs.writeFileSync('output-test.pdf', pdf); console.log('PDF generated → output-test.pdf'); } testPDFGeneration().catch(console.error); ``` -------------------------------- ### Heroku Deployment Source: https://www.npmjs.com/package/%40priom7/json2pdf/v/1.0.0?activeTab=readme Commands for deploying the application to Heroku. This involves creating a new Heroku app and pushing the code to the Heroku remote repository. ```bash heroku create git push heroku main ``` -------------------------------- ### Register Custom Fonts Source: https://www.npmjs.com/package/%40priom7/json2pdf/v/1.0.0?activeTab=readme Demonstrates how to register custom TTF fonts for use in PDF generation. Add font files to a 'fonts/' directory and reference them in the configuration. ```javascript doc.registerFont('CustomFont', 'fonts/CustomFont.ttf'); ``` ```javascript fonts: { regular: 'CustomFont', bold: 'CustomFont-Bold', italic: 'CustomFont-Italic' } ``` -------------------------------- ### Generate PDF Endpoint Source: https://www.npmjs.com/package/%40priom7/json2pdf?activeTab=code This endpoint accepts a JSON payload containing question paper details and returns a binary PDF file. ```APIDOC ## POST /api/generate-pdf ### Description Generates a PDF document from structured JSON data. ### Method POST ### Endpoint /api/generate-pdf ### Request Body - **Content-Type**: application/json - **Schema**: See JSON Schema section for detailed structure. ### Request Example ```json { "title": "Physics Examination Paper", "subject": "Physics", "date": "April 15, 2025", "duration": "3 hours", "totalMarks": 100, "instructions": [ "All questions are compulsory", "There is no negative marking" ], "questions": [ { "type": "mcq", "text": "A particle moves along the x-axis...", "options": ["300 J", "900 J", "1000 J", "3000 J"], "correctOption": 3, "solution": "Work done = ∫F(x)dx from x=0 to x=10..." } ] } ``` ### Response #### Success Response (200 OK) - **Content-Type**: application/pdf - **Body**: Binary PDF file #### Error Responses - **400 Bad Request**: Invalid JSON payload. - **500 Internal Server Error**: PDF generation failed. ``` -------------------------------- ### Layout Configuration Source: https://www.npmjs.com/package/%40priom7/json2pdf/v/1.0.0?activeTab=readme Defines the default layout configuration for PDF generation, including page size, margins, columns, and font settings. ```javascript const CONFIG = { pageSize: 'A4', margin: { top: 50, bottom: 50, left: 40, right: 40 }, columns: 2, columnGap: 20, fonts: { regular: 'Helvetica', bold: 'Helvetica-Bold', italic: 'Helvetica-Oblique' }, fontSize: { title: 16, header: 12, subHeader: 10, normal: 10, small: 8 } }; ``` -------------------------------- ### Question Object Schema Source: https://www.npmjs.com/package/@priom7/json2pdf Details the structure for individual question objects within the JSON schema. Supports different question types and optional fields like solutions and images. ```json { "type": "String", "text": "String", "options": "Array", "correctOption": "Number", "answer": "String", "solution": "String", "images": "Array" } ``` -------------------------------- ### Generate PDF Endpoint Source: https://www.npmjs.com/package/%40priom7/json2pdf This endpoint accepts a JSON payload and returns a PDF document. It's designed for generating question papers with structured content. ```APIDOC ## POST /api/generate-pdf ### Description Generates a PDF document from structured JSON data. ### Method POST ### Endpoint /api/generate-pdf ### Headers - **Content-Type**: application/json ### Request Body **Type**: `application/json` **Example**: ```json { "title": "Physics Examination Paper", "subject": "Physics", "date": "April 15, 2025", "duration": "3 hours", "totalMarks": 100, "instructions": [ "All questions are compulsory", "There is no negative marking" ], "questions": [ { "type": "mcq", "text": "A particle moves along the x-axis...", "options": ["300 J", "900 J", "1000 J", "3000 J"], "correctOption": 3, "solution": "Work done = ∫F(x)dx from x=0 to x=10..." } ] } ``` ### Response #### Success Response (200 OK) - **Content-Type**: `application/pdf` - **Body**: Binary PDF file #### Error Responses - **400 Bad Request**: Invalid JSON payload. - **500 Internal Server Error**: PDF generation failed. ``` -------------------------------- ### Generate PDF Endpoint Source: https://www.npmjs.com/package/%40priom7/json2pdf/v/1.0.0?activeTab=code This endpoint accepts a JSON payload containing question paper details and returns a generated PDF file. ```APIDOC ## POST /api/generate-pdf ### Description Generates a PDF document from structured JSON data. ### Method POST ### Endpoint /api/generate-pdf ### Parameters #### Request Body - **title** (String) - Required - Title of the examination paper - **subject** (String) - Required - Subject name - **date** (String) - Required - Exam date - **duration** (String) - Required - Exam duration (e.g., "3 hours") - **totalMarks** (Number) - Required - Total marks - **instructions** (Array) - Required - Exam instructions - **questions** (Array) - Required - List of questions - **type** (String) - Required - Type of question: "mcq", "numerical", or "descriptive" - **text** (String) - Required - Question text - **options** (Array) - Optional (For MCQ) - Answer options - **correctOption** (Number) - Optional (For MCQ) - Index of correct option - **answer** (String) - Optional - Correct answer (numerical/descriptive) - **solution** (String) - Optional - Detailed solution explanation - **images** (Array) - Optional - Images with `path`, `width`, `height`, `position` ### Request Example ```json { "title": "Physics Examination Paper", "subject": "Physics", "date": "April 15, 2025", "duration": "3 hours", "totalMarks": 100, "instructions": [ "All questions are compulsory", "There is no negative marking" ], "questions": [ { "type": "mcq", "text": "A particle moves along the x-axis...", "options": ["300 J", "900 J", "1000 J", "3000 J"], "correctOption": 3, "solution": "Work done = ∫F(x)dx from x=0 to x=10..." } ] } ``` ### Response #### Success Response (200 OK) - **Content-Type**: `application/pdf` - Body: Binary PDF file #### Error Responses - **400 Bad Request**: Invalid JSON - **500 Internal Server Error**: PDF generation failed ``` -------------------------------- ### Generate PDF Endpoint Source: https://www.npmjs.com/package/%40priom7/json2pdf?activeTab=dependencies This endpoint accepts a JSON payload containing question paper details and returns a generated PDF file. ```APIDOC ## POST /api/generate-pdf ### Description Generates a PDF document from structured JSON data representing a question paper. ### Method POST ### Endpoint `/api/generate-pdf` ### Request Body **Content-Type**: `application/json` **Example**: ```json { "title": "Physics Examination Paper", "subject": "Physics", "date": "April 15, 2025", "duration": "3 hours", "totalMarks": 100, "instructions": [ "All questions are compulsory", "There is no negative marking" ], "questions": [ { "type": "mcq", "text": "A particle moves along the x-axis...", "options": ["300 J", "900 J", "1000 J", "3000 J"], "correctOption": 3, "solution": "Work done = ∫F(x)dx from x=0 to x=10..." } ] } ``` ### Response **Content-Type**: `application/pdf` #### Success Response (200 OK) Returns a binary PDF file. #### Error Responses - `400 Bad Request` – Invalid JSON payload. - `500 Internal Server Error` – Failed to generate PDF. ``` -------------------------------- ### Generate PDF Endpoint Source: https://www.npmjs.com/package/%40priom7/json2pdf/v/1.0.0?activeTab=readme This endpoint accepts structured JSON data and returns a generated PDF document. It is designed for creating question papers with various question types and formatting options. ```APIDOC ## POST /api/generate-pdf ### Description Generates a PDF document from the provided JSON data. ### Method POST ### Endpoint /api/generate-pdf ### Parameters #### Request Body - **Content-Type**: application/json ### Request Body Example ```json { "title": "Physics Examination Paper", "subject": "Physics", "date": "April 15, 2025", "duration": "3 hours", "totalMarks": 100, "instructions": [ "All questions are compulsory", "There is no negative marking" ], "questions": [ { "type": "mcq", "text": "A particle moves along the x-axis...", "options": ["300 J", "900 J", "1000 J", "3000 J"], "correctOption": 3, "solution": "Work done = ∫F(x)dx from x=0 to x=10..." } ] } ``` ### Response #### Success Response (200 OK) - **Content-Type**: application/pdf - Body: Binary PDF file #### Error Responses - **400 Bad Request**: Invalid JSON input. - **500 Internal Server Error**: PDF generation failed. ``` -------------------------------- ### Generate PDF Endpoint Source: https://www.npmjs.com/package/%40priom7/json2pdf/v/1.0.0?activeTab=versions This endpoint accepts a JSON payload and returns a PDF document. It's designed for generating question papers with various question types and formatting options. ```APIDOC ## POST /api/generate-pdf ### Description Generates a PDF document from structured JSON data. This endpoint is suitable for creating question papers, answer keys, and solutions. ### Method POST ### Endpoint `/api/generate-pdf` ### Request Body **Content-Type**: `application/json` **Example**: ```json { "title": "Physics Examination Paper", "subject": "Physics", "date": "April 15, 2025", "duration": "3 hours", "totalMarks": 100, "instructions": [ "All questions are compulsory", "There is no negative marking" ], "questions": [ { "type": "mcq", "text": "A particle moves along the x-axis...", "options": ["300 J", "900 J", "1000 J", "3000 J"], "correctOption": 3, "solution": "Work done = \u222bF(x)dx from x=0 to x=10..." } ] } ``` ### Response **Content-Type**: `application/pdf` #### Success Response (200 OK) Returns a binary PDF file. #### Error Responses - **400 Bad Request**: Invalid JSON payload. - **500 Internal Server Error**: PDF generation failed. ``` -------------------------------- ### Generate PDF Endpoint Source: https://www.npmjs.com/package/%40priom7/json2pdf?activeTab=versions This endpoint accepts a JSON payload and returns a PDF document. It supports various question types and formatting options. ```APIDOC ## POST /api/generate-pdf ### Description Generates a PDF document from structured JSON data. ### Method POST ### Endpoint /api/generate-pdf ### Headers - **Content-Type**: application/json ### Request Body Accepts a JSON object containing paper details and questions. See the JSON Schema section for detailed structure. ### Request Example ```json { "title": "Physics Examination Paper", "subject": "Physics", "date": "April 15, 2025", "duration": "3 hours", "totalMarks": 100, "instructions": [ "All questions are compulsory", "There is no negative marking" ], "questions": [ { "type": "mcq", "text": "A particle moves along the x-axis...", "options": ["300 J", "900 J", "1000 J", "3000 J"], "correctOption": 3, "solution": "Work done = ∫F(x)dx from x=0 to x=10..." } ] } ``` ### Response #### Success Response (200 OK) - **Content-Type**: application/pdf - Body: Binary PDF file #### Error Responses - **400 Bad Request**: Invalid JSON payload. - **500 Internal Server Error**: PDF generation failed. ``` -------------------------------- ### Generate PDF Endpoint Source: https://www.npmjs.com/package/@priom7/json2pdf This endpoint accepts a JSON payload and returns a PDF document. It supports various question types and formatting options. ```APIDOC ## POST /api/generate-pdf ### Description Generates a PDF document from structured JSON data. ### Method POST ### Endpoint /api/generate-pdf ### Request Body - **Content-Type**: `application/json` - **Body**: A JSON object containing the structure for the question paper. See example below. ### Request Example ```json { "title": "Physics Examination Paper", "subject": "Physics", "date": "April 15, 2025", "duration": "3 hours", "totalMarks": 100, "instructions": [ "All questions are compulsory", "There is no negative marking" ], "questions": [ { "type": "mcq", "text": "A particle moves along the x-axis...", "options": ["300 J", "900 J", "1000 J", "3000 J"], "correctOption": 3, "solution": "Work done = ∫F(x)dx from x=0 to x=10..." } ] } ``` ### Response #### Success Response (200 OK) - **Content-Type**: `application/pdf` - Body: Binary PDF file #### Error Responses - **400 Bad Request**: Invalid JSON payload. - **500 Internal Server Error**: PDF generation failed. ``` -------------------------------- ### Generate PDF Endpoint Source: https://www.npmjs.com/package/%40priom7/json2pdf?activeTab=dependents This endpoint accepts a JSON payload containing question paper details and returns a PDF document. ```APIDOC ## POST /api/generate-pdf ### Description Generates a PDF document from structured JSON data. ### Method POST ### Endpoint /api/generate-pdf ### Headers - **Content-Type**: application/json ### Request Body Accepts a JSON object with the following structure: - **title** (String) - Title of the examination paper - **subject** (String) - Subject name - **date** (String) - Exam date - **duration** (String) - Exam duration (e.g., "3 hours") - **totalMarks** (Number) - Total marks - **instructions** (Array) - Exam instructions - **questions** (Array) - List of questions - **type** (String) - Type of question: "mcq", "numerical", or "descriptive" - **text** (String) - The question text - **options** (Array) - (For MCQ) Answer options - **correctOption** (Number) - (For MCQ) Index of the correct option - **answer** (String) - (For numerical/descriptive) Correct answer - **solution** (String) - Detailed solution explanation - **images** (Array) - (Optional) Images with `path`, `width`, `height`, `position` ### Request Example ```json { "title": "Physics Examination Paper", "subject": "Physics", "date": "April 15, 2025", "duration": "3 hours", "totalMarks": 100, "instructions": [ "All questions are compulsory", "There is no negative marking" ], "questions": [ { "type": "mcq", "text": "A particle moves along the x-axis...", "options": ["300 J", "900 J", "1000 J", "3000 J"], "correctOption": 3, "solution": "Work done = ∫F(x)dx from x=0 to x=10..." } ] } ``` ### Response #### Success Response (200 OK) - **Content-Type**: application/pdf - Body: Binary PDF file #### Error Responses - **400 Bad Request**: Invalid JSON payload. - **500 Internal Server Error**: PDF generation failed. ``` -------------------------------- ### API Endpoint for PDF Generation Source: https://www.npmjs.com/package/%40priom7/json2pdf Defines the POST endpoint for generating PDFs. It specifies the expected Content-Type for requests and the Response type. ```http POST /api/generate-pdf ``` -------------------------------- ### Page Numbering Implementation Source: https://www.npmjs.com/package/%40priom7/json2pdf/v/1.0.0?activeTab=readme Adds page numbering to the generated PDF. This code snippet should be added to `pdfGenerator.js` to display the current page number at the bottom center of each page. ```javascript doc.on('pageAdded', () => { const pageCount = doc.bufferedPageRange().count; doc.switchToPage(pageCount - 1); doc.fontSize(8) .text(`Page ${pageCount}`, doc.page.width / 2, doc.page.height - 20, { align: 'center' }); }); ``` -------------------------------- ### JSON Schema for Examination Paper Source: https://www.npmjs.com/package/@priom7/json2pdf Defines the structure and types for the JSON data used to generate PDF question papers. Includes fields for paper details and a list of questions. ```json { "title": "String", "subject": "String", "date": "String", "duration": "String", "totalMarks": "Number", "instructions": "Array", "questions": "Array" } ``` -------------------------------- ### Include Logo in PDF Source: https://www.npmjs.com/package/%40priom7/json2pdf?activeTab=readme To add a logo to your generated PDF, include the `logoPath` field in your JSON request body, specifying the path to your logo image file. ```json "logoPath": "path/to/logo.png" ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.