heavysixer / node-pptx

Generate PPTX files on the server-side with JavaScript.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support for tables?

rajatkhare619 opened this issue · comments

Guys,

Does this library support adding tables? If not, is there a way to use
https://github.com/gitbrent/PptxGenJS/
to create a slide and add it using node-pptx?

I tried something like

const PPTX = require('nodejs-pptx');
let pptx = new PPTX.Composer();
const fs = require('fs');
const pptxgenjs  =  require("pptxgenjs");
let pres = new pptxgenjs();

let slide = pres.addSlide();
slide.bkgd = '#39006A';
slide.addText('rajat');
async function init() {
    await pptx.load(`./existing.pptx`);
    await pptx.compose(async pres => {
        pres.addSlide(slide);
        fs.writeFile('helloworlds.xml',await pres.getSlide('slide1'), function (err) {
            if (err) return console.log(err);
        });
    });
    await pptx.save(`./existing1.pptx`);
}
init();

but have been unsuccessful so far.
I get UnhandledPromiseRejectionWarning: TypeError: config is not a function

@rajatkhare619 - What kind of tables are you talking about? Like an Excel type table of tabular data, or something else?

Regarding your PptxGenJS integration question: your code example definitely won't work because the "slide" object of PttxGenJS is compleely different from the one in nodejs-pptx. In PptxGenJS, their slide object contains properties and data structures that completely describe all the elements of a slide along with their position, size, color, text (if applicable), etc. This data is then used by the "save" function to generate "Office OpenXML/PresentationML" XML for each slide, which is then zipped up and saved to disk with the "pptx" extension (OK, this is an oversimplification, but you get the idea).

In contrast, in nodejs-pptx, the slide object already contains the OpenXML data describing the slide, which is eventually written to disk when you call save. There is no "second-pass" of converting the slide description to XML like you have in PptxGenJS. The OpenXML data contained in each slide object of nodejs-pptx is dynamically changed as you manipulate the pptx content via API calls.

So, that being said, if you were able to get the post-generated OpenXML data for a slide in PptxGenJS, and then you were to transfer it to a nodejs-pptx slide object, then yes, in theory, you could use PptxGenJS to create a slide and add it to nodejs-pptx.

Note however, that if the pptx in PptxGenJS was just slightly structurally different than the one in nodejs-pptx (like containing a different number of slides, different number of images, etc.), then the internal "RId's" of certain reference objects in the PptxGenJS slide XML will no longer be valid (or point to the wrong data) when used in the nodejs-pptx generated deck.

You would need to write some code to update/fix these ID values before generating the final pptx XML.

@gregdolley Thanks for the explanation.
I was talking about these types of tables (Excel like sounds good):
https://gitbrent.github.io/PptxGenJS/docs/api-tables.html
I thought it would be easier to add text in tables which would automatically create new slides in case the table rows overflow the current slide.

I don't know if there is a way to find remaining space and currently occupied space by text with node-pptx. That would be useful.

@rajatkhare619 - Ah OK, I know the tables you're talking about. Unfortunately, our nodejs-pptx API doesn't support these kinds of tables yet.

But for your second issue of finding the amount of remaining space vertically, and therefore, knowing when to page-break -- you could probably literally calculate it from the font size, font-type, and line spacing. Given any font type, the vertical size of each character is consistent for the font size being used - so point 12 font size means the vertical dimension of the character's bounding box will be 12 points high. Line spacing is obviously consistent as well. Add those two values - we'll call it "line_height." Convert points to pixels, and calculate the number of vertical pixels of each slide (use the PptxUnitHelper class for that) and you could just do slide_pixel_height/line_height = max # of lines per slide. Ex:

let num_lines_per_slide = PptxUnitHelper.toPixels(PptxUnitHelper.fromInches(slide_height_in_inches))/line_height;

I hope this helps as a work-around.

Thanks @gregdolley! I'll see if this helps.