Backend en NODE.js para generar tablas en HTML y posteriormente con ese DOM crear PDFs, con geaders, contenido de las tablas y headers de tablas en cada pagina, footer de paginacion.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
3.0 KiB

2 years ago
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) {
2 years ago
const browser = await puppeteer.launch({ headless: true });
const page = await browser.newPage();
await page.goto(`http://localhost:${port}/Invoice?id=${id}`, { waitUntil: 'networkidle0' });
2 years ago
const pdf = await page.pdf({
format: 'A4', displayHeaderFooter: true,
headerTemplate: ``,
footerTemplate: `
<div style="border-top: solid 1px #bbb; width: 100%; font-size: 9px;
padding: 5px 5px 0; color: #bbb; position: relative;">
<div style="position: absolute; right: 5px; top: 5px;"><span class="pageNumber"></span> DE <span class="totalPages"></span></div>
</div>
`,
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 => {
2 years ago
res.set({ 'Content-Type': 'application/pdf', 'Content-Length': pdf.length })
res.send(pdf)
})
})
app.listen(port, () => {
console.log(`App listening at port ${port}`)
})