https://pdfmake.github.io/docs/getting-started/client-side/
Instalar: npm install pdfmake
Codigo reutilizable:
import pdfMake from "pdfmake/build/pdfmake";import pdfFonts from "pdfmake/build/vfs_fonts";pdfMake.vfs = pdfFonts.pdfMake.vfs;var pdfObject;// https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Objetos_globales/Object/values// https://pdfmake.github.io/docs/getting-started/import * as moment from 'moment'// let dateCurrent = moment(data.created_at).format('DD-MM-YYYY')import { appConfig as env } from 'src/app/global/app.config';import { isNullOrUndefined } from 'util';//09-04-2020export function mappingHeader( arrHeader ){return arrHeader.map(item=>{return {text: item,style: 'tableHeader'};});};//12-04-2020function mappingBody( arrBody ){return arrBody.map( item=>Object.values(item) );};//12-04-2020function styleTable(){return {header: {fontSize: 18,bold: true,margin: [0, 0, 0, 10]},subheader: {fontSize: 16,bold: true,margin: [0, 10, 0, 5]},tableExample: {margin: [0, 5, 0, 15]},tableHeader: {bold: true,fontSize: 13,color: 'black'}};}//12-04-2020export function _exportPDF( arrData:Array<any>,arrHeaderCustom?:Array<string>, title?:string,subtitle?:string ){try {var arrHeader:Array<string>;if( isNullOrUndefined(arrHeaderCustom) ) arrHeader = Object.keys(arrData[0]);else arrHeader = arrHeaderCustom;arrHeader = mappingHeader( arrHeader );arrData = mappingBody( arrData );// console.log( arrData, arrHeader );let obj:Array<any> = [{ text: title , style: 'header'},{ text: [ subtitle ], color: 'gray', italics: true },{ table: {headerRows: 1,/* widths: ['*', 'auto'], widths: [ '*', 'auto', 100, '*' ],*/body: [ arrHeader ]}, style: 'tableExample',},];for (const item of arrData ) {obj[2].table.body.push( item );}var dd = {pageOrientation: 'landscape',pageMargins: [ 20, 30, 20, 30 ],content: obj,styles: styleTable()};pdfObject = pdfMake.createPdf( dd );pdfObject.download();} catch (err) {console.error( 'Error al generar pdf', err );}};===================MODO DE USO EN COMPONENTES//IMPORTARimport { _exportPDF } from 'src/app/core/utils/table_pdf';// 12-04-2020exportPDF(){var data = JSON.parse(JSON.stringify(this.dataList));var data = data.map(item=>{delete item._id;delete item.photo; delete item.status; delete item.user;return item;});_exportPDF( data, null, "Pacientes",null );}
Personalizacion:
*cambiar nombre del archivo
info: {
title: 'Reporte',
author: 'john doe',
subject: 'subject of document',
keywords: 'keywords for document',
},
*orientacion de pagina
pageOrientation: 'landscape',
*Footer en pagina - fuera del content
footer: {
columns: [
{ text: 'Criterios: Estado=true, Subcategory="Carne roja"', alignment: 'center' }
]
},
* Imagen de logo
{
// you can also fit the image inside a rectangle
image: 'data:image/png;base64,iVg==',
//fit: [100, 100],
width: 200
},
No hay comentarios.:
Publicar un comentario