Convert Office Word & PowerPoint to PDF via Free Node.js Library
Open Source Node.js PDF Converter API for Reading and Converting Office Word and PowerPoint Files to PDF with Batch Document Conversion Support.
What is Files2PDF?
In the world of document management, converting files to PDF is a daily necessity. Whether you are archiving old presentations or finalizing contracts, the need for a stable, non-editable format is universal. However, doing this manually for hundreds of files is a nightmare. Enter Files2Pdf—a lightweight, powerful Node.js package designed to automate this very process. There are several important features part of the library, such as bulk/batch document processing, working with multiple documents format, dual usage modes, smart error handling, directory automation, and so on.
Files2Pdf is a lightweight Node.js package that simplifies the conversion of office documents—specifically Microsoft Word and PowerPoint files—into PDF format. Unlike complex enterprise solutions that require heavy configurations, Files2Pdf focuses on doing one thing exceptionally well: processing entire folders of documents and outputting clean, ready-to-share PDFs. The library automatically identifies supported files in your directory and skips over incompatible ones, preventing crashes or errors during batch jobs.
Getting Started with Files2Pdf
The best way to install Files2PDF module is via npm, please use the following command to install the API.
Install Files2PDF Library via npm
npm install files2pdfInstall Files2PDF Library via GibHub
git clone "https://github.com/An4s0/Files2Pdf.git" Batch Processing Capability
The open source Files2PDF library has included various features for converting multiple files to PDF documents at the same time. One of the standout features is its ability to process entire folders of documents in a single operation. Instead of converting files one by one, you can point Files2PDF to a directory containing multiple documents, and it will systematically convert each supported file to PDF format.
How to Convert Multiple Documents to PDF Files inside Node.js Apps?
const { processFolder } = require('files2pdf');
const path = require('path');
const fs = require('fs').promises;
async function convertDocuments(inputPath, outputPath) {
try {
// Verify input folder exists
await fs.access(inputPath);
console.log(`Starting conversion process...`);
console.log(`Input folder: ${inputPath}`);
console.log(`Output folder: ${outputPath}`);
// Process all files in the folder
await processFolder(inputPath, outputPath);
console.log('Conversion completed successfully!');
return { success: true, message: 'All documents converted' };
} catch (error) {
console.error('Conversion failed:', error.message);
return { success: false, error: error.message };
}
}
// Usage example
const inputDir = path.resolve(__dirname, 'documents');
const outputDir = path.resolve(__dirname, 'pdfs');
convertDocuments(inputDir, outputDir)
.then(result => {
if (result.success) {
console.log('Success:', result.message);
} else {
console.log('Failed:', result.error);
}
});
Multi-Format Support
Files2PDF handles both legacy and modern Microsoft Office formats seamlessly. The package supports PowerPoint files in both .ppt and .pptx formats, along with Word documents in .doc and .docx formats. This comprehensive format support ensures compatibility with documents created across different versions of Microsoft Office. The following code example shows how a file monitoring system can be applied to create an automated document conversion system that monitors a directory for new files and converts them automatically.
How to Create a File Monitoring System for Converting New Files Loaded to a Directory in Node.js?
const { processFolder } = require('files2pdf');
const chokidar = require('chokidar');
const path = require('path');
const fs = require('fs').promises;
class DocumentConverter {
constructor(watchDir, outputDir) {
this.watchDir = path.resolve(watchDir);
this.outputDir = path.resolve(outputDir);
this.processing = new Set();
}
async initialize() {
// Ensure output directory exists
await fs.mkdir(this.outputDir, { recursive: true });
// Set up file watcher
const watcher = chokidar.watch(this.watchDir, {
ignored: /(^|[\/\\])\../, // ignore dotfiles
persistent: true,
ignoreInitial: false
});
watcher.on('add', async (filePath) => {
await this.handleNewFile(filePath);
});
console.log(`Watching ${this.watchDir} for new documents...`);
}
async handleNewFile(filePath) {
const ext = path.extname(filePath).toLowerCase();
const supportedFormats = ['.ppt', '.pptx', '.doc', '.docx'];
if (!supportedFormats.includes(ext)) {
return; // Skip unsupported files
}
if (this.processing.has(filePath)) {
return; // Already processing this file
}
this.processing.add(filePath);
console.log(`New file detected: ${path.basename(filePath)}`);
try {
await processFolder(path.dirname(filePath), this.outputDir);
console.log(`Successfully converted: ${path.basename(filePath)}`);
} catch (error) {
console.error(`Error converting ${path.basename(filePath)}:`, error.message);
} finally {
this.processing.delete(filePath);
}
}
}
// Initialize the converter
const converter = new DocumentConverter('./watched-folder', './converted-pdfs');
converter.initialize();
Customizable Page Size and Orientation
A one-size-fits-all approach doesn't work for PDFs. A screenshot might be best in landscape, while a text document is better in portrait. Files2PDF allows you to define the page size (e.g., A4, Letter) and orientation (Portrait or Landscape) for the entire output PDF, ensuring your content is displayed correctly.
Intelligent File Filtering
The open source Files2PDF library makes it easy for software developers to automatically identifies and processes only supported file types, gracefully skipping over unsupported formats without throwing errors. This intelligent filtering mechanism ensures smooth operation even when working with folders containing mixed file types.