Slingly HTL to pdf in AEM AEM
import Component from '@utils/Component';
import Jspdf from 'jspdf';
import $ from 'jquery';
import cookieUtils from '@utils/cookie-utils';
import html2canvas from 'html2canvas';
import {
USER_COOKIE_NAME,
} from '@globals/constants';
export default class FlagtickVerification extends Component {
private selector = 'flagtick-verification';
private $component;
constructor(cmp: HTMLElement) {
super(cmp);
this.$component = $(cmp);
const editMode = this.$component.data('editmode') as string;
if (!editMode) {
this.downloadPDF();
}
}
private downloadPDF = () => {
var node = document.getElementById('main-container');
if (node != null) {
html2canvas(node)
.then((canvas) => {
const imgWidth = 208;
const pageHeight = 295;
const imgHeight = canvas.height * (imgWidth / canvas.width);
const heightLeft = imgHeight;
const personID = cookieUtils.getCookie(USER_COOKIE_NAME);
const date = new Date();
const currentYear = date.getFullYear();
const currentMonth = date.getMonth() + 1;
const currentDay = date.getDate();
const contentDataURL = canvas.toDataURL('image/png');
const pdf = new Jspdf('p', 'mm', 'a4');
pdf.setFontSize(11);
const position = 0;
pdf.addImage(contentDataURL, 'PNG', 0, position, imgWidth, imgHeight);
pdf.setTextColor(0, 86, 129);
pdf.setFontSize(8);
pdf.text('188 Tran Tan, Hoa Hiep Nam, Lien Chieu, Da Nang 550000-84 * www.flagtick.com * 84.972.2481', 44, 292);
const ext = '.pdf';
const pdfFile = `${userID}-${currentDay}${currentMonth}${currentYear}-interview-flagtick-letter${ext}`;
pdf.save(pdfFile);
})
.catch((error) => {
console.log(error);
});
}
};
}