Convert PDF File To Excel Using JavaScript
This tutorial help to convert PDF to excel file and store the data in an Excel data table using JavaScript.
The pdf-to-excel npm package help to convert pdf to editable excel file. My requirement is to generate both PDF and Excel at client side(browser) without doing it at server side.
Convert PDF to Excel Using JavaScript
Let’s create a folder conevrt-pdf-excel. Open command line and run the below command to install npm package:
npm i pdf-to-excel
Add a new file pdftoexcel.js
file into above project.
Let’s take a sample test.pdf
file as source that converted into excel file.
Now create an pdf-to-excel object:
const pdf2excel = require('pdf-to-excel');
We’ll define some configurations parameters:
const options = { // when current pdf page number changes call this function(optional) onProcess: (e) => console.warn(`${e.numPage} / ${e.numPages}`), // pdf start page number you want to convert (optional, default 1) start: 1, // pdf end page number you want to convert (optional, default ) end: 2, }
- onProcess : callback method for current pdf page number process.
- start : The page number where the converting process start.
- end : The page number where the converting process end.
Advertisements
We’ll use pdf2excel.genXlsx()
method to convert pdf to excel:
pdf2excel.genXlsx('test.pdf', 'test.xlsx', options);
This method takes three parameters:
- First parameter takes source pdf file.
- Second parameter takes excel file source name.
- Third parameters takes options.
JavaScript code to convert pdf to excel:
try { const options = { // when current pdf page number changes call this function(optional) onProcess: (e) => console.warn(`${e.numPage} / ${e.numPages}`), // pdf start page number you want to convert (optional, default 1) start: 1, // pdf end page number you want to convert (optional, default ) end: 2, } pdf2excel.genXlsx('test.pdf', 'test.xlsx', options); } catch (err) { console.error(err); }
How To Convert PDF to Excel Using Browser
We can use this library in browser as well. First, included js files into the head section of the file.
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.3.200/pdf.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.15.5/xlsx.full.min.js"></script> <script src="/lib/index.js"></script> <script> try { // bar.pdf in your static file server's root dir. pdf2excel.genXlsx('/test.pdf', 'test.xlsx'); } catch (err) { console.error(err); } </script> K
Originally published at https://www.js-tutorials.com on January 31, 2023.