node-html-pdf 技术文档

node-html-pdf 技术文档

【免费下载链接】node-html-pdf This repo isn't maintained anymore as phantomjs got dreprecated a long time ago. Please migrate to headless chrome/puppeteer. 【免费下载链接】node-html-pdf 项目地址: https://gitcode.com/gh_mirrors/no/node-html-pdf

安装指南

安装环境要求

  • 确保已安装 Node.js 环境。
  • 确保已安装 npm 包管理工具。

安装步骤

  1. 打开终端或命令行工具。
  2. 运行以下命令安装 html-pdf 工具:
    npm install -g html-pdf
    

项目使用说明

命令行使用示例

  1. 在终端中运行以下命令,将 HTML 文件转换为 PDF 文件:
    html-pdf test/businesscard.html businesscard.pdf
    

代码示例

以下是一个简单的代码示例,展示如何使用 html-pdf 模块将 HTML 文件转换为 PDF 文件:

var fs = require('fs');
var pdf = require('html-pdf');
var html = fs.readFileSync('./test/businesscard.html', 'utf8');
var options = { format: 'Letter' };

pdf.create(html, options).toFile('./businesscard.pdf', function(err, res) {
  if (err) return console.log(err);
  console.log(res); // { filename: '/app/businesscard.pdf' }
});

项目API使用文档

基本API

var pdf = require('html-pdf');
pdf.create(html).toFile([filepath, ]function(err, res){
  console.log(res.filename);
});

pdf.create(html).toStream(function(err, stream){
  stream.pipe(fs.createWriteStream('./foo.pdf'));
});

pdf.create(html).toBuffer(function(err, buffer){
  console.log('This is a buffer:', Buffer.isBuffer(buffer));
});

// 向后兼容
// 等同于 pdf.create(html[, options]).toBuffer(callback)
pdf.create(html [, options], function(err, buffer){});

页眉和页脚

html-pdf 可以从配置对象或 HTML 源代码中读取页眉和页脚。您可以设置默认的页眉和页脚,也可以通过在 HTML 标签中添加页码(基于 1 的索引)来覆盖默认设置。

您可以使用以下任何组合的标签:

<div id="pageHeader">默认页眉</div>
<div id="pageHeader-first">第一页页眉</div>
<div id="pageHeader-2">第二页页眉</div>
<div id="pageHeader-3">第三页页眉</div>
<div id="pageHeader-last">最后一页页眉</div>
...
<div id="pageFooter">默认页脚</div>
<div id="pageFooter-first">第一页页脚</div>
<div id="pageFooter-2">第二页页脚</div>
<div id="pageFooter-last">最后一页页脚</div>

项目安装方式

通过 npm 安装

npm install -g html-pdf

通过 GitHub 安装

  1. 克隆项目仓库:
    git clone https://github.com/marcbachmann/node-html-pdf.git
    
  2. 进入项目目录:
    cd node-html-pdf
    
  3. 安装依赖:
    npm install
    

通过以上步骤,您可以成功安装并使用 node-html-pdf 项目。

【免费下载链接】node-html-pdf This repo isn't maintained anymore as phantomjs got dreprecated a long time ago. Please migrate to headless chrome/puppeteer. 【免费下载链接】node-html-pdf 项目地址: https://gitcode.com/gh_mirrors/no/node-html-pdf

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值