This commit is contained in:
@@ -0,0 +1,150 @@
|
||||
<script lang="ts">
|
||||
import { PDFDocument } from '@cantoo/pdf-lib';
|
||||
import FileDropzone from '$lib/components/FileDropzone.svelte';
|
||||
import Button from '$lib/components/ui/button.svelte';
|
||||
import { downloadBlob } from '$lib/download';
|
||||
import { Lock } from '@lucide/svelte';
|
||||
|
||||
let pdfFile: File | null = $state(null);
|
||||
let password = $state('');
|
||||
let confirmPassword = $state('');
|
||||
let allowPrinting = $state(true);
|
||||
let allowCopying = $state(true);
|
||||
let isProcessing = $state(false);
|
||||
let error = $state<string | null>(null);
|
||||
|
||||
const handleFileSelected = (files: File[]) => {
|
||||
pdfFile = files[0] ?? null;
|
||||
error = null;
|
||||
};
|
||||
|
||||
const encryptPdf = async () => {
|
||||
if (!pdfFile || !password) return;
|
||||
|
||||
if (password !== confirmPassword) {
|
||||
error = 'Passwörter stimmen nicht überein.';
|
||||
return;
|
||||
}
|
||||
|
||||
if (password.length < 1) {
|
||||
error = 'Bitte geben Sie ein Passwort ein.';
|
||||
return;
|
||||
}
|
||||
|
||||
isProcessing = true;
|
||||
error = null;
|
||||
|
||||
try {
|
||||
const bytes = new Uint8Array(await pdfFile.arrayBuffer());
|
||||
const pdfDoc = await PDFDocument.load(bytes);
|
||||
|
||||
pdfDoc.encrypt({
|
||||
userPassword: password,
|
||||
ownerPassword: crypto.randomUUID(),
|
||||
permissions: {
|
||||
printing: allowPrinting,
|
||||
copying: allowCopying
|
||||
}
|
||||
});
|
||||
|
||||
const encryptedBytes = await pdfDoc.save();
|
||||
|
||||
const baseName = pdfFile.name.replace(/\.pdf$/i, '');
|
||||
downloadBlob(encryptedBytes, `${baseName}_geschuetzt.pdf`);
|
||||
} catch (err) {
|
||||
console.error('Encryption failed:', err);
|
||||
error = 'Fehler beim Verschlüsseln des PDFs.';
|
||||
} finally {
|
||||
isProcessing = false;
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
||||
<div class="mx-auto max-w-4xl space-y-6">
|
||||
<div>
|
||||
<h1 class="text-2xl font-bold text-primary-900">Passwort setzen</h1>
|
||||
<p class="mt-2 text-sm text-primary-700">
|
||||
Verschlüsseln Sie Ihr PDF mit einem Passwort, um es vor unbefugtem Zugriff zu schützen.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{#if !pdfFile}
|
||||
<FileDropzone onFilesSelected={handleFileSelected} />
|
||||
{:else}
|
||||
<div class="space-y-6">
|
||||
<FileDropzone
|
||||
onFilesSelected={handleFileSelected}
|
||||
label="Anderes PDF ablegen"
|
||||
sublabel={pdfFile.name}
|
||||
class="py-4"
|
||||
/>
|
||||
<div class="rounded-xl border border-primary-200 bg-white p-4 space-y-4">
|
||||
<div class="flex items-center gap-2 text-primary-900">
|
||||
<Lock class="h-5 w-5" />
|
||||
<h2 class="text-lg font-semibold">{pdfFile.name}</h2>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label class="block text-sm font-medium text-primary-900 mb-1" for="encrypt-password">
|
||||
Passwort
|
||||
</label>
|
||||
<input
|
||||
id="encrypt-password"
|
||||
type="password"
|
||||
bind:value={password}
|
||||
class="w-full rounded-lg border border-primary-200 px-3 py-2 text-sm focus:border-primary focus:outline-none focus:ring-1 focus:ring-primary"
|
||||
placeholder="Passwort eingeben"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label
|
||||
class="block text-sm font-medium text-primary-900 mb-1"
|
||||
for="encrypt-password-confirmation"
|
||||
>
|
||||
Passwort bestätigen
|
||||
</label>
|
||||
<input
|
||||
id="encrypt-password-confirmation"
|
||||
type="password"
|
||||
bind:value={confirmPassword}
|
||||
class="w-full rounded-lg border border-primary-200 px-3 py-2 text-sm focus:border-primary focus:outline-none focus:ring-1 focus:ring-primary"
|
||||
placeholder="Passwort wiederholen"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div class="space-y-2">
|
||||
<p class="text-sm font-medium text-primary-900">Berechtigungen</p>
|
||||
<div class="flex items-center gap-2">
|
||||
<input
|
||||
type="checkbox"
|
||||
id="allow-printing"
|
||||
bind:checked={allowPrinting}
|
||||
class="h-4 w-4"
|
||||
/>
|
||||
<label for="allow-printing" class="text-sm text-primary-700">Drucken erlauben</label>
|
||||
</div>
|
||||
<div class="flex items-center gap-2">
|
||||
<input type="checkbox" id="allow-copying" bind:checked={allowCopying} class="h-4 w-4" />
|
||||
<label for="allow-copying" class="text-sm text-primary-700">Kopieren erlauben</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{#if error}
|
||||
<div class="rounded-lg border border-red-200 bg-red-50 p-4 text-sm text-red-800">
|
||||
{error}
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
<div class="flex justify-end gap-2">
|
||||
<Button
|
||||
onclick={encryptPdf}
|
||||
disabled={isProcessing || !password || password !== confirmPassword}
|
||||
>
|
||||
{isProcessing ? 'Wird verschlüsselt...' : 'Passwort setzen & herunterladen'}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
Reference in New Issue
Block a user