JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 8338
  • Score
    100M100P100Q129585F
  • License MIT

Converts SVG to PNG using headless Chromium

Package Exports

  • convert-svg-to-png

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (convert-svg-to-png) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

convert-svg-to-png

A Node.js module for converting SVG to PNG using headless Chromium.

Build Status Dependency Status Dev Dependency Status License Release

Install

Install using npm:

$ npm install --save convert-svg-to-png

You'll need to have at least Node.js 8 or newer.

API

convert(source[, options])

Converts the specified source SVG into a PNG using the options provided via a headless Chromium instance.

source can either be a SVG buffer or string.

If the width and/or height cannot be derived from source then they must be provided via their corresponding options. This method attempts to derive the dimensions from source via any width/height attributes or its calculated viewBox attribute.

An error will occur if source does not contain an SVG element or no width and/or height options were provided and this information could not be derived from source.

Options

Option Type Default Description
baseFile String N/A Path of file to be converted into a file URL to use for all relative URLs contained within SVG. Overrides baseUrl option.
baseUrl String "file:///path/to/cwd" Base URL to use for all relative URLs contained within SVG. Overridden by baseUrl option.
height Number/String N/A Height of the PNG to be generated. Derived from SVG source if omitted.
width Number/String N/A Width of the PNG to be generated. Derived from SVG source if omitted.

Example

const { convert } = require('convert-svg-to-png');
const fs = require('fs');
const path = require('path');
const util = require('util');

const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);

async function convertSvgFile(filePath) {
  const dirPath = path.dirname(filePath);
  const input = await readFile(filePath);
  const output = await convert(input, { baseFile: dirPath });

  await writeFile(path.join(dirPath, `${path.basename(filePath, '.svg')}.png`), output);
}

createConverter()

Creates an instance of Converter.

It is important to note that, after the first time Converter#convert is called, a headless Chromium instance will remain open until Converter#destroy is called. This is done automatically when using the main API convert method, however, when using Converter directly, it is the responsibility of the caller. Due to the fact that creating browser instances is expensive, this level of control allows callers to reuse a browser for multiple conversions. It's not recommended to keep an instance around for too long, as it will use up resources.

Example

const { createConverter } = require('convert-svg-to-png');
const fs = require('fs');
const path = require('path');
const util = require('util');

const readdir = util.promisify(fs.readdir);
const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);

async function convertSvgFiles(dirPath) {
  const converter = createConverter();

  try {
    const filePaths = await readdir(dirPath);

    for (const filePath of filePaths) {
      const extension = path.extname(filePath);
      if (extension !== '.svg') {
        continue;
      }

      const input = await readFile(path.join(dirPath, filePath));
      const output = await converter.convert(input, { baseFile: dirPath });

      await writeFile(path.join(dirPath, `${path.basename(filePath, extension)}.png`), output);
    }
  } finally {
    await converter.destroy();
  }
}

version

The current version of this library.

Example

const { version } = require('convert-svg-to-png');

version;
//=> "0.1.0"

Bugs

If you have any problems with this library or would like to see changes currently in development you can do so here.

Contributors

If you want to contribute, you're a legend! Information on how you can do so can be found in CONTRIBUTING.md. We want your suggestions and pull requests!

A list of all contributors can be found in AUTHORS.md.

License

See LICENSE.md for more information on our MIT license.

Copyright !ninja