weasyprint 命令详解

| 选择喜欢的代码风格  

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

weasyprint 命令安装:


-bash/zsh: weasyprint: command not found

# 如果您的发行版上没有 WeasyPrint,或者您想使用更新版本的 WeasyPrint,
# 则必须确保安装了 Python(至少版本 3.7.0)和 Pango(至少版本 1.44.0) 通过启动来验证这一点:

python3 --version
pango-view --version

# 如果您的发行版提供的 Pango 版本太旧,您可以使用 WeasyPrint 52.5 版本,它不需要最新的 Pango 功能。


# 当一切正常后,您可以直接在系统上或使用 pip 在虚拟环境中安装 WeasyPrint:
python3 -m venv venv
source venv/bin/activate
pip install weasyprint
weasyprint --info


# Windows (WSL2)
Only Windows 11 64-bit is supported!

# Debian ≥ 11
# To install WeasyPrint without a virtualenv, you need the following packages:
apt install python3-pip python3-cffi python3-brotli libpango-1.0-0 libpangoft2-1.0-0

# To install WeasyPrint inside a virtualenv using wheels (if possible), you need the following packages:
apt install python3-pip libpango-1.0-0 libpangoft2-1.0-0

# To install WeasyPrint inside a virtualenv without using wheels, you need the following packages:
apt install python3-pip libpango-1.0-0 libpangoft2-1.0-0 libjpeg-dev libopenjp2-7-dev libffi-dev


# Ubuntu ≥ 20.04
# To install WeasyPrint without a virtualenv, you need the following packages:
apt install python3-pip python3-cffi python3-brotli libpango-1.0-0 libharfbuzz0b libpangoft2-1.0-0

# To install WeasyPrint inside a virtualenv using wheels (if possible), you need the following packages:
apt install python3-pip libpango-1.0-0 libharfbuzz0b libpangoft2-1.0-0

# To install WeasyPrint inside a virtualenv without using wheels, you need the following packages:
apt install python3-pip libpango-1.0-0 libharfbuzz0b libpangoft2-1.0-0 libffi-dev libjpeg-dev libopenjp2-7-dev


# Alpine ≥ 3.12
# To install WeasyPrint without a virtualenv, you need the following packages:
apk add py3-pip py3-pillow py3-cffi py3-brotli gcc musl-dev python3-dev pango

# To install WeasyPrint inside a virtualenv using wheels (if possible), you need the following packages:
apk add py3-pip gcc musl-dev python3-dev pango zlib-dev jpeg-dev openjpeg-dev g++ libffi-dev

# To install WeasyPrint inside a virtualenv without using wheels, you need the following packages:
apk add py3-pip gcc musl-dev python3-dev pango zlib-dev jpeg-dev openjpeg-dev g++ libffi-dev


# Arch Linux
# To install WeasyPrint without a virtualenv, you need the following packages:
pacman -S python-pip pango python-cffi python-pillow python-brotli python-zopfli

# To install WeasyPrint inside a virtualenv using wheels (if possible), you need the following packages:
pacman -S python-pip pango

# To install WeasyPrint inside a virtualenv without using wheels, you need the following packages:
pacman -S python-pip pango gcc libjpeg-turbo openjpeg2


# Fedora ≥ 34
# To install WeasyPrint without a virtualenv, you need the following packages:
yum install python-pip python-pillow python-cffi python3-brotli pango

# To install WeasyPrint inside a virtualenv using wheels (if possible), you need the following packages:
yum install python-pip pango

# To install WeasyPrint inside a virtualenv without using wheels, you need the following packages:
yum install python-pip pango gcc python3-devel gcc-c++ zlib-devel libjpeg-devel openjpeg2-devel libffi-devel


# OS X
brew install weasyprint

# Dockerfile
dockerfile.run/weasyprint

weasyprint 命令补充说明:


WeasyPrint 是一种智能解决方案,可帮助 Web 开发人员创建 PDF 文档。 它是免费的开源软件,可以轻松插入您的应用程序和网站,并将简单的 HTML 页面变成华丽的 PDF(可以是统计图表、账单、门票等…)

WeasyPrint 是一个 HTML / CSS 的可视化渲染引擎,可以导出为 PDF 和 PNG。 支持打印标准。WeasyPrint 基于各种库,但不基于完整的渲染引擎,如 WebKit 或 Gecko。

WeasyPrint 的 CSS 布局引擎是用 Python 编写,专为分页而设计。

weasyprint 命令语法:


weasyprint [-h] [--version] [-e ENCODING] [-f {pdf,png}]
                         [-s STYLESHEET] [-m MEDIA_TYPE] [-r RESOLUTION]
                         [-u BASE_URL] [-a ATTACHMENT] [-p] [-v] [-d] [-q]
                         input output

weasyprint 命令选项:


positional arguments:
         input                 URL or filename of the HTML input, or - for stdin
         output                Filename where output is written, or - for stdout

optional arguments:
         -h, --help            show this help message and exit
         --version             Print WeasyPrint's version number and exit.
         -e ENCODING, --encoding ENCODING
                               Character encoding of the input
         -f {pdf,png}, --format {pdf,png}
                               Output format. Can be omitted if `output` ends with a
                               .pdf or .png extension.
         -s STYLESHEET, --stylesheet STYLESHEET
                               URL or filename for a user CSS stylesheet. May be
                               given multiple times.
         -m MEDIA_TYPE, --media-type MEDIA_TYPE
                               Media type to use for @media, defaults to print
         -r RESOLUTION, --resolution RESOLUTION
                               PNG only: the resolution in pixel per CSS inch.
                               Defaults to 96, one PNG pixel per CSS pixel.
         -u BASE_URL, --base-url BASE_URL
                               Base for relative URLs in the HTML input. Defaults to
                               the input's own filename or URL or the current
                               directory for stdin.
         -a ATTACHMENT, --attachment ATTACHMENT
                               URL or filename of a file to attach to the PDF
                               document
         -p, --presentational-hints
                               Follow HTML presentational hints.
         -v, --verbose         Show warnings and information messages.
         -d, --debug           Show debugging messages.
         -q, --quiet           Hide logging messages.

weasyprint 命令实例:


weasyprint 将 HTML 文件渲染为 PDF:

weasyprint [path/to/input.html] [path/to/output].pdf

weasyprint 将 HTML 文件渲染为 PNG,包括附加的用户样式表:

weasyprint [path/to/input.html] [path/to/output].png --stylesheet [path/to/stylesheet.css]

weasyprint 渲染时输出额外的调试信息:

weasyprint [path/to/input.html] [path/to/output].pdf --verbose

weasyprint 输出为 PNG 时,指定自定义分辨率

weasyprint [path/to/input.html] [path/to/output].png --resolution [300]

weasyprint 为输入 HTML 文件中的相对 URL 指定基本 URL:

weasyprint [path/to/input.html] [path/to/output].png --base-url [url_or_filename]

WeasyPrint 扩展阅读:




weasyprint 命令评论