Help us shape the future of Go! Take the 2026 Go Developer Survey

browser

package
v0.1.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 21, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package browser 通过 chromedp 驱动 Chrome/Chromium 把 HTML/MHTML 渲染为 PDF。 它使用浏览器原生打印引擎,支持纸张尺寸、背景和外部资源访问控制。

Index

Constants

View Source
const DefaultTimeout = 2 * time.Minute

DefaultTimeout 是单次渲染的默认超时时间。

View Source
const EnvChrome = "OFD_CHROME"

EnvChrome 是覆盖 Chrome/Chromium 可执行文件路径的环境变量名。

Variables

This section is empty.

Functions

func ConvertFileToPDF

func ConvertFileToPDF(ctx context.Context, path string, options Options) ([]byte, error)

ConvertFileToPDF 把本地 HTML/MHTML 文件渲染为 PDF 字节。

func FindChrome

func FindChrome(explicit string) (string, error)

FindChrome 定位 Chrome/Chromium 可执行文件。显式路径与 OFD_CHROME 会被校验; 否则在 PATH 中查找,仍未命中时返回空路径,交由 chromedp 在其内置的 PATH 与平台常见安装目录中查找。显式路径无效时返回明确错误。

Types

type Options

type Options struct {
	// Chrome 是可执行文件路径;为空时自动查找。
	Chrome string
	// Timeout 是渲染超时;为 0 时使用 DefaultTimeout。
	Timeout time.Duration
	// Width 和 Height 是纸张尺寸(毫米)。
	Width  float64
	Height float64
	// Landscape 为真时横向打印。
	Landscape bool
	// PrintBackground 为真时打印背景颜色和图片。
	PrintBackground bool
	// AllowRemoteResources 为真时允许加载外部资源;默认禁止。
	AllowRemoteResources bool
	// NoSandbox 为真时给 Chrome 加 --no-sandbox。
	NoSandbox bool
	// TempDir 是临时文件根目录;为空时使用系统临时目录。
	TempDir string
}

Options 控制单次渲染行为。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL