Etapa 1 - Instale o pacote do criador em PDF usando o seguinte comando
$ npm i pdf-creator-node --save
-Save Flag adiciona o nome do pacote ao arquivo package.json.
Etapa 2 - Adicione os pacotes necessários e leia o modelo HTML
//Required package
var pdf = require ( "pdf-creator-node" ) ;
var fs = require ( "fs" ) ;
// Read HTML Template
var html = fs . readFileSync ( "template.html" , "utf8" ) ;
Etapa 3 - Crie seu modelo 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 >
Etapa 4 - forneça formato e orientação conforme sua necessidade
"altura": "10.5in", // unidades permitidas: mm, cm, in, px
"Largura": "8in", // unidades permitidas: mm, cm, in, px
"formato": "letra", // unidades permitidas: A3, A4, A5, Legal, Carta, tablóide
"Orientação": "Retrato", // retrato ou paisagem
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'
}
}
} ;
Etapa 5 - Forneça HTML, dados do usuário e caminho em PDF para saída
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.
Etapa 6 - Depois de definir todos os parâmetros, basta passar o documento e as opções para o método pdf.create
.
pdf
. create ( document , options )
. then ( ( res ) => {
console . log ( res ) ;
} )
. catch ( ( error ) => {
console . error ( error ) ;
} ) ;
Você pode fazer verificações condicionais chamando o Exemplo do Bloco Helper 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 >
Pode verificar variáveis com diferentes string do tipo ie, número inteiro , booleano , duplo
Outros operadores lógicos são-:
{ { # 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 } }
##OBSERVAÇÃO!! Você só pode corresponder a 2 variáveis
O nó PDF-Creators é licenciado pelo MIT.