Miss any of our Open RFC calls?Watch the recordings here! »

convertapi

1.4.0 • Public • Published

ConvertAPI Node.js Client

npm version Build Status License: MIT

Convert your files with our online file conversion API

The ConvertAPI helps converting various file formats. Creating PDF and Images from various sources like Word, Excel, Powerpoint, images, web pages or raw HTML codes. Merge, Encrypt, Split, Repair and Decrypt PDF files. And many others files manipulations. In just few minutes you can integrate it into your application and use it easily.

Requirements

Node.js v6.0 and later.

Installation

npm install convertapi --save

Usage

The package needs to be configured with your account's secret key which is available at https://www.convertapi.com/a

Require it with the key's value:

var convertapi = require('convertapi')('your-api-secret');

Or using ES modules:

import convertapiPackage from 'convertapi';
 
const convertapi = convertapiPackage('your-api-secret');

You can specify aditional options, like proxy configuration and timeouts, when initializing the client:

var convertapi = require('convertapi')('your-api-secret', {
  conversionTimeout: 60,
  uploadTimeout: 60,
  downloadTimeout: 60,
  proxy: {
    host: '127.0.0.1',
    port: 9000,
    auth: {
      username: 'testuser',
      password: 'secret'
    }
  }
});

File conversion

Example to convert file to PDF. All supported formats and options can be found here.

convertapi.convert('pdf', { File: '/path/to/my_file.docx' })
  .then(function(result) {
    // get converted file url
    console.log("Converted file url: " + result.file.url);
 
    // save to file
    return result.file.save('/path/to/save/file.pdf');
  })
  .then(function(file) {
    console.log("File saved: " + file);
  })
  .catch(function(e) {
    console.error(e.toString());
  });

Other result operations:

// save all result files to folder
result.saveFiles('/path/to/save/files').then(function(files) {
  console.log("Files saved: " + files);
});
 
// get conversion cost
console.log("Conversion cost: " + result.conversionCost);

Convert file url

var resultPromise = convertapi.convert('pdf', { File: 'https://website/my_file.docx' });

Specifying from format

var resultPromise = convertapi.convert('pdf', { File: 'https://website/my_file' }, 'docx');

Additional conversion parameters

ConvertAPI accepts extra conversion parameters depending on converted formats. All conversion parameters and explanations can be found here.

var resultPromise = convertapi.convert(
  'pdf',
  {
    File: '/path/to/my_file.docx',
    PageRange: '1-10',
    PdfResolution: '150',
  }
);

User information

You can always check remaining seconds amount by fetching user information.

convertapi.getUser().then(function(info) {
  console.log("Seconds left: " + info.SecondsLeft);
});

Error handling

convertapi
  .convert('pdf', { File: 'https://website/my_file.docx' })
  .catch(function(e) {
    console.error(e.toString());
    console.debug(e.request);
    console.debug(e.response);
  });

More examples

You can find more advanced examples in the examples/ folder.

Development Commands

  • npm run clean - Remove lib/ directory
  • npm test - Run tests with linting and coverage results.
  • npm run test:only - Run tests without linting or coverage.
  • npm run test:watch - You can even re-run tests on file changes!
  • npm run test:prod - Run tests with minified code.
  • npm run test:examples - Test written examples on pure JS for better understanding module usage.
  • npm run lint - Run ESlint with airbnb-config
  • npm run cover - Get coverage report for your code.
  • npm run build - Babel will transpile ES6 => ES5 and minify the code.
  • npm run prepublish - Hook for npm. Do all the checks before publishing your module.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/ConvertAPI/convertapi-node. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

ConvertAPI Node.js Client is available as open source under the terms of the MIT License.

Install

npm i convertapi

DownloadsWeekly Downloads

550

Version

1.4.0

License

MIT

Unpacked Size

27.8 kB

Total Files

19

Last publish

Collaborators

  • avatar
  • avatar