@browserless/pdf

中文文档
14.1.6发布于 2026/11/07MITCommonJS近 30 天下载 2.3万

Convert websites to high-quality PDFs with customizable margins, background printing, and optimized scaling.

原始使用文档
文档目录12 节
  1. Install
  2. About
  3. What this package does
  4. Usage
  5. Default settings
  6. Options
  7. Margin configuration
  8. Smart content detection
  9. Examples
  10. How it fits in the monorepo
  11. Dependencies
  12. License
browserless browserless

Powered by microlink.io Last version Coverage Status NPM Status

@browserless/pdf: Sensible good defaults for exporting a website as PDF.

See the pdf section on our website for more information.

Install

Using npm:

npm install @browserless/pdf --save

About

This package provides PDF generation from web pages with sensible defaults and smart content detection. It wraps Puppeteer's page.pdf() with optimized settings and a retry mechanism to ensure the page is fully rendered before generating the PDF.

What this package does

The @browserless/pdf package allows you to:

  • Generate PDFs from any URL with production-ready defaults
  • Wait until the page is ready before printing, including blank-screen checks
  • Customize margins with a simple string or per-side configuration
  • Scale content to fit pages optimally
  • Use all Puppeteer PDF options for advanced customization

Usage

const createPdf = require('@browserless/pdf')
const createGoto = require('@browserless/goto')

const goto = createGoto({ timeout: 30000 })
const pdf = createPdf({ goto })

// With browserless
const browserless = await browser.createContext()
const pdfBuffer = await browserless.pdf('https://example.com')

// Or directly with a page
const page = await browser.newPage()
const pdfFn = pdf(page)
const pdfBuffer = await pdfFn('https://example.com')

Default settings

The package applies these optimized defaults:

OptionDefaultDescription
margin'0.35cm'Page margins (applied to all sides)
scale0.65Content scale factor for optimal fit
printBackgroundtrueInclude background colors/images
waitUntil'auto'Smart waiting with content detection

Options

OptionTypeDefaultDescription
marginstring | object'0.35cm'Page margins
scalenumber0.65Scale of the webpage rendering (0.1 - 2)
printBackgroundbooleantruePrint background graphics
waitUntilstring'auto'When to consider navigation done
formatstring'Letter'Paper format (A4, Letter, etc.)
landscapebooleanfalsePaper orientation
widthstring | number—Paper width (overrides format)
heightstring | number—Paper height (overrides format)
pageRangesstring—Paper ranges to print, e.g., '1-5, 8'
headerTemplatestring—HTML template for header
footerTemplatestring—HTML template for footer
displayHeaderFooterbooleanfalseDisplay header and footer
preferCSSPageSizebooleanfalseGive priority to CSS @page size

All Puppeteer page.pdf() options are supported.

Margin configuration

Set margins as a single value (applied to all sides) or per-side:

// Single value for all sides
await browserless.pdf(url, { margin: '1cm' })

// Per-side configuration
await browserless.pdf(url, {
  margin: {
    top: '2cm',
    right: '1cm',
    bottom: '2cm',
    left: '1cm'
  }
})

Supported units: px, in, cm, mm

Smart content detection

When waitUntil: 'auto' (the default), the package:

  1. Navigates to the page
  2. Waits for paint/readiness signals (waitForReady + isPageReady)
  3. Hydrates overflow content when ready (prepareFullDocument)
  4. Generates the PDF

This ensures dynamic content (JavaScript-rendered pages) is fully loaded before PDF generation.

// Use auto detection (default)
await browserless.pdf(url, { waitUntil: 'auto' })

// Or use standard Puppeteer wait conditions
await browserless.pdf(url, { waitUntil: 'networkidle0' })
await browserless.pdf(url, { waitUntil: 'domcontentloaded' })

Examples

Basic PDF generation

const pdfBuffer = await browserless.pdf('https://example.com')
await fs.writeFile('output.pdf', pdfBuffer)

A4 format with custom margins

const pdfBuffer = await browserless.pdf('https://example.com', {
  format: 'A4',
  margin: '2cm',
  scale: 0.8
})

Landscape with header/footer

const pdfBuffer = await browserless.pdf('https://example.com', {
  landscape: true,
  displayHeaderFooter: true,
  headerTemplate: '<div style="font-size:10px; text-align:center; width:100%;">Header</div>',
  footerTemplate: '<div style="font-size:10px; text-align:center; width:100%;"><span class="pageNumber"></span>/<span class="totalPages"></span></div>'
})

Print specific pages

const pdfBuffer = await browserless.pdf('https://example.com', {
  pageRanges: '1-3, 5'
})

How it fits in the monorepo

This is a core functionality package for PDF generation:

ConsumerPurpose
browserless (core)Provides the .pdf() method
@browserless/cliPowers the browserless pdf command

Dependencies

PackagePurpose
@browserless/gotoPage navigation with ad blocking
@browserless/screenshotReadiness and blank-screen checks
@kikobeats/time-spanTiming measurements for retry logic
debug-logfmtStructured debug logging
pretty-msHuman-readable time formatting

License

@browserless/pdf © Microlink, released under the MIT License.
Authored and maintained by Microlink with help from contributors.

The logo has been designed by xinh studio.

microlink.io · GitHub microlinkhq · X @microlinkhq

版本与发布记录

@browserless/pdf 共发布 426 个版本,首次发布于 2019/03/11;过去 12 个月发布了 107 个版本。

版本发布日期距今
14.1.6latest2026/11/071 天前
14.1.52026/11/062 天前
14.1.42026/11/062 天前
14.1.22026/11/035 天前
14.1.12026/11/026 天前
14.0.332026/10/2712 天前

关于 @browserless/pdf 的常见问题

@browserless/pdf 是什么?

Convert websites to high-quality PDFs with customizable margins, background printing, and optimized scaling.

如何安装 @browserless/pdf?

在项目目录运行 npm install @browserless/pdf,或使用 pnpm add / yarn add / bun add。

@browserless/pdf 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/browserless__pdf 是否存在。

@browserless/pdf 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@browserless/pdf 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >= 24。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@browserless/pdf 可以商用吗?

@browserless/pdf 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@browserless/pdf 的下载量有多少?

根据 npm 官方下载统计,@browserless/pdf 在 2026/10/09 – 2026/11/07 这 30 天内被下载 23,184 次,日均约 773 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@browserless/pdf 还在维护吗?

最新版本 14.1.6 发布于 2026/11/07(1 天前);至今共发布 426 个版本,其中过去 12 个月发布了 107 个;首次发布于 2019/03/11。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。