jermainedilao
07/10/2024, 12:08 PMcoil-compose
? Could you share some references? (perhaps a custom fetcher implementation or a forked github repository)
Just want to look for any ideas before going to the last resort of migrating all assets from PDF to SVG. 🙏Michael Paus
07/10/2024, 4:13 PMfun native_pdfBytesToImgBytes(
pdfBytes: ByteArray,
fitWidth: Double,
fitHeight: Double,
pageIndex: Int,
imageFormat: String
): ByteArray? {
// Why is this deprecated and hidden? It would be the perfect fit here.
// val pfd = ParcelFileDescriptor.fromData(pdfBytes, "PDF")
val file = File.createTempFile("scratch", "pdf")
try {
file.writeBytes(pdfBytes)
val fd = ParcelFileDescriptor.open(file, MODE_READ_ONLY)
PdfRenderer(fd).use { pdfRenderer ->
pdfRenderer.openPage(pageIndex).use { page ->
val fitScale = fitScale(page.width.toDouble(), page.height.toDouble(), fitWidth, fitHeight)
val pageWidth = (page.width * fitScale).toInt()
val pageHeight = (page.height * fitScale).toInt()
val bitmap = Bitmap.createBitmap(pageWidth, pageHeight, Bitmap.Config.ARGB_8888)
page.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY)
val inMemoryOutputStream = ByteArrayOutputStream()
bitmap.compress(imageFormat.toCompressFormat(), compQuality[imageFormat]!!, inMemoryOutputStream)
return inMemoryOutputStream.toByteArray()
}
}
} catch (e: Exception) {
log.error(e) { "Could not render PDF." }
return null
} finally {
file.delete()
}
}
jermainedilao
07/11/2024, 1:32 AMMichael Paus
07/11/2024, 6:07 AMjermainedilao
07/11/2024, 6:10 AM