From 277287d050243ef328614974bc6f6d13e45f84c3 Mon Sep 17 00:00:00 2001 From: yebintang <553387747@qq.com> Date: Thu, 19 Nov 2026 10:53:21 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Enhance=20file=20saving=20functiona?= =?UTF-8?q?lity=20to=20support=20PDF=20creation=20and=20conditional=20writ?= =?UTF-8?q?ing=20based=20on=20file=20extension.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../filesaver/FileSaverLauncher.web.kt | 40 ++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/sample/shared/src/webMain/kotlin/io/github/vinceglb/filekit/sample/shared/ui/screens/filesaver/FileSaverLauncher.web.kt b/sample/shared/src/webMain/kotlin/io/github/vinceglb/filekit/sample/shared/ui/screens/filesaver/FileSaverLauncher.web.kt index 5e263333..89d09c2e 100644 --- a/sample/shared/src/webMain/kotlin/io/github/vinceglb/filekit/sample/shared/ui/screens/filesaver/FileSaverLauncher.web.kt +++ b/sample/shared/src/webMain/kotlin/io/github/vinceglb/filekit/sample/shared/ui/screens/filesaver/FileSaverLauncher.web.kt @@ -9,6 +9,8 @@ import io.github.vinceglb.filekit.PlatformFile import io.github.vinceglb.filekit.dialogs.FileKitDialogException import io.github.vinceglb.filekit.dialogs.FileKitDialogSettings import io.github.vinceglb.filekit.dialogs.openFileSaver +import io.github.vinceglb.filekit.extension +import io.github.vinceglb.filekit.write import io.github.vinceglb.filekit.writeString import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CoroutineStart @@ -48,7 +50,11 @@ internal actual fun rememberFileSaverLauncher( dialogSettings = dialogSettings, ) if (file != null) { - file.writeString("FileKit web file saver sample") + if (file.extension.equals("pdf", ignoreCase = true)) { + file.write(createSamplePdf()) + } else { + file.writeString("FileKit web file saver sample") + } } onResult(file) } catch (failure: FileKitDialogException) { @@ -63,3 +69,35 @@ internal actual fun rememberFileSaverLauncher( } } } + +private fun createSamplePdf(): ByteArray { + val text = "FileKit web file saver sample" + val contentStream = "BT\n/F1 18 Tf\n72 720 Td\n($text) Tj\nET" + val objects = listOf( + "<< /Type /Catalog /Pages 2 0 R >>", + "<< /Type /Pages /Kids [3 0 R] /Count 1 >>", + "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] " + + "/Resources << /Font << /F1 4 0 R >> >> /Contents 5 0 R >>", + "<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>", + "<< /Length ${contentStream.encodeToByteArray().size} >>\nstream\n$contentStream\nendstream", + ) + val pdf = StringBuilder("%PDF-1.4\n") + val objectOffsets = mutableListOf() + + objects.forEachIndexed { index, body -> + objectOffsets += pdf.toString().encodeToByteArray().size + pdf.append("${index + 1} 0 obj\n$body\nendobj\n") + } + + val crossReferenceOffset = pdf.toString().encodeToByteArray().size + pdf.append("xref\n0 ${objects.size + 1}\n") + pdf.append("0000000000 65535 f \n") + objectOffsets.forEach { offset -> + pdf.append("${offset.toString().padStart(10, '0')} 00000 n \n") + } + pdf.append( + "trailer\n<< /Size ${objects.size + 1} /Root 1 0 R >>\n" + + "startxref\n$crossReferenceOffset\n%%EOF\n", + ) + return pdf.toString().encodeToByteArray() +}