Search code examples
requirejspptxgenjs

How to use PptxGenJS with RequireJS


I have tried with the following code:

require(['https://cdn.jsdelivr.net/gh/gitbrent/[email protected]/dist/pptxgen.bundle.js'], 
  (pptxgenjs) => {
  var pptx = new pptxgenjs();
  var slide = pptx.addSlide();
  slide.addText(
      "BONJOUR - CIAO - GUTEN TAG - HELLO - HOLA - NAMASTE - OLÀ - ZDRAS-TVUY-TE - こんにちは - 你好",
      { x:0, y:1, w:'100%', h:2, align:'center', color:'0088CC', fill:'F1F1F1', fontSize:24 }
  );
  pptx.writeFile('PptxGenJS-Demo');  
})

I think I might need to setup a 'require.config' file but I have no idea on how to do that. Please, advise.


Solution

  • When I tried to use the bundle version it didn't work - the pptx instance didn't have the addSlide() method.

    After checking the docs and looking at the source code I've managed to make this compatible with RequireJS. Here is the code:

    // first, require the dependency of PptxGenJs - JSZip
    require(['https://cdn.jsdelivr.net/gh/gitbrent/[email protected]/libs/jszip.min.js'], (JSZip) => {
        // JSZip supports RequireJS but PptxGenJs needs it as global, so set it explicitly
        window.JSZip = JSZip;
        // here is your code, but instead of bundle fetch regular version
        require(['https://cdn.jsdelivr.net/gh/gitbrent/[email protected]/dist/pptxgen.min.js'], () => {
            var pptx = new PptxGenJS();
            var slide = pptx.addSlide();
            slide.addText(
                "BONJOUR - CIAO - GUTEN TAG - HELLO - HOLA - NAMASTE - OLÀ - ZDRAS-TVUY-TE - こんにちは - 你好",
                { x:0, y:1, w:'100%', h:2, align:'center', color:'0088CC', fill:'F1F1F1', fontSize:24 }
            );
            pptx.writeFile('PptxGenJS-Demo');
        });
    });