Skip to content

Add Base64 Image support for the .addImage() method #442

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,13 @@ var imageId2 = workbook.addImage({
buffer: fs.readFileSync('path/to.image.png'),
extension: 'png',
});

// add image to workbook by base64
var myBase64Image = "data:image/png;base64,iVBORw0KG...";
var imageId2 = workbook.addImage({
base64: myBase64Image,
extension: 'png',
});
```

### Add image background to worksheet
Expand Down
6 changes: 5 additions & 1 deletion lib/utils/zip-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ var ZipWriter = function() {
utils.inherits(ZipWriter, events.EventEmitter, {

append: function(data, options) {
this.zip.file(options.name, data);
if (options.hasOwnProperty('base64') && options.base64) {
this.zip.file(options.name, data, { base64: true });
} else {
this.zip.file(options.name, data);
}
},
finalize: function() {
var options = {
Expand Down
8 changes: 8 additions & 0 deletions lib/xlsx/xlsx.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,14 @@ XLSX.prototype = {
resolve();
});
}
if (medium.base64) {
return new PromishLib.Promish(function(resolve) {
var dataimg64 = medium.base64;
var content = dataimg64.substring(dataimg64.indexOf(',') + 1);
zip.append(content, { name: filename, base64: true });
resolve();
});
}
}
return PromishLib.Promish.reject(new Error('Unsupported media'));
}));
Expand Down