Hello, I wanted to ask if there is a good way to s...
# compose-desktop
c
Hello, I wanted to ask if there is a good way to show PDF files with Compose Multiplatform projects. Thank you very much 🙂
m
Are you talking specifically for desktop or for other targets as well ?
c
Well, for all targets would be the perfect solution, of course 🙂. But also just for desktop would be interesting.
m
For desktop you can use a library from apache called pdfbox
implementation("org.apache.pdfbox:pdfbox:2.0.27")
You can render pdf files as images and display those images in your compose code. You can return a
Flow<ImageBitmap>
instead of a
List<ImageBitmap>
so you can display the images directly when it's ready instead of waiting for all images. Also if you are going to deal with large pdf files you need to render only the pages that you want and that you are displaying in the screen because this rendering is not cheap, you may also need to save the rendered images into a temp files to avoid storing that images in memory.
c
Wow, thank you very much! Do you also know a good solution for other platforms? But this really helps me for now 🙂
m
For android there's something from google called Pdf Renderer https://developer.android.com/reference/android/graphics/pdf/PdfRenderer For web you can look in npm for a library that does the same thing like this one https://www.npmjs.com/package/pdf2pic There's a similar solution for iOS as well, you need to convert that swift code to Kotlin https://www.hackingwithswift.com/example-code/core-graphics/how-to-render-a-pdf-to-an-image
m
There's also a port of pdfbox to Android. https://github.com/TomRoush/PdfBox-Android
i made a PDF viewer with pdfbox once, it was quite straightforward.
110 Views