const express = require ( 'express' ) ;
const puppeteer = require ( 'puppeteer' )
var Reportes = require ( './Repository/Reportes' ) ;
const app = express ( )
let options = { format : 'A4' } ;
const port = 9091
app . set ( 'view engine' , 'ejs' )
const user = {
firstName : 'Tim' ,
lastName : 'Cook' ,
}
app . get ( '/' , ( req , res ) => {
Reportes . getAmazonInvoicePDF ( req . query . id ) . then ( ( InvoiceDetail ) => {
//res.status(200).send(data);
res . render ( 'pages/AmazonInvoice' , {
user : user ,
InvoiceDetail : InvoiceDetail
} )
} )
} )
app . get ( '/Invoice' , async ( req , res ) => {
const InvoiceDetail = await Reportes . getAmazonInvoiceDetail ( req . query . id ) . then ( ( InvoiceDetail ) => {
return InvoiceDetail
} )
const InvoiceShipmentInfo = await Reportes . getAmazonInvoiceShipmentInfo ( req . query . id ) . then ( ( InvoiceShipmentInfo ) => {
return InvoiceShipmentInfo
} )
const InvoiceHeader = await Reportes . getAmazonInvoiceHeader ( req . query . id ) . then ( ( InvoiceHeader ) => {
return InvoiceHeader
} )
const ObjBillerInfo = InvoiceShipmentInfo . filter ( shipment => shipment . AddressType === 'BILLER' ) ;
const BillerL1 = ObjBillerInfo [ 0 ] . Name
const BillerL2 = ObjBillerInfo [ 0 ] . AddressLine1 + ", " + ObjBillerInfo [ 0 ] . City + ", " + ObjBillerInfo [ 0 ] . StateProvince + ", " + ObjBillerInfo [ 0 ] . CountryName
const BillerL3 = ObjBillerInfo [ 0 ] . Zip
const ObjSellerInfo = InvoiceShipmentInfo . filter ( shipment => shipment . AddressType === 'SELLER' ) ;
const SellerL1 = ObjSellerInfo [ 0 ] . Name
const SellerL2 = ObjSellerInfo [ 0 ] . AddressLine1 + ", " + ObjBillerInfo [ 0 ] . City + ", " + ObjBillerInfo [ 0 ] . StateProvince + ", " + ObjBillerInfo [ 0 ] . CountryName
const SellerL3 = ObjSellerInfo [ 0 ] . Zip
res . render ( 'pages/Amazon/Invoice' , {
user : user ,
InvoiceDetail : InvoiceDetail ,
InvoiceHeader : InvoiceHeader [ 0 ] ,
AmazonShipmentReferenceId : InvoiceDetail [ 0 ] . AmazonShipmentReferenceId ,
BillerL1 : BillerL1 ,
BillerL2 : BillerL2 ,
BillerL3 : BillerL3 ,
SellerL1 : SellerL1 ,
SellerL2 : SellerL2 ,
SellerL3 : SellerL3
} )
} )
async function printPDF ( id ) {
const browser = await puppeteer . launch ( { headless : true } ) ;
const page = await browser . newPage ( ) ;
await page . goto ( ` http://localhost: ${ port } /Invoice?id= ${ id } ` , { waitUntil : 'networkidle0' } ) ;
const pdf = await page . pdf ( {
format : 'A4' , displayHeaderFooter : true ,
headerTemplate : ` ` ,
footerTemplate : `
< div style = " border - top : solid 1 px # bbb ; width : 100 % ; font - size : 9 px ;
padding : 5 px 5 px 0 ; color : # bbb ; position : relative ; " >
< div style = "position: absolute; right: 5px; top: 5px;" > < span class = "pageNumber" > < / s p a n > D E < s p a n c l a s s = " t o t a l P a g e s " > < / s p a n > < / d i v >
< / d i v >
` ,
margin : { bottom : '70px' } ,
} ) ;
await browser . close ( ) ;
return pdf
}
app . get ( '/getAmazonInvoicePDF' , async ( req , res ) => {
let file = [ { url : ` http://localhost: ${ port } /Invoice?id= ${ req . query . id } ` , name : 'AmazonInvoice.pdf' } ] ;
printPDF ( req . query . id ) . then ( pdf => {
res . set ( { 'Content-Type' : 'application/pdf' , 'Content-Length' : pdf . length } )
res . send ( pdf )
} )
} )
app . listen ( port , ( ) => {
console . log ( ` App listening at port ${ port } ` )
} )