Converting CSV files to plain TXT format is a frequent requirement when you need lightweight data extracts for logging or further processing. Aspose.PDF Cloud SDK for Node.JS provides a powerful library that handles file storage and format conversion in the cloud. In this guide you will see a step‑by‑step implementation that uploads a CSV, converts it to TXT, and saves the result locally using async/await. The example also demonstrates clean‑up of temporary files and basic error handling.
How to Convert CSV to TXT in Node.JS - Step by Step
- Install the library and import required modules: Add the package to your project and require the needed classes.
const fs = require('fs');
const path = require('path');
const { PdfApi } = require('asposepdfcloud');
- Initialize the PdfApi with your credentials: Create an instance of
PdfApiusing your client ID and secret.
const clientId = 'YOUR_CLIENT_ID';
const clientSecret = 'YOUR_CLIENT_SECRET';
const pdfApi = new PdfApi(clientId, clientSecret);
- Upload the CSV file to Aspose Cloud storage: Use
uploadFileto transfer the local CSV to the cloud.
const localCsvPath = path.resolve(__dirname, 'input.csv');
const remoteCsvPath = 'input.csv';
await pdfApi.uploadFile(remoteCsvPath, fs.createReadStream(localCsvPath));
- Convert the uploaded CSV to TXT: Call
getDocumentwith the target format'txt'.
const convertResponse = await pdfApi.getDocument(remoteCsvPath, null, 'txt');
- Save the TXT stream locally and clean up: Pipe the response body to a file and optionally delete the remote CSV.
const localTxtPath = path.resolve(__dirname, 'output.txt');
const writeStream = fs.createWriteStream(localTxtPath);
await new Promise((resolve, reject) => {
convertResponse.body.pipe(writeStream);
convertResponse.body.on('error', reject);
writeStream.on('finish', resolve);
writeStream.on('error', reject);
});
await pdfApi.deleteFile(remoteCsvPath); // optional cleanup
For more details on the PdfApi class, refer to the API reference.
Complete Code Example: CSV to TXT Conversion with Aspose.PDF
The following code demonstrates the full workflow from uploading a CSV file to retrieving the converted TXT file.
const fs = require('fs');
const path = require('path');
const { PdfApi } = require('asposepdfcloud');
// Replace with your actual Aspose PDF Cloud credentials
const clientId = 'YOUR_CLIENT_ID';
const clientSecret = 'YOUR_CLIENT_SECRET';
// Initialize the PdfApi instance
const pdfApi = new PdfApi(clientId, clientSecret);
// Paths (adjust as needed)
const localCsvPath = path.resolve(__dirname, 'input.csv');
const remoteCsvPath = 'input.csv'; // Path in Aspose Cloud storage
const localTxtPath = path.resolve(__dirname, 'output.txt');
async function convertCsvToTxt() {
try {
// 1. Upload the CSV file to Aspose Cloud storage
const uploadResponse = await pdfApi.uploadFile(remoteCsvPath, fs.createReadStream(localCsvPath));
if (uploadResponse.status !== 'OK') {
throw new Error(`Upload failed: ${JSON.stringify(uploadResponse)}`);
}
console.log('CSV file uploaded successfully.');
// 2. Convert the uploaded CSV to TXT format
const convertResponse = await pdfApi.getDocument(remoteCsvPath, null, 'txt');
if (!convertResponse.body) {
throw new Error('Conversion response does not contain a body stream.');
}
// 3. Save the converted TXT stream to a local file
const writeStream = fs.createWriteStream(localTxtPath);
await new Promise((resolve, reject) => {
convertResponse.body.pipe(writeStream);
convertResponse.body.on('error', reject);
writeStream.on('finish', resolve);
writeStream.on('error', reject);
});
console.log(`Conversion completed. TXT saved to ${localTxtPath}`);
// 4. (Optional) Clean up: delete the CSV file from cloud storage
const deleteResponse = await pdfApi.deleteFile(remoteCsvPath);
if (deleteResponse.status !== 'OK') {
console.warn(`Failed to delete remote CSV: ${JSON.stringify(deleteResponse)}`);
} else {
console.log('Remote CSV file deleted.');
}
} catch (error) {
console.error('An error occurred:', error.message);
}
}
// Execute the conversion
convertCsvToTxt();
Note: This code example demonstrates the core functionality. Before using it in your project, make sure to update any file paths and configuration values to match your actual environment, verify that all required dependencies are properly installed, and test thoroughly in your development environment. If you encounter any issues, please refer to the official documentation or reach out to the support team for assistance.
Convert CSV Files to TXT Using cURL
You can achieve the same conversion using the REST API directly. The steps below show how to obtain an access token, upload the CSV, request the conversion, and download the resulting TXT file.
- Get an access token
curl -X POST "https://api.aspose.cloud/connect/token" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=client_credentials&client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET"
- Upload the CSV file
curl -X PUT "https://api.aspose.cloud/v3.0/pdf/storage/file/input.csv" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: text/csv" \
--data-binary @input.csv
- Convert the uploaded CSV to TXT
curl -X GET "https://api.aspose.cloud/v3.0/pdf/input.csv?format=txt" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-o output.txt
- Delete the remote CSV file (optional)
curl -X DELETE "https://api.aspose.cloud/v3.0/pdf/storage/file/input.csv" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN"
For a complete list of parameters, see the official API documentation.
Installing and Configuring Aspose.PDF Cloud SDK for Node.JS
Add the library to your project with npm and ensure you have a valid Aspose Cloud account.
npm install asposepdfcloud
You can also download the package directly from the release page. The library requires Node.js 12 or higher and an active Aspose Cloud subscription.
Key Features of Aspose.PDF Cloud SDK for CSV to TXT Tasks
- Cloud storage integration - Files are stored in Aspose Cloud, eliminating local disk constraints.
- On‑the‑fly conversion - The
getDocumentmethod returns a stream, allowing immediate processing without intermediate files. - Async/await support - All operations return promises, fitting naturally into modern Node.JS codebases.
- Streaming I/O - Uploads and downloads are streamed, keeping memory usage low for large CSV files.
- Comprehensive format support - Besides TXT, the same API can convert to PDF, DOCX, HTML, and more.
Fine-Tuning Conversion Options for CSV to TXT
While the basic conversion requires only the target format, you can customize the request with additional parameters such as storage name or folder location. The getDocument method accepts a folder argument (set to null in the example) and a storage argument if you use a custom storage.
Example of specifying a storage name (code snippet already used in the main flow):
const convertResponse = await pdfApi.getDocument(remoteCsvPath, null, 'txt');
For a full list of optional parameters, refer to the API reference.
Conclusion
Converting CSV to TXT in Node.JS becomes straightforward with the Aspose.PDF Cloud SDK for Node.JS. The library handles authentication, cloud storage, and streaming conversion, letting you focus on business logic. Remember to review the pricing details on the product page and obtain a temporary license from the temporary license page for evaluation before moving to production. With the code and cURL examples in this guide, you can integrate CSV to TXT conversion into any Node.JS application quickly and reliably.
FAQs
How do I convert CSV to TXT in Node.JS using Aspose.PDF?
Use the library to upload the CSV, call getDocument with the 'txt' format, and stream the response to a local file. The full code example above shows each step.
What authentication method does the library use?
Provide your client ID and client secret; the library obtains an OAuth token automatically and includes it in each request.
Can the conversion handle large files efficiently?
Yes. Both upload and download are streamed, so the process works with large CSV files without loading the entire content into memory.
Where can I get a temporary license for testing?
A temporary license is available from the temporary license page.