Étape 1 - Installez le package PDF Creator à l'aide de la commande suivante
$ npm i pdf-creator-node --save
- L'approche de l'énergie ajoute le nom du package au fichier package.json.
Étape 2 - Ajouter les packages requis et lire le modèle HTML
//Required package
var pdf = require ( "pdf-creator-node" ) ;
var fs = require ( "fs" ) ;
// Read HTML Template
var html = fs . readFileSync ( "template.html" , "utf8" ) ;
Étape 3 - Créez votre modèle 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 >
Étape 4 - Fournir un format et une orientation selon vos besoins
"Hauteur": "10,5 pouces", // Unités autorisées: mm, cm, in, px
"largeur": "8in", // Unités autorisées: mm, cm, in, px
"Format": "Lettre", // Unités autorisées: A3, A4, A5, juridique, lettre, tabloïd
"Orientation": "Portrait", // Portrait ou paysage
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'
}
}
} ;
Étape 5 - Fournir un HTML, des données utilisateur et un chemin PDF pour la sortie
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.
Étape 6 - Après avoir réglé tous les paramètres, passez simplement le document et les options sur la méthode pdf.create
.
pdf
. create ( document , options )
. then ( ( res ) => {
console . log ( res ) ;
} )
. catch ( ( error ) => {
console . error ( error ) ;
} ) ;
Vous pouvez effectuer des vérifications conditionnelles en appelant l'exemple d' assistance
{ { # 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 >
Peut vérifier les variables avec une chaîne de type différente, IE, entier , booléen , double
Les autres opérateurs logiques sont-:
{ { # 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 } }
##NOTE!! Vous ne pouvez faire correspondre que 2 variables
PDF-créateur-nœud est sous licence MIT.