Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Failing after 3m18s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Failing after 3m27s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Failing after 3m0s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Failing after 2m56s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 4m4s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 4m1s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 3m41s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 3m39s
CI / Build-Artefakte aktuell (pull_request) Failing after 1m10s
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
28 lines
1.3 KiB
JavaScript
28 lines
1.3 KiB
JavaScript
/**
|
||
* Erste Seite eines PDFs als JPEG in A4-Druckqualität (ca. 200 dpi) – im Browser,
|
||
* damit der Server nichts umwandeln muss.
|
||
*/
|
||
export async function pdfToImage(file, targetWidth = 1654) {
|
||
const [pdfjs, worker] = await Promise.all([import('pdfjs-dist'), import('pdfjs-dist/build/pdf.worker.min.mjs?url')]);
|
||
pdfjs.GlobalWorkerOptions.workerSrc = worker.default;
|
||
|
||
const pdf = await pdfjs.getDocument({ data: await file.arrayBuffer(), isEvalSupported: false, enableXfa: false }).promise;
|
||
|
||
try {
|
||
const page = await pdf.getPage(1);
|
||
const viewport = page.getViewport({ scale: targetWidth / page.getViewport({ scale: 1 }).width });
|
||
const canvas = document.createElement('canvas');
|
||
canvas.width = Math.round(viewport.width);
|
||
canvas.height = Math.round(viewport.height);
|
||
const context = canvas.getContext('2d');
|
||
context.fillStyle = '#ffffff';
|
||
context.fillRect(0, 0, canvas.width, canvas.height);
|
||
await page.render({ canvasContext: context, viewport }).promise;
|
||
|
||
const blob = await new Promise((resolve) => canvas.toBlob(resolve, 'image/jpeg', 0.9));
|
||
|
||
return new File([blob], file.name.replace(/\.pdf$/i, '') + '.jpg', { type: 'image/jpeg' });
|
||
} finally {
|
||
pdf.destroy();
|
||
}
|
||
}
|