mirror of
https://github.com/arsvendg/Stirling-PDF.git
synced 2026-07-14 10:34:06 +02:00
# Description of Changes <!-- Please provide a summary of the changes, including: - What was changed - Why the change was made - Any challenges encountered Closes #(issue_number) --> --- ## Checklist ### General - [ ] I have read the [Contribution Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md) - [ ] I have read the [Stirling-PDF Developer Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/DeveloperGuide.md) (if applicable) - [ ] I have read the [How to add new languages to Stirling-PDF](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/HowToAddNewLanguage.md) (if applicable) - [ ] I have performed a self-review of my own code - [ ] My changes generate no new warnings ### Documentation - [ ] I have updated relevant docs on [Stirling-PDF's doc repo](https://github.com/Stirling-Tools/Stirling-Tools.github.io/blob/main/docs/) (if functionality has heavily changed) - [ ] I have read the section [Add New Translation Tags](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/HowToAddNewLanguage.md#add-new-translation-tags) (for new translation tags only) ### UI Changes (if applicable) - [ ] Screenshots or videos demonstrating the UI changes are attached (e.g., as comments or direct attachments in the PR) ### Testing (if applicable) - [ ] I have tested my changes locally. Refer to the [Testing Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/DeveloperGuide.md#6-testing) for more details. --------- Co-authored-by: Anthony Stirling <[email protected]>
102 lines
2.4 KiB
TypeScript
102 lines
2.4 KiB
TypeScript
import { PDFDocument, PDFFont, PDFImage } from 'pdf-lib';
|
|
import type { TFunction } from 'i18next';
|
|
import { colorPalette } from './pdfPalette';
|
|
|
|
interface StartPageParams {
|
|
doc: PDFDocument;
|
|
font: PDFFont;
|
|
fontBold: PDFFont;
|
|
marginX: number;
|
|
marginY: number;
|
|
contentWidth: number;
|
|
pageWidth: number;
|
|
pageHeight: number;
|
|
title: string;
|
|
isContinuation: boolean;
|
|
t: TFunction<'translation'>;
|
|
}
|
|
|
|
export const startReportPage = ({
|
|
doc,
|
|
font,
|
|
fontBold,
|
|
marginX,
|
|
marginY,
|
|
pageWidth,
|
|
pageHeight,
|
|
title,
|
|
isContinuation,
|
|
t,
|
|
}: StartPageParams) => {
|
|
const page = doc.addPage([pageWidth, pageHeight]);
|
|
let cursorY = pageHeight - marginY;
|
|
|
|
if (isContinuation) {
|
|
const heading = `${title} - ${t('validateSignature.report.continued', 'Continued')}`;
|
|
page.drawText(heading, {
|
|
x: marginX,
|
|
y: cursorY - 18,
|
|
size: 12,
|
|
font: fontBold,
|
|
color: colorPalette.textMuted,
|
|
});
|
|
cursorY -= 36;
|
|
}
|
|
|
|
const pageNumber = doc.getPageCount();
|
|
page.drawText(`${t('validateSignature.report.page', 'Page')} ${pageNumber}`, {
|
|
x: pageWidth - marginX - 80,
|
|
y: marginY / 2,
|
|
size: 9,
|
|
font,
|
|
color: colorPalette.textMuted,
|
|
});
|
|
|
|
page.drawText(t('validateSignature.report.footer', 'Validated via Stirling PDF'), {
|
|
x: marginX,
|
|
y: marginY / 2,
|
|
size: 9,
|
|
font,
|
|
color: colorPalette.textMuted,
|
|
});
|
|
|
|
return { page, cursorY };
|
|
};
|
|
|
|
export const createThumbnailLoader = (doc: PDFDocument) => {
|
|
const cache = new Map<string, { image: PDFImage } | null>();
|
|
|
|
return async (url: string) => {
|
|
if (cache.has(url)) {
|
|
return cache.get(url) ?? null;
|
|
}
|
|
|
|
try {
|
|
const response = await fetch(url);
|
|
const bytes = new Uint8Array(await response.arrayBuffer());
|
|
const contentType = response.headers.get('content-type') || '';
|
|
let image: PDFImage;
|
|
|
|
if (contentType.includes('png')) {
|
|
image = await doc.embedPng(bytes);
|
|
} else if (contentType.includes('jpeg') || contentType.includes('jpg')) {
|
|
image = await doc.embedJpg(bytes);
|
|
} else {
|
|
try {
|
|
image = await doc.embedPng(bytes);
|
|
} catch {
|
|
image = await doc.embedJpg(bytes);
|
|
}
|
|
}
|
|
|
|
const result = { image };
|
|
cache.set(url, result);
|
|
return result;
|
|
} catch (error) {
|
|
console.warn('[validateSignature] Failed to load thumbnail', error);
|
|
cache.set(url, null);
|
|
return null;
|
|
}
|
|
};
|
|
};
|