PDF Document Conversion for Node.js

Export PDF to Microsoft Office® Word, Excel, PowerPoint Presentations, Images, EPUB and fixed-layout formats

Overview

Are you searching for a method to transform PDF files into other formats using server-side JavaScript? Aspose.PDF for Node.js via C++ is the perfect solution for converting PDF documents. This article will demonstrate how to convert PDF to text using JavaScript. PDF files can include various content, such as text, images, clickable buttons, hyperlinks, embedded fonts, signatures, and stamps. When converting a PDF file to another format, users often want to be able to edit the PDF content. With Aspose.PDF for JavaScript via C++, you can easily and quickly convert your PDF documents to and from the most popular formats. Our library ensures that your PDF files are converted accurately and successfully.

PDF to DOCX Conversion

Example: JavaScript Code for PDF to DOC Conversion

const AsposePdf = require('../AsposePDFforNode.js');
const pdf_file = '../ReadMe.pdf';
AsposePdf().then(AsposePdfModule => {
    /*Convert a PDF-file to DocX and save the "ResultPDFtoDocX.docx"*/
    const json = AsposePdfModule.AsposePdfToDocX(pdf_file, "./ResultPDFtoDocX.docx");
    console.log("AsposePdfToDocX => %O", json.errorCode == 0 ? json.fileNameResult : json.errorText);
});

Aspose.PDF for Node.js supports PDF to DOCX conversion directly on the Web. First, we create ‘FileReader’. Then, we call the AsposePdfToDoc function. The web worker is set up to listen for messages using the onmessage event handler. When it receives a message from the web page, it processes the request and sends a response back to the main thread. The resultant file we can become by Download File Function. The DownloadFile function is responsible for generating a link that allows the user to download a file. Next, if the ‘json.errorCode’ is 0, then json.fileNameResult will contain the name you specified earlier. If the ‘json.errorCode’ parameter is not equal to 0 and, accordingly, there will be an error in your file, then information about such an error will be contained in the ‘json.errorText’ property. The name of the resulting file is ‘ResultPDFtoDoc.doc’.

PDF to XSLX Conversion

PDF to XSLX Conversion

const AsposePdf = require('../AsposePDFforNode.js');
const pdf_file = '../ReadMe.pdf';
AsposePdf().then(AsposePdfModule => {
    /*Convert a PDF-file to XlsX and save the "ResultPDFtoXlsX.xlsx"*/
    const json = AsposePdfModule.AsposePdfToXlsX(pdf_file, "./ResultPDFtoXlsX.xlsx");
    console.log("AsposePdfToXlsX => %O", json.errorCode == 0 ? json.fileNameResult : json.errorText);
});

This code is a JavaScript function that converts a PDF file to an Excel file using the Aspose.PDF. It takes an event object as a parameter, which contains the selected PDF file from a file input element. It creates a FileReader object and assigns a callback function to its onload event. The callback function is executed when the file is read as an array buffer. Next, call the AsposePdfToXlsX function. The function takes the file content, the original file name, and the output file name as arguments and returns a JSON object with the result. The JSON object has an errorCode property and an errorText property. If the errorCode is 0, it means that the conversion was successful and the JSON object also has a fileNameResult property, which contains the name of the output Excel file. If the errorCode is not 0, it means that there was an error. The name of the resulting file is ‘ResultPDFtoXlsX.xlsx’.

PDF to XPS Conversion

Example: JavaScript Code PDF to XPS Conversion

const AsposePdf = require('../AsposePDFforNode.js');
const pdf_file = '../ReadMe.pdf';
AsposePdf().then(AsposePdfModule => {
    /*Convert a PDF-file to Xps and save the "ResultPDFtoXps.xps"*/
    const json = AsposePdfModule.AsposePdfToXps(pdf_file, "./ResultPDFtoXps.xps");
    console.log("AsposePdfToXps => %O", json.errorCode == 0 ? json.fileNameResult : json.errorText);
});

This code is a JavaScript function that converts a PDF file to a XPS file using the Aspose.PDF. It takes an event object as a parameter, which contains the selected PDF file from a file input element. Next, creates a FileReader object and assigns a callback function to its onload event. The callback function is executed when the file is read as an array buffer.Call the AsposePdfToPptX function. The function takes the file content, the original file name, and the output file name as arguments and returns a JSON object with the result. The JSON object has an errorCode property and an errorText property. If the errorCode is 0, it means that the conversion was successful and the JSON object also has a fileNameResult property, which contains the name of the output PowerPoint file. If the errorCode is not 0, it means that there was an error and the errorText property contains the error message. As a result, the DownloadFile function creates a download link for the output PowerPoint file. The function takes the output file name ‘ResultPDFtoPptX.pptx’.

PDF to PNG Conversion

Example: JavaScript Code for PDF to PNG conversion

const AsposePdf = require('../AsposePDFforNode.js');
const pdf_file = '../ReadMe.pdf';
AsposePdf().then(AsposePdfModule => {
    /*Convert a PDF-file to PNG with template "ResultPdfToPng{0:D2}.png" ({0}, {0:D2}, {0:D3}, ... format page number), resolution 150 DPI and save*/
    const json = AsposePdfModule.AsposePdfPagesToPng(pdf_file, "./ResultPdfToPng{0:D2}.png", 150);
    console.log("AsposePdfPagesToPng => %O", json.errorCode == 0 ? json.filesNameResult : json.errorText);
});

This code is a JavaScript function that converts a PDF file to PNG images using the Aspose.PDF. It creates a FileReader object and assigns a callback function to its onload event. The callback function is executed when the file is read as an array buffer. Calls the AsposePdfPagesToPng function, which is a wrapper for the Aspose.PDF. The function takes the file content, the original file name, the output file name template, and the resolution as arguments and returns a JSON object with the result. The JSON object has an errorCode property and an errorText property. As a result, the DownloadFile function for each output file creates a download link for the PNG image. The function takes the output file name ‘ResultPdfToPng{0:D2}.png’.

PDF to PNG Conversion

Example: JavaScript Code for PDF to PNG conversion

const AsposePdf = require('../AsposePDFforNode.js');
const pdf_file = '../ReadMe.pdf';
AsposePdf().then(AsposePdfModule => {    
    const json = AsposePdfModule.AsposePdfPagesToPng(pdf_file, "./ResultPdfToPng{0:D2}.png", 150);
    console.log("AsposePdfPagesToPng => %O", json.errorCode == 0 ? json.filesNameResult : json.errorText);
});