How to Convert Microsoft PowerPoint to PDF with JavaScript in NodeJS
Why Convert PowerPoint to PDF with JavaScript?
The pdfRest Convert to PDF API Tool offers a seamless way to convert various file formats to PDF using simple API calls. This tutorial will demonstrate how to send an API call to Convert to PDF with JavaScript, which is particularly useful for web developers and anyone working in a JavaScript environment. By integrating this API, developers can automate the process of converting documents to PDF format, ensuring consistency and efficiency in document handling within their applications.
Scenarios where Convert to PDF can be invaluable include generating presentations for wider distribution, archiving internal training materials, or creating client proposals with a polished look. For instance, a marketing team could use this API to convert their dynamic PowerPoint presentations into static PDFs before sending them to potential clients. This ensures the presentations maintain their formatting and visual elements across different devices, while also allowing for easy sharing and archiving.
Convert PowerPoint to PDF with JavaScript Code Example
var axios = require('axios'); var FormData = require('form-data'); var fs = require('fs'); // Create a new form data instance and append the PDF file and parameters to it var data = new FormData(); data.append('file', fs.createReadStream('/path/to/file.pptx')); data.append('compression', 'lossy'); data.append('downsample', '300'); data.append('tagged_pdf', 'off'); data.append('output', 'pdfrest_pdf'); // define configuration options for axios request var config = { method: 'post', maxBodyLength: Infinity, // set maximum length of the request body url: 'https://api.pdfrest.com/pdf', headers: { 'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', // Replace with your API key ...data.getHeaders() // set headers for the request }, data : data // set the data to be sent with the request }; // send request and handle response or error axios(config) .then(function (response) { console.log(JSON.stringify(response.data)); }) .catch(function (error) { console.log(error); }); // If you would like to download the file instead of getting the JSON response, please see the 'get-resource-id-endpoint.js' sample.
Source code reference: GitHub - pdf-rest-api-samples
Breaking Down the Code
The code above uses the Axios library to perform an HTTP POST request to the pdfRest API. The FormData library is used to create a multipart/form-data payload, which is necessary for file uploads. Here's a breakdown of the key components:
var data = new FormData(); data.append('file', fs.createReadStream('/path/to/file.pptx')); data.append('compression', 'lossy'); data.append('downsample', '300'); data.append('tagged_pdf', 'off'); data.append('output', 'pdfrest_pdf');
In this snippet, a new FormData object is created, and several fields are appended to it:
'file'
: This is the file input stream for the document you want to convert to PDF.'compression'
: Specifies the type of compression to apply. 'lossy' allows for smaller file size at the potential cost of quality.'downsample'
: Sets the resolution for images within the PDF. '300' is a common DPI value for high-quality prints.'tagged_pdf'
: Determines whether to create a tagged PDF ('on') or not ('off'). Tagged PDFs support accessibility features.'output'
: The name of the output parameter, 'pdfrest_pdf' in this case.
These parameters are further explained in the pdfRest Cloud API Reference Guide.
var config = { method: 'post', maxBodyLength: Infinity, url: 'https://api.pdfrest.com/pdf', headers: { 'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', ...data.getHeaders() }, data : data };
The config
object sets up the request details, including the API endpoint URL, the headers (which include the API key for authentication), and the data payload. The spread operator (...
) is used to include the headers from the FormData object.
Finally, the Axios instance sends the request and handles the response with a promise, logging the result or any errors encountered.
Beyond the Tutorial
By following this tutorial, you've learned how to make a multipart API call to Convert to PDF using JavaScript. This allows you to integrate PDF conversion capabilities directly into your JavaScript applications, enhancing their functionality and user experience.
To explore the full capabilities of pdfRest, you can demo all of the pdfRest API Tools in the API Lab and refer to the API Reference Guide for more detailed information on the available endpoints and parameters.
Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at GitHub - JSON payload examples.