Convert the an Excel generated file to PDF in React - javascript

I want to convert the excel file into pdf. Likewise, I had previously generated the excel file with excelJs and save it on pc with file-saver plugin.
Now I just want to convert that same excel file blob into the Pdf file and then save to the pdf.
Here is my code snippet for saving the excel file.
const buf = await workbook.xlsx.writeBuffer();
const data = new Blob([buf], { type: fileType })
FileSaver.saveAs(data, 'User Listing Report' + fileExtension);

i think you need to use a combination of the following JavaScript libraries there's this project https://github.com/react-csv/react-csv
but i think in the server there is some problems with this method
i hope i help u !!!

Related

Exported HTML table to excel giving warning while opening the file using office Excel

I am trying to export HTML table to xls format. But when I try to open the file using Excel, I am given a warning message. The waring message:
I using this lines of code:
var file = new Blob([ html.outerHTML], {
type: "application/vnd.ms-excel"
});
var url = URL.createObjectURL(file);
var filename = dateSelected + "-" + "attendance" + ".xls"
//here we are creating HTML <a> Tag which can be trigger to download the excel file.
var a = document.createElement('a');
a.id = "export";
document.body.appendChild(a);
//here we are checking if the bwoswer is IE or not if IE then we use window.navigator.msSaveBlob function otherwise Go with Simple Blob file.
if (window.navigator && window.navigator.msSaveBlob) {
window.navigator.msSaveBlob(file, filename);
a.click();
document.body.removeChild(a);
document.body.removeChild(html);
} else {
a.download = filename;
a.href = url;
a.click();
document.body.removeChild(a);
document.body.removeChild(html);
}
I tried to change the blob type but nothing is working. How can i solve this issue?
The Problem
If you open the file in notepad or other text editor, you will find that what is saved in the file is html or xml. However, open any XLS file generated by Excel, and you will see unreadable binary characters.
Since Excel can open xml files, you don't feel that there's something wrong under the hood. However the *.xls format is binary in nature and is different from xml. Excel would warn you if the file extension doesn't match the underlying file format. Google for excel extension hardening and you would know what I'm talking about.
The Solution
You need to save it in that (excel binary) format if you want excel to recognize it as XLS file. There are so many libraries available (some paid and some free) that can help you generate true XLS files. I would recommend you using one of those. The other way is to switch to another format like XLSX. The problem would remain same though. Unless excel is able to match the file extension with the file format, it will continue to show this warning. This is a security measure.
AFAIK there's probably a registry hack to get around this problem. But I won't recommend you that, because it will open you to the security problem for which this feature is there in Excel.

create and save excel file with nodejs

I am trying to create and save an excel file. I used this module
and followed their example. In my case I want to save the result locally. Somehow like this:
var fs = require('fs');
fs.writeFile(filename, result,"binary");
I cannot open the file via excel, because it's saying that the file is corrupted or broken. Looking again at the example of the module, I know that result is in a binary format.
What can I do to parse result correctly for saving the excel file?
Thanks in Advance.

Use Html5 to read Excel file

I need to read excel file data when upload the file .
Is there any way that can use html5 to read excel file data when file uploading in client side??
heard about
var reader = new FileReader();
any way that we can use this
Referring to #mituw16 comment on the question, take a look at the following question:
How to parse Excel file in Javascript/HTML5
As a seperate response to work from, I would suggest using a plugin like:
https://github.com/SheetJS/js-xlsx
To iterate through the spreadsheet and save the information in your database.

How can I download CSV data from variable to a csv file?

I am using papa parse library. It helped me in converting JSON to CSV but how can I download the same data into a excel file. My data is huge
If I'm understanding your question correctly, you have a CSV file which you now want to tell the browser to download?
Have you looked at https://github.com/eligrey/FileSaver.js/ ?
Allows you to tell the browser to download a wide variety of files.
An example in using it:
var blob = new Blob(myBigCSVFile, {type: "text/csv;charset=utf-8"});
saveAs(blob, "file.csv");
The browser, upon reaching the saveAs function will download the file you specified.
Checkout SheetJS/js-xlsx library.
Supported read formats:
Excel 2007+ XML Formats (XLSX/XLSM),
Excel 2007+ Binary Format (XLSB),
Excel 2003-2004 XML Format (XML "SpreadsheetML"),
Excel 97-2004 (XLS BIFF8),
Excel 5.0/95 (XLS BIFF5),
OpenDocument Spreadsheet (ODS)
Supported write formats:
XLSX, CSV (and general DSV), JSON and JS objects (various styles)

Warning when opening xls file which is exported from html table

I used the following code. I can download the file with extension .xls. When I open this downloaded file I receive this warning:
The file you are trying to open, 'Statement.xls', is in a different
format than specified by the file extension. Verify that the file is
not corrupted and is from a trusted source before opening the file. Do
you want to open the file now?
Javascript Code:
var blob = new Blob([document.getElementById('exportable').innerHTML], {
type: "application/vnd.ms-excel;" });
saveAs(blob, "Statement.xls");`
HTML code:
`<table id="exportable">
<tbody>
<tr><th>ColumnOne</th><th>ColumnTwo</th><th>ColumnThree</th></tr>
<tr><td>row1Col1</td><td>row1Col2</td><td>row1Col3</td></tr>
<tr><td>row2Col1</td><td>row2Col2</td><td>row2Col3</td></tr>
<tr><td>row3Col1</td><td>row3Col2</td><td>test</td></tr>
</tbody>
</table>`
External library:
https://rawgithub.com/eligrey/FileSaver.js/master/FileSaver.js
Office and Excel are able to cope with two different file formats. As those formats are totally different from the inside (the old one is binary, the new one is simply zipped up XML) different extensions and mimetypes were assigned to these formats.
Obviously you are creating a file with the new format and assign it the old extension. Excel complains about that.
Change your code to look like this:
var blob = new Blob([document.getElementById('exportable').innerHTML], {
type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" });
saveAs(blob, "Statement.xlsx");
For the sake of learning you might want to have a look at your created file with a hex editor.
Others had similar issues, have a look here: https://github.com/eligrey/FileSaver.js/issues/139
When saving file with *.xls extension, file type should be set to "application/xls"
Since file type and extension will be matching, you shouldn't get any warning.

Categories