Svg to png

API

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

can either be a SVG buffer or string.

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

This method is resolved with the PNG output buffer.

An error will occur if both the and options have been provided, does not contain an SVG
element or no and/or options were provided and this information could not be derived from .

Option Type Default Description
String N/A Background color to be used to fill transparent regions within the SVG. Will remain transparent if omitted.
String N/A Path of the file to be converted into a file URL to use for all relative URLs contained within the SVG. Cannot be used in conjunction with the option.
String Base URL to use for all relative URLs contained within the SVG. Cannot be used in conjunction with the option.
Number/String N/A Height of the output to be generated. Derived from SVG input if omitted.
Object N/A Options that are to be passed directly to when creating the instance.
Number Scale to be applied to the width and height (specified as options or derived).
Number/String N/A Width of the output to be generated. Derived from SVG input if omitted.

The option is not available when calling this method on a instance created using
.

const{convert}=require('convert-svg-to-png');constexpress=require('express');constapp=express();app.post('/convert',async(req, res)=>{constpng=awaitconvert(req.body);res.set('Content-Type','image/png');res.send(png);});app.listen(3000);

Converts the SVG file at the specified path into a PNG using the provided and writes it to the output file.

The output file is derived from unless the option is specified.

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

This method is resolved with the path of the PNG output file for reference.

An error will occur if both the and options have been provided, the input file does not contain an
SVG element, no and/or options were provided and this information could not be derived from input file,
or a problem arises while reading the input file or writing the output file.

Has the same options as the standard method but also supports the following additional options:

Option Type Default Description
String N/A Path of the file to which the PNG output should be written to. Derived from input file path if omitted.
const{convertFile}=require('convert-svg-to-png');(async()=>{constinputFilePath='/path/to/my-image.svg';constoutputFilePath=awaitconvertFile(inputFilePath);console.log(outputFilePath);})();

Creates an instance of using the provided.

It is important to note that, after the first time either or are called, a
headless Chromium instance will remain open until is called. This is done automatically when using
the convert methods, however, when using 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.

Option Type Default Description
Object N/A Options that are to be passed directly to when creating the instance.
const{createConverter}=require('convert-svg-to-png');constfs=require('fs');constutil=require('util');constreaddir=util.promisify(fs.readdir);asyncfunctionconvertSvgFiles(dirPath){constconverter=createConverter();try{constfilePaths=awaitreaddir(dirPath);for(constfilePathof filePaths){awaitconverter.convertFile(filePath);}}finally{awaitconverter.destroy();}}
Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *

Adblock
detector