Issue with loading PowerPoint file from buffer
The save() method allows you to specify either a callback (passing buffer) or file path, but when loading a PowerPoint file, you must pass a file path. This can be pretty inconvenient in cases where you already have the PowerPoint file contents in a buffer.
Proposed solution
To address this issue, can we please add support for loading from a buffer as well as a file path.
I've quickly scanned your code, and at first glance it looks like it's as simple as changing loadExistingPPTX() :
|
async loadExistingPPTX(done) { |
|
if (this.templateFilePath !== null) { |
|
await this.powerPointFactory.loadFromRawFileData(fs.readFileSync(this.templateFilePath)); |
|
} |
|
} |
To something
like:
async loadExistingPPTX(done) {
// Throw an error if templateFilePath is null or undefined
if (!this.templateFilePath) {
throw new Error('Input is null or undefined. Input must be a file path or buffer.');
}
try {
// Check if templateFilePath is a string (file path)
if (typeof this.templateFilePath === 'string') {
// Load PowerPoint file from the specified file path using fs.readFileSync()
await this.powerPointFactory.loadFromRawFileData(fs.readFileSync(this.templateFilePath));
}
// Check if templateFilePath is a buffer
else if (Buffer.isBuffer(this.templateFilePath)) {
// Load PowerPoint file from the buffer directly using powerPointFactory.loadFromRawFileData()
await this.powerPointFactory.loadFromRawFileData(this.templateFilePath);
}
// If templateFilePath is neither a string nor a buffer, throw an error
else {
throw new Error('Invalid input type. Input must be a file path or buffer.');
}
} catch (error) {
// Catch any errors that occur during loading and rethrow with a more descriptive message
throw new Error(`Error loading PPTX file: ${error.message}`);
}
}
Obviously we'd also need to change the variable name to templateFile or similar and any other housekeeping as needed.
Would you accept a PR from me along these lines to add this feature?
Issue with loading PowerPoint file from buffer
The
save()method allows you to specify either a callback (passing buffer) or file path, but when loading a PowerPoint file, you must pass a file path. This can be pretty inconvenient in cases where you already have the PowerPoint file contents in a buffer.Proposed solution
To address this issue, can we please add support for loading from a buffer as well as a file path.
I've quickly scanned your code, and at first glance it looks like it's as simple as changing
loadExistingPPTX():node-pptx/lib/presentation.js
Lines 99 to 103 in 7e137b5
To something like:
Obviously we'd also need to change the variable name to
templateFileor similar and any other housekeeping as needed.Would you accept a PR from me along these lines to add this feature?