question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

adding image not working.

See original GitHub issue

Hi, I am using exceljs in a html environment. I wanted to use addImage. But as soon as i do that i get an exception.

i used the following code:

// add image to workbook by filename
			var imageId1 = workbook.addImage({
			  filename: 'img/logo.jpeg',
			  extension: 'jpeg',
			});

i am using the following require.js definitions ` //Set the path to jQuery to local path requirejs.config({ paths: { jquery: ‘js/jquery’, Excel: “js/exceljs_2017_07_16”, //Excel: “js/exceljs”, fs: “js/filesaver.min”, // enables download of files within javascript - is used from exceljs jszip: “js/jszip.min”, // jszip is used to generate zip files - is used from exceljs lodash: “js/lodash”, // jszip is used to generate zip files - is used from exceljs es6shim: “js/es6-shim” // jszip is used to generate zip files - is used from exceljs } });

require([‘Excel’, ‘fs’, ‘jszip’, ‘lodash’, ‘es6shim’, ‘jquery’ ], function (Excel, fileSaver) { … } i get the message that fs.readFile is not a function TypeError: fs.readFile is not a function at http://localhost:8080/exceljsTest/js/exceljs_2017_07_16.js:10749:8 at lib$es6$promise$$internal$$initializePromise (http://localhost:8080/exceljsTest/js/exceljs_2017_07_16.js:14955:9) at Promish.lib$es6$promise$promise$$Promise [as constructor] (http://localhost:8080/exceljsTest/js/exceljs_2017_07_16.js:15246:9) at new Promish (http://localhost:8080/exceljsTest/js/exceljs_2017_07_16.js:35957:108) at fsReadFileAsync (http://localhost:8080/exceljsTest/js/exceljs_2017_07_16.js:10748:10) at http://localhost:8080/exceljsTest/js/exceljs_2017_07_16.js:11086:18 at Array.map (native) at 85.module.exports.addMedia (http://localhost:8080/exceljsTest/js/exceljs_2017_07_16.js:11082:47) at http://localhost:8080/exceljsTest/js/exceljs_2017_07_16.js:11311:21 at lib$es6$promise$$internal$$tryCatch (http://localhost:8080/exceljsTest/js/exceljs_2017_07_16.js:14908:16) `

The following code inside of exceljs (line 10749) is used (with a reference to fs. function fsReadFileAsync(filename, options) { return new PromishLib.Promish(function (resolve, reject) { fs.readFile(filename, options, function (error, data) { if (error) { reject(error); } else { resolve(data); } }); }); }

What is the way to attach fs = What library do i have to include ? i am sure i can’t include node.js 😃 Thanks and best regards Manfred

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:15 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
ritesh-nitwcommented, Jun 13, 2018

@Mafi78 : There are two interfaces of this library, stream mode and doc mode. Doc mode: var workbook = new Excel.Workbook(); Stream mode: var workBook = new Excel.stream.xlsx.WorkbookWriter({useSharedStrings: true, useStyles: true, stream: res}); Stream mode is used when you have massive data, always recommended by author while doc mode blows up when you have more than 33K rows *100 cols. Your fixes is for doc mode.

In stream mode, addImage function is missing from both workbook-writer and worksheet-writer. Infact, the author has not implemented addImage functionality for stream mode.

The simplest replicator is:

const Excel = require('exceljs');
var workBook = new Excel.stream.xlsx.WorkbookWriter({useSharedStrings: true, useStyles: true, stream: res});// res is the response stream
workBook.addWorksheet('test');
var currentWorksheet= workBook.getWorksheet('test');
var imageId = workBook.addImage({
                            base64: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAH4AAAAsCAMAAACUu/xGAAAAq1BMVEUAAABlZVJlZVKsrJthYU+zs6Grq5ylpZazs6FlZVJfX01lZVJlZVKsrJurq5urq5xlZVKtrZ1lZVJlZVKvr52zs6GysqCoqJeqqpmzs6Grq5xlZVJgYE6zs6Gnp5mrq5yiopRjY1CRkX2rq5yzs6FlZVKRkX2goJKKineRkX2Pj3yrq5yIiHWRkX2RkX2RkX1lZVKRkX2rq5yzs6GoqJdfX02goJKHh3SHh3VrpzVsAAAAMHRSTlMAQIDHx3+Ax0Ag7qBgIA9AEFCPMLOgMO7bYKBQ24+zYNuzkY9wcAXu0oiocPFBMHYlVbK0AAAD3UlEQVRYw6SW7Y6qMBCGB0IkLfKdnB9ocFmjru7HERL03P+VnXY6bdmWjcF9f2inxjydvjMDcHy99zP693oEp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6hIYsPP8MwTVR5SLZA4573xHMpCV7xGZBFmxyProfR64yNCgKh4hygjXIuvpdcbPyEayA2vsEpRHcgl6gtzr8A9ho0RlgQnBPoK4tV45gBfGQZ6KQBDqzRcjdeAqQwHUfYp+SohcQdc1/Ukm4Gw4dV6vqTkM+uQpRv8E2VPF/sPp9xSb2qlGH4AAAAASUVORK5CYII=',
                            extension: 'png'
                        });
                        currentWorksheet.addImage(imageId, {
                            tl: {col: 0, row: 0},
                            br: {col: 9, row: 18},
                            editAs: 'absolute'
                        });
 workBook.commit()
            .then(function () {
                // the stream has been written
                callBack();
            });

Throws exception on workBook.addImage and currentWorksheet.addImage.

1reaction
canotechcommented, May 14, 2021

You should use “path” library from NodeJS for adding image to workbook:

import * as path from ‘path’;

const IMAGE_PATH = path.resolve(‘public/image.jpg’); // path of our image … const logo = workbook.addImage({ filename: IMAGE_PATH, extension: ‘jpg’, }); … workSheet.addImage(logo, ‘A1:B5’); // set image in range of columns

This working for me

Read more comments on GitHub >

github_iconTop Results From Across the Web

7 Reasons Why Images Are Not Loading on Your Website
7 Reasons Why Images Are Not Loading on Your Website · 1. Incorrect File Paths · 2. Files Names Misspelled · 3. Wrong...
Read more >
Reasons Why Images Not Loading on Your Website
See now some reasons that can explain why your images not loading on your site and the way to fix it. a picture...
Read more >
How to fix image not showing in an HTML page
How to fix image not showing in an HTML page · Check the src path to of the <img> tag · Check the...
Read more >
Image is not showing in browser? - html - Stack Overflow
I find out the way how to set the image path just remove the "/" before the destination folder as "images/66.jpg" not "/images/66.jpg"...
Read more >
Error: Images Not Appearing on My Web Page
Below are some reasons why your images may not be displayed correctly on your Web page. Image files were not uploaded to the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found