Skip to content

配置

所有选项作为第二个参数传递给 html2pptx()

typescript
const pptx = await html2pptx(element, options);

演示文稿选项

选项默认值说明
slideLayout'LAYOUT_16x9'幻灯片布局:'LAYOUT_16x9''LAYOUT_4x3''LAYOUT_16x10''LAYOUT_WIDE''LAYOUT_USER'
title-演示文稿标题元数据
author-演示文稿作者元数据
company-公司名称元数据

渲染选项

选项默认值说明
scaleauto缩放比例。省略时,自动计算以适应幻灯片而不放大
x0X 轴偏移量(像素)
y0Y 轴偏移量(像素)
width元素宽度输出宽度(像素)
height元素高度输出高度(像素)
backgroundColor#ffffff背景颜色。设为 null 表示透明

行为选项

选项默认值说明
loggingtrue启用调试日志
skipValidationfalse跳过输入验证(SSRF/XSS 检查)
enablePerformanceMonitoringfalse启用流水线性能监控
removeContainertrue渲染后清理克隆的 iframe

回调选项

选项默认值说明
onclone-文档克隆后的回调函数。接收 (document, element) 参数。用于在渲染前修改内容
ignoreElements-用于从渲染中排除元素的谓词函数

onclone 示例

typescript
const pptx = await html2pptx(element, {
    onclone: (clonedDoc, clonedElement) => {
        // 在 PPTX 输出中隐藏水印
        const watermark = clonedDoc.querySelector('.watermark');
        if (watermark) watermark.style.display = 'none';
    }
});

ignoreElements 示例

typescript
const pptx = await html2pptx(element, {
    ignoreElements: (el) => {
        return el.classList.contains('no-export');
    }
});

图片处理选项

选项默认值说明
allowTaintfalse允许跨域图片
useCORSfalse使用 CORS 加载图片
imageTimeout15000图片加载超时时间(毫秒)。设为 0 禁用
proxy-加载跨域图片的代理 URL

高级选项

选项默认值说明
pptx-外部 PptxGenJS 实例,用于向其添加幻灯片(多页幻灯片内部使用)
cspNonce-Content-Security-Policy 的 nonce 值
windowWidthwindow.innerWidth媒体查询求值的窗口宽度
windowHeightwindow.innerHeight媒体查询求值的窗口高度
scrollX页面滚动 XX 轴滚动位置
scrollY页面滚动 YY 轴滚动位置

完整示例

typescript
import html2pptx from 'html2pptx-pro';

const pptx = await html2pptx(document.getElementById('report'), {
    title: 'Quarterly Report',
    author: 'Analytics Team',
    slideLayout: 'LAYOUT_16x9',
    logging: false,
    ignoreElements: (el) => el.classList.contains('screen-only'),
    onclone: (doc, el) => {
        doc.querySelector('.timestamp').textContent = new Date().toLocaleDateString();
    }
});

const blob = await pptx.write({ outputType: 'blob' });