参数说明

HTML/URL地址渲染生成PDF,主要采用Puppetter,参数主要作用于Puppetter,其参数名称与Puppetter内部使用的参数名称和作用基本保持一致。例如:
page.pdf([options])
options 有以下属性:

  • scale
  • displayHeaderFooter
  • headerTemplate

……
在使用API接口进行传参数时,对应的改为了【pdf.scale】、【pdf.displayHeaderFooter】、【pdf.headerTemplate】,方便理解参数含义。

Puppetter参数说明见:Puppetter参数说明

通用参数

参数 类型 默认值 必传 描述
url String -
- [x]

| 需渲染为PDF的URL地址 | | outputType | String | - |
- [x]

| 输出格式。主要包括:PDF | | output | String | - | | 输出目录 | | fileName | String | - |
- [x]

| 输出文件名称, 由数字和字母组成,如:test | | emulateMediaType | boolean | false | | 输出PDF时多媒体处理 | | cookies | Array | - | | cookie相关配置,详细见【Cookie相关参数】 | | pdf | Object | - | | PDF相关参数配置,详细见【PDF相关参数】 | | launch | Object | - | | Launch相关参数配置见【Launch相关参数】 |

Cookie相关参数

参数 类型 默认值 必传 描述
name String -
- [x]

| Cookie名称 | | value | String | - |
- [x]

| Cookie值 | | url | String | - | | Cookie url | | domain | String | - | | Cookie domain | | path | String | - | | Cookie path | | expires | long | - | | Cookie 过期时间(ms) | | httpOnly | boolean | - | | Cookie httpOnly | | secure | boolean | - | | Cookie secure | | sameSite | String | - | | Strict 或者 Lax |

PDF相关参数

参数 类型 默认值 必传 描述
scale double 1 网页渲染的比例。 默认值为1。小数位数必须介于0.1和2之间。
printBackground boolean true 打印背景图片
displayHeaderFooter boolean false 展示header和footer
headerTemplate String - header模版
footerTemplate String - footer模版
landscape boolean false 纸张方向
pageRanges String 打印范围,例如:1-5, 8, 11-13. 默认为空,表示打印所有页
format String Letter 纸张格式。如果设置,则优先级低于宽度或者高度选项。支持:Letter、Legal、Tabloid、Ledger、A0、A1、A2、A3、A4、A5、A6
width String - 纸张宽度。单位为:px、in、cm、mm
height String - 纸张高度。单位为:px、in、cm、mm
margin.top String - 上边距
margin.right String - 右边距
margin.bottom String - 下边距
margin.left String - 左边距
preferCSSPageSize boolean false 将在页面中声明的任何CSS @page大小优先于在width和height或format选项中声明的大小。 默认值为false,这将缩放内容以适合纸张尺寸。

Launch相关参数

参数 类型 默认值 必传 描述
ignoreTTPSErrors boolean true 是否忽略https的错误
headless boolean true 是否以无头浏览器模式运行
timeout long 30000 最大等待浏览器示例启动的时间
defaultViewport.width int 1600 page的宽
defaultViewport.height int 1200 page的高
defaultViewport.isLandscape boolean false 指定是否处于横向模式
defaultViewport.deviceScaleFactor int 1 指定设备比例因子(可以认为是dpr)

PDF渲染API

1. POST /rest/pdf/render

Body

  1. {
  2. "url": "",
  3. "outputType": "PDF",
  4. "output": "./",
  5. "fileName": "",
  6. "emulateScreenMedia": false,
  7. "pdf": {...},
  8. "cookies": {...},
  9. "launch": {...}
  10. }

返回值

  1. {
  2. "data": "",
  3. "message": "",
  4. "success": true
  5. }

2. GET /rest/pdf/show

请求体

【参数见上面参数】5.html

返回值

Response Header:

  1. Access-Control-Expose-Headers: Content-Disposition
  2. Content-Disposition: inline;filename=fileName.pdf
  3. Content-Type: application/pdf;charset=utf-8


注意: 浏览器尝试打开,支持office online或浏览器预览pdf功能**