Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- generate(header, content) {
- let doc = new jsPDF('l', 'px', 'a3');
- let images = [];
- let eachImgRow = []
- let maxHeight = [0, 0, 0, 0, 0, 0];
- let maxWidth = [0, 0, 0, 0, 0, 0];
- let arrImgElements = [] // contain all image data
- let imgColumn = [] // contain index of image column
- let imgKeyColumn = [] // contain key on image column
- let indexTemp: number;
- let columnIndex = 0;
- // get image column index
- for (let key in content[0]) {
- if (content[0][key].hasOwnProperty('src')) {
- imgColumn = [...imgColumn, columnIndex]
- imgKeyColumn = [...imgKeyColumn, key]
- }
- columnIndex++
- }
- // get all image data
- for (const each of content) {
- indexTemp = 0;
- for (const key in each) {
- if (imgColumn.includes(indexTemp)) {
- arrImgElements = [...arrImgElements, each[key]]
- each[key] = ''
- }
- indexTemp++
- }
- }
- // split image data to image data per row
- for (let i = 0; i < arrImgElements.length / imgColumn.length; i++) {
- const imgColSize = imgColumn.length;
- if (i === 0) {
- eachImgRow = [...eachImgRow, arrImgElements.slice(0, imgColSize)]
- }
- else {
- eachImgRow = [...eachImgRow, arrImgElements.slice((i * imgColSize) + 1,
- (i * imgColSize) + (imgColSize + 1))]
- }
- }
- // //get max height and width of each of column
- // for (let objectIndex = 0; objectIndex < eachImgRow.length; objectIndex++) {
- // for (let imgIndex = 0; imgIndex < eachImgRow[objectIndex].length; imgIndex++) {
- // if (eachImgRow[objectIndex][imgIndex].clientHeight > maxHeight[imgIndex]) {
- // maxHeight[imgIndex] = eachImgRow[objectIndex][imgIndex].clientHeight;
- // }
- // if (eachImgRow[objectIndex][imgIndex].clientWidth > maxWidth[imgIndex]) {
- // maxWidth[imgIndex] = eachImgRow[objectIndex][imgIndex].clientWidth;
- // }
- // }
- // }
- let columnStyles = {} //contain max width of image column
- columnIndex = 0
- // for (const index of imgKeyColumn) { //get max width of image column
- // // columnStyles[index] = { columnWidth: maxWidth[columnIndex] } use for dynamic size
- // columnStyles[index] = { columnWidth: 110 }
- // columnIndex++
- // }
- columnStyles['id'] = { columnWidth: 30, valign: 'middle', fontSize: 12, halign: 'center' }
- columnStyles['score'] = { columnWidth: 50, valign: 'middle', fontSize: 12, halign: 'center' }
- columnStyles['bestRef1'] = { columnWidth: 162, columnHeight: 50 }
- columnStyles['sign1'] = { columnWidth: 162, columnHeight: 50 }
- columnStyles['score1'] = { columnWidth: 50, valign: 'middle', fontSize: 12, halign: 'center' }
- columnStyles['bestRef2'] = { columnWidth: 162, columnHeight: 50 }
- columnStyles['sign2'] = { columnWidth: 162, columnHeight: 50 }
- columnStyles['score2'] = { columnWidth: 50, valign: 'middle', fontSize: 12, halign: 'center' }
- let i = 0;
- doc.autoTable(header, content, {
- theme: 'grid',
- tableWidth: 'auto',
- styles: {
- valign: 'top'
- },
- columnStyles: columnStyles,
- createdHeaderCell: function (cell, data) {
- cell.styles.halign = 'center'
- },
- createdCell: function (cell, opts) {
- // cell.styles.cellPadding = { bottom: Math.max(...maxHeight) }; use for dynamic size
- cell.styles.cellPadding = { bottom: 50 };
- },
- drawCell: (cell, opts) => {
- if (imgColumn.includes(opts.column.index)) {
- images.push({
- url: arrImgElements[i]['src'],
- width: arrImgElements[i]['width'],
- height: arrImgElements[i]['height'],
- x: cell.textPos.x,
- y: cell.textPos.y,
- });
- i++;
- }
- },
- addPageContent: () => {
- let colHeight = 50
- let colWidth = 162
- let imgHeight = 0
- let imgWidth = 0
- let radio = 0
- for (let i = 0; i < images.length; i++) {
- if (images[i].url) {
- imgHeight = Number(images[i].height)
- imgWidth = Number(images[i].width)
- radio = Math.min(colHeight / imgHeight, colWidth / imgWidth)
- images[i].width = imgWidth * radio
- images[i].height = imgHeight * radio
- console.log(images[i])
- doc.addImage(images[i].url,
- images[i].x, images[i].y - ((colHeight - imgHeight) / 4),
- images[i].width,
- images[i].height);
- }
- }
- }
- });
- doc.save("cmasv.pdf");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement