Advertisement
jarturon55

generate function

Jul 3rd, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.   generate(header, content) {
  2.  
  3.     let doc = new jsPDF('l', 'px', 'a3');
  4.     let images = [];
  5.     let eachImgRow = []
  6.     let maxHeight = [0, 0, 0, 0, 0, 0];
  7.     let maxWidth = [0, 0, 0, 0, 0, 0];
  8.  
  9.     let arrImgElements = [] // contain all image data
  10.     let imgColumn = [] // contain index of image column
  11.     let imgKeyColumn = [] // contain key on image column
  12.     let indexTemp: number;
  13.     let columnIndex = 0;
  14.  
  15.     // get image column index
  16.  
  17.     for (let key in content[0]) {
  18.       if (content[0][key].hasOwnProperty('src')) {
  19.         imgColumn = [...imgColumn, columnIndex]
  20.         imgKeyColumn = [...imgKeyColumn, key]
  21.       }
  22.       columnIndex++
  23.     }
  24.     // get all image data
  25.     for (const each of content) {
  26.       indexTemp = 0;
  27.       for (const key in each) {
  28.         if (imgColumn.includes(indexTemp)) {
  29.           arrImgElements = [...arrImgElements, each[key]]
  30.           each[key] = ''
  31.         }
  32.         indexTemp++
  33.       }
  34.     }
  35.     // split image data to image data per row
  36.     for (let i = 0; i < arrImgElements.length / imgColumn.length; i++) {
  37.       const imgColSize = imgColumn.length;
  38.       if (i === 0) {
  39.         eachImgRow = [...eachImgRow, arrImgElements.slice(0, imgColSize)]
  40.       }
  41.       else {
  42.         eachImgRow = [...eachImgRow, arrImgElements.slice((i * imgColSize) + 1,
  43.           (i * imgColSize) + (imgColSize + 1))]
  44.       }
  45.     }
  46.     // //get max height and width of each of column
  47.     // for (let objectIndex = 0; objectIndex < eachImgRow.length; objectIndex++) {
  48.     //   for (let imgIndex = 0; imgIndex < eachImgRow[objectIndex].length; imgIndex++) {
  49.     //     if (eachImgRow[objectIndex][imgIndex].clientHeight > maxHeight[imgIndex]) {
  50.     //       maxHeight[imgIndex] = eachImgRow[objectIndex][imgIndex].clientHeight;
  51.     //     }
  52.     //     if (eachImgRow[objectIndex][imgIndex].clientWidth > maxWidth[imgIndex]) {
  53.     //       maxWidth[imgIndex] = eachImgRow[objectIndex][imgIndex].clientWidth;
  54.     //     }
  55.     //   }
  56.     // }
  57.  
  58.     let columnStyles = {} //contain max width of image column
  59.     columnIndex = 0
  60.     // for (const index of imgKeyColumn) { //get max width of image column
  61.     //   // columnStyles[index] = { columnWidth: maxWidth[columnIndex] } use for dynamic size
  62.     //   columnStyles[index] = { columnWidth: 110 }
  63.     //   columnIndex++
  64.     // }
  65.  
  66.     columnStyles['id'] = { columnWidth: 30, valign: 'middle', fontSize: 12, halign: 'center' }
  67.     columnStyles['score'] = { columnWidth: 50, valign: 'middle', fontSize: 12, halign: 'center' }
  68.     columnStyles['bestRef1'] = { columnWidth: 162, columnHeight: 50 }
  69.     columnStyles['sign1'] = { columnWidth: 162, columnHeight: 50 }
  70.     columnStyles['score1'] = { columnWidth: 50, valign: 'middle', fontSize: 12, halign: 'center' }
  71.     columnStyles['bestRef2'] = { columnWidth: 162, columnHeight: 50 }
  72.     columnStyles['sign2'] = { columnWidth: 162, columnHeight: 50 }
  73.     columnStyles['score2'] = { columnWidth: 50, valign: 'middle', fontSize: 12, halign: 'center' }
  74.     let i = 0;
  75.     doc.autoTable(header, content, {
  76.       theme: 'grid',
  77.       tableWidth: 'auto',
  78.       styles: {
  79.         valign: 'top'
  80.       },
  81.       columnStyles: columnStyles,
  82.       createdHeaderCell: function (cell, data) {
  83.         cell.styles.halign = 'center'
  84.       },
  85.       createdCell: function (cell, opts) {
  86.         // cell.styles.cellPadding = { bottom: Math.max(...maxHeight) }; use for dynamic size
  87.         cell.styles.cellPadding = { bottom: 50 };
  88.       },
  89.       drawCell: (cell, opts) => {
  90.         if (imgColumn.includes(opts.column.index)) {
  91.           images.push({
  92.             url: arrImgElements[i]['src'],
  93.             width: arrImgElements[i]['width'],
  94.             height: arrImgElements[i]['height'],
  95.             x: cell.textPos.x,
  96.             y: cell.textPos.y,
  97.           });
  98.           i++;
  99.         }
  100.       },
  101.       addPageContent: () => {
  102.         let colHeight = 50
  103.         let colWidth = 162
  104.         let imgHeight = 0
  105.         let imgWidth = 0
  106.         let radio = 0
  107.         for (let i = 0; i < images.length; i++) {
  108.           if (images[i].url) {
  109.             imgHeight = Number(images[i].height)
  110.             imgWidth = Number(images[i].width)
  111.             radio = Math.min(colHeight / imgHeight, colWidth / imgWidth)
  112.             images[i].width = imgWidth * radio
  113.             images[i].height = imgHeight * radio
  114.             console.log(images[i])
  115.             doc.addImage(images[i].url,
  116.               images[i].x, images[i].y - ((colHeight - imgHeight) / 4),
  117.               images[i].width,
  118.               images[i].height);
  119.           }
  120.         }
  121.       }
  122.     });
  123.     doc.save("cmasv.pdf");
  124.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement