步骤1-使用以下命令安装PDF Creator软件包
$ npm i pdf-creator-node --save
- 保存标志将软件包名称添加到package.json文件。
步骤2-添加必需的软件包并读取HTML模板
//Required package
var pdf = require ( "pdf-creator-node" ) ;
var fs = require ( "fs" ) ;
// Read HTML Template
var html = fs . readFileSync ( "template.html" , "utf8" ) ;
步骤3-创建您的HTML模板
<!DOCTYPE html >
< html >
< head >
< meta charset =" utf-8 " />
< title > Hello world! </ title >
</ head >
< body >
< h1 > User List </ h1 >
< ul >
{{#each users}}
< li > Name: {{this.name}} </ li >
< li > Age: {{this.age}} </ li >
< br />
{{/each}}
</ ul >
</ body >
</ html >
步骤4-根据您的需要提供格式和方向
“高度”:“ 10.5in”,//允许的单位:mm,cm,in,px
“宽度”:“ 8in”,//允许的单位:mm,cm,in,px
“格式”:“字母”,//允许的单位:A3,A4,A5,法律,字母,小报
“方向”:“肖像”,//肖像或景观
var options = {
format : "A3" ,
orientation : "portrait" ,
border : "10mm" ,
header : {
height : "45mm" ,
contents : '<div style="text-align: center;">Author: Shyam Hajare</div>'
} ,
footer : {
height : "28mm" ,
contents : {
first : 'Cover page' ,
2 : 'Second page' , // Any page number is working. 1-based index
default : '<span style="color: #444;">{{page}}</span>/<span>{{pages}}</span>' , // fallback value
last : 'Last Page'
}
}
} ;
步骤5-为输出提供HTML,用户数据和PDF路径
var users = [
{
name : "Shyam" ,
age : "26" ,
} ,
{
name : "Navjot" ,
age : "26" ,
} ,
{
name : "Vitthal" ,
age : "26" ,
} ,
] ;
var document = {
html : html ,
data : {
users : users ,
} ,
path : "./output.pdf" ,
type : "" ,
} ;
// By default a file is created but you could switch between Buffer and Streams by using "buffer" or "stream" respectively.
步骤6-设置所有参数后,只需将文档和选项传递给pdf.create
方法。
pdf
. create ( document , options )
. then ( ( res ) => {
console . log ( res ) ;
} )
. catch ( ( error ) => {
console . error ( error ) ;
} ) ;
您可以通过调用助手块IFCOND示例进行有条件的检查
{ { # ifCond inputData "===" toCheckValue } }
<!DOCTYPE html >
< html >
< head >
< meta charset =" utf-8 " />
< title > Hello world! </ title >
</ head >
< body >
< h1 > User List </ h1 >
< ul >
{{#each users}}
< li > Name: {{this.name}} </ li >
< li > Age: {{#ifCond this.age '===' '26'}} </ li >
< br />
{{/ifCond}}
{{/each}}
</ ul >
</ body >
</ html >
可以检查使用不同类型的IE字符串,整数,布尔值,双重检查变量
其他逻辑运算符是 - :
{ { # ifCond inputData "==" toCheckValue } }
{ { # ifCond inputData "===" toCheckValue } }
{ { # ifCond inputData "!=" toCheckValue } }
{ { # ifCond inputData "!==" toCheckValue } }
{ { # ifCond inputData "<" toCheckValue } }
{ { # ifCond inputData "<=" toCheckValue } }
{ { # ifCond inputData ">" toCheckValue } }
{ { # ifCond inputData ">=" toCheckValue } }
{ { # ifCond inputData "&&" toCheckValue } }
{ { # ifCond inputData "||" toCheckValue } }
##笔记!!您只能匹配2个变量
PDF-CREATOR节点已获得MIT许可。