360 Secure SkillHub
360 安全技能中心

HTML to PDF

该 Skill 通过 Node 脚本与 Puppeteer 无头浏览器把本地 HTML 文件或网页 URL 渲染为 PDF,支持页面格式、横竖方向、边距、缩放、背景打印、页眉页脚和页码范围等选项,既可命令行调用也可作为模块在代码中调用。

hankagent
hankagent
热度 90.29
最近更新 2026-05-17 15:20:50
分类:效率依赖:代码执行 / 联网浏览搜索 / 读写文件应用场景:文件转换 / 自动化脚本

安装技能

复制下面的话术发给你的 Agent,它会通过 secure-skills 检索并安装该技能。

执行命令安装此 skill: npx secure-skills-cli add html-to-pdf
Metadata
name
html-to-pdf
description
Convert HTML files and URLs to PDF using Puppeteer. Use when a user needs to convert HTML documents, web pages, or reports to PDF format with custom formatting options (margins, page size, orientation, headers/footers).

HTML to PDF Conversion

Convert HTML files and web pages to professional PDF documents using Puppeteer.

Quick Start

CLI Usage

node scripts/html-to-pdf.js input.html output.pdf
node scripts/html-to-pdf.js input.html output.pdf A4
node scripts/html-to-pdf.js https://example.com output.pdf A4

Programmatic Usage

const convertHtmlToPdf = require('./scripts/html-to-pdf.js');

// Simple conversion
await convertHtmlToPdf('input.html', 'output.pdf');

// With options
await convertHtmlToPdf('input.html', 'output.pdf', {
  format: 'Letter',
  landscape: true,
  margin: { top: '20mm', bottom: '20mm' }
});

Features

  • ✅ Convert local HTML files
  • ✅ Convert web URLs (http/https)
  • ✅ Customizable page formats (A4, Letter, etc.)
  • ✅ Custom margins and spacing
  • ✅ Landscape/portrait modes
  • ✅ Print backgrounds
  • ✅ Headers and footers
  • ✅ Page ranges
  • ✅ Zoom scaling

Common Options

Option Type Default Notes
format string 'A4' A4, Letter, A3, A5, etc.
landscape boolean false Landscape orientation
margin object 10mm all { top, right, bottom, left }
scale number 1 0.1 to 2.0
printBackground boolean true Include background colors
displayHeaderFooter boolean false Show header/footer

Advanced Usage

With Headers and Footers

await convertHtmlToPdf('page.html', 'output.pdf', {
  displayHeaderFooter: true,
  headerTemplate: '<div>Page <span class="pageNumber"></span></div>',
  footerTemplate: '<div>© 2024</div>',
  margin: { top: '40px', bottom: '40px' }
});

Specific Page Ranges

await convertHtmlToPdf('document.html', 'pages-1-5.pdf', {
  pageRanges: '1-5'
});

Installation

Requires Node.js and npm:

npm install puppeteer

Reference

For complete API documentation and all available options, see api.md.