PptxGenJS Documentation

repository·master·Indexed 26 days ago

https://github.com/gitbrent/pptxgenjs

A JavaScript library for programmatically generating professional PowerPoint (OOXML) presentations. PptxGenJS works in Node.js and browser environments, supporting frameworks like React, Angular, and Vite. It provides capabilities to create slides, add text, shapes, images, and charts, and convert HTML tables to slides. The library supports multiple export formats including base64, Blob, Buffer, and Node streams.

Tokens
8.3K
Snippets
11
Records
65
Agent score
91%

What's inside PptxGenJS

  1. Install PptxGenJS via CDN for browser usage

    master

    For direct use in a web browser without a build step, include the bundled version via jsDelivr. This bundle includes the required JSZip dependency in a single file.

    <script src="https://cdn.jsdelivr.net/gh/gitbrent/pptxgenjs/dist/pptxgen.bundle.js"></script>
  2. Test PptxGenJS with Vite and TypeScript

    master

    To validate integration in modern front-end SPA toolchains (Vite, TypeScript, React):

    1. Prepare Files: Ensure dist/pptxgen.es.js and types/index.d.ts are copied to your local node_modules.
    2. Configure Demo: Update package.json in demos/vite-demo/.
    3. Verify Types: Open src/tstest/Test.tsx and use IntelliSense to verify autocompletion (e.g., pptxgen.ChartType.).
    4. Run App:
      cd demos/vite-demo
      npm install
      npm run dev
    5. Cross-Device Validation: Access the demo from your network (MacBook, iPhone, or Android) to ensure MIME types are valid and files render correctly in PowerPoint or previewers.
    cd demos/vite-demo
    npm install
    npm run dev
  3. Configure React-specific ESLint rules

    master

    To add React-specific linting, install eslint-plugin-react-x and eslint-plugin-react-dom, then add them to your eslint.config.js plugins and rules sections.

    // eslint.config.js
    import reactX from 'eslint-plugin-react-x'
    import reactDom from 'eslint-plugin-react-dom'
    
    export default tseslint.config({
      plugins: {
        // Add the react-x and react-dom plugins
        'react-x': reactX,
        'react-dom': reactDom,
      },
      rules: {
        // other rules...
        // Enable its recommended typescript rules
        ...reactX.configs['recommended-typescript'].rules,
        ...reactDom.configs.recommended.rules,
      },
    })
  4. Enable type-aware ESLint rules in Vite + TypeScript

    master

    For production applications using this Vite + TypeScript template, it is recommended to enable type-aware lint rules by replacing tseslint.configs.recommended with tseslint.configs.recommendedTypeChecked, tseslint.configs.strictTypeChecked, or tseslint.configs.stylisticTypeChecked. You must also configure parserOptions to point to your tsconfig files.

    export default tseslint.config({
      extends: [
        // Remove ...tseslint.configs.recommended and replace with this
        ...tseslint.configs.recommendedTypeChecked,
        // Alternatively, use this for stricter rules
        ...tseslint.configs.strictTypeChecked,
        // Optionally, add this for stylistic rules
        ...tseslint.configs.stylisticTypeChecked,
      ],
      languageOptions: {
        // other options...
        parserOptions: {
          project: ['./tsconfig.node.json', './tsconfig.app.json'],
          tsconfigRootDir: import.meta.dirname,
        },
      },
    })
  5. Run the Regular Node.js Demo

    master
    The regular Node.js demo allows you to generate various types of PowerPoint presentations via the command line. You can generate a simple presentation, a presentation containing all available demo objects, or a presentation containing specific object types (e.g., 'Table', 'Text').
  6. Quick Start: Generate a basic presentation

    master

    Creating a presentation involves four steps: initializing the presentation object, adding a slide, adding objects (like text, shapes, or images) to that slide, and saving the file.

    import pptxgen from "pptxgenjs";
    
    // 1. Create a new Presentation
    let pres = new pptxgen();
    
    // 2. Add a Slide
    let slide = pres.addSlide();
    
    // 3. Add one or more objects (Tables, Shapes, Images, Text and Media) to the Slide
    let textboxText = "Hello World from PptxGenJS!";
    let textboxOpts = { x: 1, y: 1, color: "363636" };
    slide.addText(textboxText, textboxOpts);
    
    // 4. Save the Presentation
    pres.writeFile();
  7. Test PptxGenJS in the Browser

    master

    To validate browser compatibility using the standalone bundle as a script:

    1. Start the local test server:
      cd demos
      node browser_server.mjs
    2. Open the Demo Page at http://localhost:8000/browser/index.html.
    3. Verify in DevTools (Sources tab) that pptxgen.bundle.js is the latest version.
    4. Run UI-driven demos and verify rendering.
    5. Mobile Testing: Access the page via your local IP (e.g., http://192.168.254.x:8000/browser/index.html) on an iPhone to test mobile compatibility.

    Web Worker API Testing:

    • Open http://localhost:8000/browser/worker_test.html.
    • Note: Use Chrome only; Safari is not supported for this test.

    Microsoft 365 Validation:

    • Upload the generated PPTX files to OneDrive/Office/M365 and use the web viewer to ensure file integrity.
    cd demos
    node browser_server.mjs
  8. Build for gh-pages (Manual)

    master

    When preparing a release for gh-pages, do not use the Vite deploy script. Instead, follow these manual steps to ensure full control over the content:

    1. Run the build command:
      npm run build
    2. Manually copy the entire `dist` folder from `demos/vite-demo/` to a safe location.
    3. Use this copied folder when updating the `gh-pages` branch.
    
  9. Test PptxGenJS in Node.js

    master

    To validate the CommonJS module functionality in pure Node.js environments, use the following commands within the demos/node directory:

    CLI Tests: Run these to confirm console output and exported PPTX files:

    cd demos/node
    npm run demo
    npm run demo-all

    Stream Tests: Run this to confirm the stream download functionality:

    npm run demo-stream

    Then, verify the resulting file by accessing the stream URL on a mobile device.

    cd demos/node
    npm run demo
    npm run demo-all
    # or
    npm run demo-stream
  10. Run PptxGenJS manual tests

    master

    To perform a full manual test suite across all supported platforms (Browser, Node.js, Web Worker, and Vite/TypeScript), follow these steps:

    1. Run npm run ship to prepare the environment.
    2. Execute the specific test suites for your target environment (Browser, Node, or Vite) as detailed in the platform-specific sections.
    npm run ship