CairoSVG - Convert SVG to PNG or PDF - Contents

本文介绍如何安装并使用CairoSVG,一个纯Python编写的库,用于将SVG文件转换为PNG或PDF。适用于Linux、OS X和Windows系统,支持嵌入式矢量图像和CSS样式应用。

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 page

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值