Paso 1: instale el paquete PDF Creator usando el siguiente comando
$ npm i pdf-creator-node --save
--Save Flag agrega el nombre del paquete al archivo Packle.json.
Paso 2: agregue los paquetes requeridos y lea la plantilla HTML
//Required package
var pdf = require ( "pdf-creator-node" ) ;
var fs = require ( "fs" ) ;
// Read HTML Template
var html = fs . readFileSync ( "template.html" , "utf8" ) ;
Paso 3 - Crea tu plantilla 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 >
Paso 4: proporcionar formato y orientación según su necesidad
"Altura": "10.5in", // unidades permitidas: mm, cm, in, px
"Ancho": "8in", // unidades permitidas: mm, cm, in, px
"Formato": "Carta", // Unidades permitidas: A3, A4, A5, Legal, Carta, Tabloide
"Orientación": "Retrato", // Retrato o paisaje
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'
}
}
} ;
Paso 5: proporcionar HTML, datos de usuario y ruta PDF para la salida
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.
Paso 6 - Después de configurar todos los parámetros, simplemente pase documento y opciones al método pdf.create
.
pdf
. create ( document , options )
. then ( ( res ) => {
console . log ( res ) ;
} )
. catch ( ( error ) => {
console . error ( error ) ;
} ) ;
Puede hacer verificaciones condicionales llamando a un ejemplo de bloque auxiliar
{ { # 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 >
Puede verificar variables con diferentes tipos, es decir, una cadena , entero , booleano , doble
Otros operadores lógicos son::
{ { # 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 } }
##¡¡NOTA!! Solo puedes coincidir con 2 variables
PDF-creator-nodo tiene licencia MIT.