mirror of
https://github.com/soconnor0919/hristudio.git
synced 2026-03-23 19:27:51 -04:00
feat: Implement digital signatures for participant consent and introduce study forms management.
This commit is contained in:
61
src/lib/pdf-generator.ts
Normal file
61
src/lib/pdf-generator.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
export interface PdfOptions {
|
||||
filename?: string;
|
||||
}
|
||||
|
||||
const getHtml2PdfOptions = (filename?: string) => ({
|
||||
margin: 0.5,
|
||||
filename: filename ?? 'document.pdf',
|
||||
image: { type: 'jpeg' as const, quality: 0.98 },
|
||||
html2canvas: { scale: 2, useCORS: true, backgroundColor: "#ffffff", windowWidth: 800 },
|
||||
jsPDF: { unit: 'in', format: 'letter' as const, orientation: 'portrait' as const }
|
||||
});
|
||||
|
||||
const createPrintWrapper = (htmlContent: string) => {
|
||||
const printWrapper = document.createElement("div");
|
||||
printWrapper.style.position = "absolute";
|
||||
printWrapper.style.left = "-9999px";
|
||||
printWrapper.style.top = "0px";
|
||||
printWrapper.className = "light"; // Prevent dark mode variables from bleeding into the physical PDF
|
||||
|
||||
const element = document.createElement("div");
|
||||
element.innerHTML = htmlContent;
|
||||
// Assign standard prose layout and explicitly white/black print colors
|
||||
element.className = "prose prose-sm max-w-none p-12 bg-white text-black";
|
||||
element.style.width = "800px";
|
||||
element.style.backgroundColor = "white";
|
||||
element.style.color = "black";
|
||||
|
||||
printWrapper.appendChild(element);
|
||||
document.body.appendChild(printWrapper);
|
||||
|
||||
return { printWrapper, element };
|
||||
};
|
||||
|
||||
export async function downloadPdfFromHtml(htmlContent: string, options: PdfOptions = {}): Promise<void> {
|
||||
// @ts-ignore - Dynamic import to prevent SSR issues with window/document
|
||||
const html2pdf = (await import('html2pdf.js')).default;
|
||||
|
||||
const { printWrapper, element } = createPrintWrapper(htmlContent);
|
||||
|
||||
try {
|
||||
const opt = getHtml2PdfOptions(options.filename);
|
||||
await html2pdf().set(opt).from(element).save();
|
||||
} finally {
|
||||
document.body.removeChild(printWrapper);
|
||||
}
|
||||
}
|
||||
|
||||
export async function generatePdfBlobFromHtml(htmlContent: string, options: PdfOptions = {}): Promise<Blob> {
|
||||
// @ts-ignore - Dynamic import to prevent SSR issues with window/document
|
||||
const html2pdf = (await import('html2pdf.js')).default;
|
||||
|
||||
const { printWrapper, element } = createPrintWrapper(htmlContent);
|
||||
|
||||
try {
|
||||
const opt = getHtml2PdfOptions(options.filename);
|
||||
const pdfBlob = await html2pdf().set(opt).from(element).output('blob');
|
||||
return pdfBlob;
|
||||
} finally {
|
||||
document.body.removeChild(printWrapper);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user