Commit 9bae4e07 by pantakan konthang

แก้ไข ปริ้น pdf pms ไม่ให้ติดเงา

parent 61fd2b63
...@@ -284,26 +284,84 @@ export class PmsIdpComponent { ...@@ -284,26 +284,84 @@ export class PmsIdpComponent {
// @ViewChild('pdfArea') pdfArea!: ElementRef<HTMLElement>; // @ViewChild('pdfArea') pdfArea!: ElementRef<HTMLElement>;
// async exportPdf() {
// this.pdfPrintCheck = 1
// const el = this.pdfArea?.nativeElement;
// if (!el) return;
// const mod: any = await import('html2pdf.js');
// const html2pdf = mod.default ?? mod;
// // 1) คลี่กว้างจริงแล้วสเกลลงพอดี A4
// const cleanupScale = this.expandToFullWidthThenScale(el);
// // 2) ปลดคลิป overflow/sticky ของลูก ๆ ทั้งหมด
// const cleanupUnclip = this.unclipOverflows(el);
// const prevBodyOverflow = document.body.style.overflow;
// document.body.style.overflow = 'hidden';
// // ขนาดจริงของ element (หลังถูกขยายแล้ว)
// const fullW = el.scrollWidth;
// const fullH = el.scrollHeight;
// const epsilon = 2; // กันเผื่อ
// const opt = {
// margin: [10, 10, 10, 10],
// filename: `pms-${this.evaluateeId || 'employee'}-${new Date().toISOString().slice(0, 10)}.pdf`,
// image: { type: 'jpeg', quality: 0.96 },
// html2canvas: {
// scale: 2,
// useCORS: true,
// logging: false,
// scrollX: 0,
// scrollY: -window.scrollY,
// // 👇 บอกขนาดที่จะเรนเดอร์ให้เท่าของจริงทั้งหมด
// width: 800,
// // height: fullH,
// height: Math.max(0, fullH - epsilon),
// windowWidth: 800,
// // windowHeight: fullH,
// windowHeight: Math.max(0, fullH - epsilon),
// // foreignObjectRendering: true, // ลองเปิดถ้า layout ยังเพี้ยน (มีข้อจำกัดกับบางเคส)
// },
// jsPDF: { unit: 'mm', format: 'a4', orientation: 'portrait' },
// pagebreak: { mode: ['css', 'legacy'], avoid: ['.no-break', 'table', 'img'] },
// onclone: (doc:any) => {
// doc.querySelectorAll('[data-hide-in-pdf]').forEach((el:any) => el.remove());
// }
// };
// try {
// await html2pdf().from(el).set(opt).save();
// } finally {
// cleanupUnclip();
// cleanupScale();
// document.body.style.overflow = prevBodyOverflow;
// }
// this.pdfPrintCheck = 0
// }
async exportPdf() { async exportPdf() {
this.pdfPrintCheck = 1 this.pdfPrintCheck = 1;
const el = this.pdfArea?.nativeElement; const el = this.pdfArea?.nativeElement;
if (!el) return; if (!el) return;
// 👉 ลบ shadow ออกจากทุก element ที่มี shadow class
const shadowEls = el.querySelectorAll('.shadow');
shadowEls.forEach(e => e.classList.remove('shadow'));
const mod: any = await import('html2pdf.js'); const mod: any = await import('html2pdf.js');
const html2pdf = mod.default ?? mod; const html2pdf = mod.default ?? mod;
// 1) คลี่กว้างจริงแล้วสเกลลงพอดี A4
const cleanupScale = this.expandToFullWidthThenScale(el); const cleanupScale = this.expandToFullWidthThenScale(el);
// 2) ปลดคลิป overflow/sticky ของลูก ๆ ทั้งหมด
const cleanupUnclip = this.unclipOverflows(el); const cleanupUnclip = this.unclipOverflows(el);
const prevBodyOverflow = document.body.style.overflow; const prevBodyOverflow = document.body.style.overflow;
document.body.style.overflow = 'hidden'; document.body.style.overflow = 'hidden';
// ขนาดจริงของ element (หลังถูกขยายแล้ว)
const fullW = el.scrollWidth; const fullW = el.scrollWidth;
const fullH = el.scrollHeight; const fullH = el.scrollHeight;
const epsilon = 2; // กันเผื่อ const epsilon = 2;
const opt = { const opt = {
margin: [10, 10, 10, 10], margin: [10, 10, 10, 10],
...@@ -315,20 +373,16 @@ export class PmsIdpComponent { ...@@ -315,20 +373,16 @@ export class PmsIdpComponent {
logging: false, logging: false,
scrollX: 0, scrollX: 0,
scrollY: -window.scrollY, scrollY: -window.scrollY,
// 👇 บอกขนาดที่จะเรนเดอร์ให้เท่าของจริงทั้งหมด
width: 800, width: 800,
// height: fullH,
height: Math.max(0, fullH - epsilon), height: Math.max(0, fullH - epsilon),
windowWidth: 800, windowWidth: 800,
// windowHeight: fullH,
windowHeight: Math.max(0, fullH - epsilon), windowHeight: Math.max(0, fullH - epsilon),
// foreignObjectRendering: true, // ลองเปิดถ้า layout ยังเพี้ยน (มีข้อจำกัดกับบางเคส)
}, },
jsPDF: { unit: 'mm', format: 'a4', orientation: 'portrait' }, jsPDF: { unit: 'mm', format: 'a4', orientation: 'portrait' },
pagebreak: { mode: ['css', 'legacy'], avoid: ['.no-break', 'table', 'img'] }, pagebreak: { mode: ['css', 'legacy'], avoid: ['.no-break', 'table', 'img'] },
onclone: (doc:any) => { onclone: (doc: any) => {
doc.querySelectorAll('[data-hide-in-pdf]').forEach((el:any) => el.remove()); doc.querySelectorAll('[data-hide-in-pdf]').forEach((el: any) => el.remove());
} },
}; };
try { try {
...@@ -337,9 +391,14 @@ export class PmsIdpComponent { ...@@ -337,9 +391,14 @@ export class PmsIdpComponent {
cleanupUnclip(); cleanupUnclip();
cleanupScale(); cleanupScale();
document.body.style.overflow = prevBodyOverflow; document.body.style.overflow = prevBodyOverflow;
// 👉 คืน class shadow กลับ
shadowEls.forEach(e => e.classList.add('shadow'));
} }
this.pdfPrintCheck = 0
} this.pdfPrintCheck = 0;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment