Javascript excel to pdf

How to Convert Excel to PDF Using JavaScript

Vyom Srivastava

Illustration: How to Convert Excel to PDF Using JavaScript

In this post, you’ll learn how to convert Excel files to PDFs in your JavaScript application using PSPDFKit’s XLSX to PDF JavaScript API. With our API, you can convert up to 100 PDF files per month for free. All you need to do is create a free account to get access to your API key.

It’s now possible to view and edit Word, Excel, and PowerPoint documents directly in the browser, without any server-side processing required.

PSPDFKit API

Document conversion is just one of our 30+ PDF API tools. You can combine our conversion tool with other tools to create complex document processing workflows. You’ll be able to convert various file formats into PDFs and then:

  • Merge several resulting PDFs into one
  • OCR, watermark, or flatten PDFs
  • Remove or duplicate specific PDF pages
Читайте также:  Пример телеграмм бота python

Once you create your account, you’ll be able to access all our PDF API tools.

Step 1 — Creating a Free Account on PSPDFKit

Go to our website, where you’ll see the page below, prompting you to create your free account.

Free account PSPDFKit API

Once you’ve created your account, you’ll be welcomed by the page below, which shows an overview of your plan details.

Free plan PSPDFKit API

As you can see in the bottom-left corner, you’ll start with 100 documents to process, and you’ll be able to access all our PDF API tools.

Step 2 — Obtaining the API Key

After you’ve verified your email, you can get your API key from the dashboard. In the menu on the left, click API Keys. You’ll see the following page, which is an overview of your keys:

Convert Excel to PDF JavaScript API Key

Copy the Live API Key, because you’ll need this for the Excel to PDF API.

Step 3 — Setting Up Folders and Files

Now, create a folder called excel_to_pdf and open it in a code editor. For this tutorial, you’ll use VS Code as your primary code editor. Next, create two folders inside excel_to_pdf and name them input_documents and processed_documents .

Next, copy your Excel file to the input_documents folder and rename it to document.xlsx . You can use our demo document as an example.

Then, in the root folder, excel_to_pdf , create a file called processor.js . This is the file where you’ll keep your code.

Your folder structure will look like this:

excel_to_pdf ├── input_documents | └── document.xlsx ├── processed_documents └── processor.js

Step 4 — Installing Dependencies

To get started converting PDF pages, you first need to install the following dependencies:

  • axios — This package is used for making REST API calls.
  • Form-Data — This package is used for creating form data.

Use the command below to install both of them:

npm install axios npm install form-data

Step 5 — Writing the Code

Now, open the processor.js file and paste the code below into it:

const axios = require('axios'); const FormData = require('form-data'); const fs = require('fs'); const formData = new FormData(); formData.append( 'instructions', JSON.stringify(< parts: [ < file: 'document', >, ], >), ); formData.append( 'document', fs.createReadStream('input_documents/document.xlsx'), )(async () => < try < const response = await axios.post( 'https://api.pspdfkit.com/build', formData, < headers: formData.getHeaders(< Authorization: 'Bearer YOUR API KEY HERE', >), responseType: 'stream', >, ); response.data.pipe( fs.createWriteStream('processed_documents/result.pdf'), ); > catch (e) < const errorString = await streamToString(e.response.data); console.log(errorString); > >)(); function streamToString(stream) < const chunks = []; return new Promise((resolve, reject) => < stream.on('data', (chunk) => chunks.push(Buffer.from(chunk))); stream.on('error', (err) => reject(err)); stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')), ); >); >

Make sure to replace YOUR_API_KEY_HERE with your API key.

Code Explanation

Here, you have a form variable that contains the instructions for the API. You’re using createReadStream to read the input PDF file. You then have a function, axios.post() , that’ll make the POST request to the Excel to PDF API.

The response of the API is stored in the processed_documents folder.

Output

To execute the code, use the following command:

On the successful execution of the code, you’ll see a new processed file named result.pdf in the processed_documents folder.

The folder structure will look like this:

excel_to_pdf ├── input_documents | └── document.xlsx ├── processed_documents | └── result.pdf └── processor.js

Final Words

In this post, you learned how to easily and seamlessly convert Excel files to PDF documents for your JavaScript application using our Excel to PDF JavaScript API.

You can integrate these functions into your existing applications. With the same API token, you can also perform other operations, such as merging several documents into a single PDF, adding watermarks, and more. To get started with a free trial, sign up here.

Источник

Convert XLS to PDF using Node.js

Excel to PDF

Excel is one of the popular file formats for computation data storage. It is capable of storing data like numbers and formulas, text, and drawing shapes. Furthermore, Excel is part of the Microsoft Office Suite of software, so many users use it to produce the XLS files. Furthermore, owing to its capabilities for storing data in a grid of cells arranged in numbered rows and letter-named columns to organize data manipulations like arithmetic operations, it has become very popular. But in order to view these files, you need to have a specific application installed. Also, various versions of software display the files in different formattings. Therefore, in order to resolve all these issues, the conversion of the file to PDF format is a viable solution because the majority of web browsers on desktops and various free applications on mobile devices conveniently display PDF documents.

In this article, we are going to discuss the conversion of Excel to PDF conversion using Cloud REST API.

Excel to PDF Conversion API#

Aspose.Cells Cloud is our award-winning REST API offering the capabilities to create excel files from scratch, edit the content in the existing files and, render the file to formats such as XLSM, HTML, XPS, TIFF, SVG, and much more. So as per the scope of this article, we are going to use Aspose.Cells Cloud SDK for Node.js.

Convert Excel to PDF using cURL Commands#

In this section, we are going to use the cURL Commands to save Excel to PDF format. So the first step is to create a Free account subscription on Aspose.Cloud dashboard. If you have GitHub or Google account, simply Sign Up. Otherwise, click on the Create a new Account button and provide the required information. Login to the dashboard to access your Client Credentials.

Now please execute the following command to generate the JWT access token.

curl -v "https://api.aspose.cloud/connect/token" \ -X POST \ -d "grant_type=client_credentials&client_id=718e4235-8866-4ebe-bff4-f5a14a4b6466&client_secret=388e864b819d8b067a8b1cb625a2ea8e" \ -H "Content-Type: application/x-www-form-urlencoded" \ -H "Accept: application/json" 

Once we have the JWT token, we need to execute the following cURL command to upload a file from the local system to Cloud storage and then perform the conversion to PDF format.

curl -X PUT "https://api.aspose.cloud/v3.0/cells/storage/file/conditional.xls" \ -H "accept: application/json" -H "authorization: Bearer " \ -H "Content-Type: multipart/form-data" \ -d > 
https://api.aspose.cloud/v3.0/cells/storage/file/conditional.xlsx 

In case you need to perform the conversion of an XLS file already available in Cloud storage to PDF format, please try using the following cURL command:

curl -X GET "https://api.aspose.cloud/v3.0/cells/conditional.xls?format=PDF&isAutoFit=true&onlySaveTable=false&outPath=output.pdf" \ -H "accept: application/json" \ -H "authorization: Bearer " 

Convert XLS to PDF in Node.js#

In this section, we are going to discuss the steps and details on how to perform the Excel worksheet conversion to PDF format using Node.js SDK. The first step is to install the SDK on the local system which is available for download at NPM and GitHub. So we are going to run the following command to install the SDK from NPM

After the installation, we can execute the following code snippet to perform XLS to PDF conversion using Node.js

  • Create objects defining Client ID and Client Secret details
  • Then create an object of CellsApi class that takes Client ID and Client Secret details as arguments
  • The next step is to read the contents of the input XLS file available on the local system using the createReadStream(…) method of the File System Module
  • Now create an instance of UploadFileRequest class and then pass input Stream data to the file property
  • In order to upload the file to cloud storage, please call uploadFile(…) method of CellsApi
  • The next step is to create an object of CellsSaveAs_PostDocumentSaveAsRequest(…) class
  • Furthermore, create an instance of PdfSaveOptions class and pass the value of PDF to saveFormat property
  • Finally, call the cellsSaveAsPostDocumentSaveAs(..) method of CellsApi class to initiate the conversion process

Conclusion#

In this article, we have discussed the details regarding the conversion of Excel to PDF format in a simple and convenient manner. Please note that Aspose.Cells Cloud SDK for Node.js is developed according to open-source principles, so the complete source code is available for download at GitHub. In case you encounter any issue while using the API or you have any related queries, please do not hesitate to ask via the Free product support forum.

You may consider visiting the following links to learn more about

Источник

Convert Excel Files to PDF in Node.js

Convert Excel to PDF in Node.js

Excel spreadsheets are commonly used to store and analyze data in the form of rows and columns. However, in various cases, you need to convert the Excel files to PDF before sharing them over the internet. In order to automate Excel to PDF conversion, this article shows how to convert Excel files to PDF programmatically using Node.js. Furthermore, advanced options to customize the Excel to PDF conversion are also discussed.

Node.js Excel to PDF Conversion API#

For converting the Excel XLSX/XLS files to PDF, we will use Aspose.Cells for Node.js via Java. It is a spreadsheet manipulation API that lets you create, read, modify, and convert Excel workbooks seamlessly. You can either download the API’s package or install it use the following NPM command.

Convert Excel XLSX to PDF in Node.js#

The following are the steps to convert Excel XLSX or XLS files to PDF.

The following code sample shows how to convert an Excel XLSX file to PDF.

Excel File#

Node.js Excel to PDF

Converted PDF#

Excel to PDF

Excel XLSX/XLS to PDF — Advanced Options#

Aspose.Cells also allows you to control the Excel to PDF conversion using different options. For example, you can set PDF compliance, compression, gridlines style, number of pages per sheet, etc. To customize the conversion, the API provides PdfSaveOptions class.

The following are the steps to convert an Excel file to PDF with advanced options.

  • Load the Excel file using Workbook class.
  • Create an object of PdfSaveOptions class.
  • Set the desired options e.g. PDF compliance using PdfOptions.setCompliance(PdfCompliance) method.
  • Save Excel file as PDF using Workbook.save(string, PdfSaveOptions) method.

The following code sample shows how to convert Excel to PDF using advanced options.

Get a Free API License#

In order to use the API without evaluation limitations, you can request a free temporary license.

Conclusion#

In this article, you have learned how to convert Excel files to PDF within Node.js applications. Furthermore, you have seen how to control Excel to PDF conversion using advanced options. You can explore more about the Node.js Excel API using the documentation. Furthermore, you can contact us via our forum in case of any query.

See Also#

Источник

Оцените статью