您的位置:首页 > 大数据 > 人工智能

CairoSVG - Convert SVG to PNG or PDF - Contents

2014-01-04 19:23 567 查看
CairoSVG - Convert SVG to PNG or PDF - Contents

User Documentation

AuthorGuillaume Ayoub
Date2011-02-13
This document is a short description for installing and using CairoSVG.

Edit this pageInstallationDependencies
CairoSVG

Command-Line UsageDescription
Examples

API

Installation

Dependencies

CairoSVG is written in pure Python and only depends on Pycairo. It is known to work with Python 2.6, 2.7, 3.2 and 3.3.

Linux and OS X users certainly have Python already installed. For Windows users, please install Python thanks to the adequate installer.

CairoSVG can use lxml to parse the SVG file, and tinycss plus cssselect to apply CSS not included in thestyleattribute of the tags. If these packages are not available, CSS will only be supported in thestyleattributes.

Embedded raster images other than PNG are handled by Pillow.

CairoSVG

CairoSVG can be freely downloaded on the project website, download section. Just get the file and unzip it in a folder of your choice.

Command-Line Usage

Description

Here is the simple CairoSVG usage:

Usage: cairosvg.py filename [options]

Options:
-h, --help            show this help message and exit
-v, --version         show version and exit
-f FORMAT, --format=FORMAT
output format
-d DPI, --dpi=DPI     ratio between 1in and 1px
-o OUTPUT, --output=OUTPUT
output filename
Supported output formats arepdf,psandpng(default ispdf). The default output is the standard output. If an output filename is given, the format is automatically chosen according to the extension.

Thedpioptions sets the ratio between pixels and real-life units such as millimeters and inches (as explained in the specification).

Moreover, if-is used as filename, CairoSVG reads the SVG string from the standard input.

Examples

Here are some usage examples:

# Convert to pdf, write to standard output
cairosvg test.svg

# Convert to png, write to standard output
cairosvg test.svg -f png

# Convert to ps, write to test.ps
cairosvg test.svg -o test.ps

# Convert an SVG string to pdf, write to standard output
echo "<svg height='30' width='30'><text y='10'>123</text></svg>" \
| cairosvg -

API

Thecairosvgmodule offers 4 functions:

svg2pdf,
svg2png,
svg2ps, and
svg2svg(!).
These functions expect one of these parameters:

bytestring, a byte string containing SVG, or
url, an URL or a filename, or
file_obj, a file-like object.
If thewrite_toargument is provided (filename or file-like object), the output is written there. Otherwise, the function returns a byte string.

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: