Node : Excel to buffer conversion loses styling - node.js

I have a scenario where Im using the 'excel4node' npm dependency to create a excel workbook with some custom styling in it (Like block lettered headers etc). I'm converting it to buffer using wb.writeToBuffer() in excel4node library, and writing it to an s3 bucket. On the other end, I'm fetching the object from the s3 bucket, and converting the buffer from s3 back to excel file using the XLSX dependency (Cuz looks like excel4node doesnt support buffer to excel conversion) as follows
const XLSX = require('xlsx');
let workbook = XLSX.read(data.Body);
const wbout = XLSX.write(workbook, { bookType: 'xlsx', type: 'buffer'});
In this conversion process, I am losing the custom styling I added while creating the excel file using excel4node dependency. How do I handle this case where I dont want to lose the custom styling.

Related

Reading xls file is returning html tags using library xlsx

I am using xlsx library of nodejs for reading xls file. According to the document the library supports xls file format. On reading I am getting html tags along with it.
I can remove the html tags using regex or replace function but does the library give support to do that as I couldn't find it in the documentation?
Excel File format: Microsoft Excel 97-2003 Worksheet (.xls)
The demo link they have provided in their documentation https://oss.sheetjs.com/sheetjs/ works but when I try to do the same with my code it doesn't give the desired result.
let xlsx = require('xlsx');
let fs = require('fs');
let workBookData = xlsx.readFile('data.xls'); // parses a file
console.log(workBookData);
Here is an image of the result I am getting.
This was an issue/bug in the library. A PR has been created for this and it will be fixed in the new version of the library.

Is there an npm package for converting RTF strings to PDF file in node.js?

I'm looking for an RTF to PDF converter for Node.js. I'm not finding too many packages over at the npm site.
The application is as follows: I need to pull an RTF document (created in Word and saved as .rtf) from an AWS S3 bucket. Then I need to read the file as an RTF string. Next, I need to replace certain placeholder tokens in the RTF string with values (ex. in "{host-name} would like to invite you to {address} on {date}."--I need to replace {host-name}, {address}, and {date} with actual values). Then, with actual values plugged in, I need to convert the string to a PDF that I can attach to an email.
For this reason, I cannot use any of the several Word-to-PDF packages out there (because I need to intervene in the intermediate step of plugging the placeholders with values), so I'm looking for an RTF-to-PDF converted (where the RTF starts as a string).
Is there such a thing?
Thanks.
LibreOffice in headless mode should provide file conversion to PDF.
You can integrate it in node with LibreOffice-Convert.
However you'll need LibreOffice in your machine, as it's not installed with npm. See here for the location according to your system.
This is how it's used in the command line, so it supports RTF to PDF conversion:
soffice --headless --convert-to pdf file_name.rtf
(source)
In theory it should be implemented like this (adapted from npmjs):
const libre = require('libreoffice-convert');
const path = require('path');
const fs = require('fs');
const enterPath = path.join(__dirname, '/resources/example.rtf');
const outputPath = path.join(__dirname, '/resources/example.pdf');
// Read file
const file = fs.readFileSync(enterPath);
// Convert it to pdf format with undefined filter (see Libreoffice doc about filter)
libre.convert(file, '.pdf', undefined, (err, done) => {
if (err) {
console.log(`Error converting file: ${err}`);
}
// Here in done you have pdf file which you can save or transfer in another stream
fs.writeFileSync(outputPath, done);
});

Special character issue in Excel for CSV file, Sheetjs prepend BOM for utf8 CSV files

I am using https://www.npmjs.com/package/xlsx library to create CSV.
This is the final code to write CSV code
let workbook: WorkBook = this.workBookCreator.getWorkbook(workBookName);
return this.xlsxModule.write(workbook, {
bookType: format,
type: "buffer"
});
And that will be uploaded to S3 bucket.
To download file
window.location.href = fileLocation;
Where fileLocation is the S3 pre-signed URL with a GET request.
When the downloaded file opens in Microsoft Excel, Special characters are messed up due to UTF-8 charset are not being recognized by EXCEL.
Can anyone please help me, how can I set charset UTF-8 while creating CSV to support all special characters to Excel?
I have tried the below solutions so far:
Tried to change the buffer type of XLSX
Added IconV & IconV-lite for buffer conversion
Changed the Content-type of the S3 bucket file.
Tried to change buffer type to string
I know we can prepend BOM to CSV file while writing. But couldn't find from where I can prepend.
So, Finally, I found the solution (Tried too many google search)
I used iconv-lite npm package
And here is code:
const txt = '\uFEFF'+ iconV.decode(CSVString, 'utf8');
CSVString = iconV.encode(txt, 'utf8');
Hope, I can save someone's day!

Reading an Excel file with fs.createReadStream

I am new to node js
I want to print excel data without using any library as using libraries takes time and we have to deal with large memory files.
I tried reading a .xlsx file using fs.createReadStream() and logged the data to console, it prints some different characters but the actual data.
nodejs code
const stream=fs.createReadStream('example.xlsx');
stream.on('data',function(data){
console.log(Buffer.from(data, 'base64').toString('utf8'))
})
Can I know how to get the actual data or any library that can read large excel files?

Trying to convert xlsx to csv using a node stream

I have a lambda script that retrieves an email from s3, parses it with MailParser (streaming), transforms attachments to csv if necessary, and stores them in a different bucket. The script handles csv files (no conversion) and zip files, but I can't figure out how to convert xls to csv using streams.
Exceljs looks really good for this, but I can't get it to work for some reason (and I'm really new to streams so that's probably it).
var workbook = new Excel.Workbook();
var xstream = workbook.xlsx.createInputStream();
xstream.on('done', function(data) {
// convert to csv and s3.upload
});
attachment.stream
.pipe(xstream);
I'm getting an error
Error: Unexpected xml node in parseOpen
before the 'done' event so I'm not sure if I'm using createInputStream correctly.

Resources