Deploy Inicial do SteelCheck com Docker Build Automatizado
This commit is contained in:
+28
-18
@@ -2,37 +2,47 @@ import { jsPDF } from 'jspdf';
|
||||
import html2canvas from 'html2canvas';
|
||||
|
||||
export const exportAsPdf = async (elementId: string, fileName: string, action: 'preview' | 'download'): Promise<void> => {
|
||||
const element = document.getElementById(elementId);
|
||||
const container = document.getElementById(elementId);
|
||||
|
||||
if (!element) {
|
||||
if (!container) {
|
||||
console.error(`Element with id ${elementId} not found.`);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// We temporarily make the element visible to html2canvas if it's hidden by CSS
|
||||
// by cloning it or just capturing it directly (html2canvas can capture off-screen if it's positioned absolute)
|
||||
|
||||
const canvas = await html2canvas(element, {
|
||||
scale: 2, // Higher resolution
|
||||
useCORS: true,
|
||||
logging: false,
|
||||
backgroundColor: '#ffffff' // Ensure white background
|
||||
});
|
||||
const pages = container.querySelectorAll('.report-page');
|
||||
if (pages.length === 0) {
|
||||
console.error('No report pages found');
|
||||
return;
|
||||
}
|
||||
|
||||
const imgData = canvas.toDataURL('image/jpeg', 0.95);
|
||||
|
||||
// A4 dimensions in mm: 210 x 297
|
||||
try {
|
||||
const pdf = new jsPDF({
|
||||
orientation: 'portrait',
|
||||
unit: 'mm',
|
||||
format: 'a4',
|
||||
});
|
||||
|
||||
const pdfWidth = pdf.internal.pageSize.getWidth();
|
||||
const pdfHeight = (canvas.height * pdfWidth) / canvas.width;
|
||||
for (let i = 0; i < pages.length; i++) {
|
||||
const pageEl = pages[i] as HTMLElement;
|
||||
|
||||
const canvas = await html2canvas(pageEl, {
|
||||
scale: 2, // Higher resolution
|
||||
useCORS: true,
|
||||
logging: false,
|
||||
backgroundColor: '#ffffff' // Ensure white background
|
||||
});
|
||||
|
||||
pdf.addImage(imgData, 'JPEG', 0, 0, pdfWidth, pdfHeight);
|
||||
const imgData = canvas.toDataURL('image/jpeg', 0.95);
|
||||
|
||||
const pdfWidth = pdf.internal.pageSize.getWidth();
|
||||
const pdfHeight = (canvas.height * pdfWidth) / canvas.width;
|
||||
|
||||
if (i > 0) {
|
||||
pdf.addPage();
|
||||
}
|
||||
|
||||
pdf.addImage(imgData, 'JPEG', 0, 0, pdfWidth, pdfHeight);
|
||||
}
|
||||
|
||||
if (action === 'download') {
|
||||
pdf.save(`${fileName}.pdf`);
|
||||
|
||||
Reference in New Issue
Block a user