1 단계 - 다음 명령을 사용하여 PDF Creator 패키지 설치
$ npm i pdf-creator-node --save
-save flag 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-Node는 MIT 라이센스가 부여되었습니다.