Reputation: 201
I'm using the following code to create a document and write to a file. However, when I view the created pdf file, all the views have been drawn except for images. I tried to set the layout as the content of an activity to find out if my images were corrupted, or I had to resize them, but they render flawlessly when running the app. What might be the problem in this case? Do I need to write images programmatically? Here's the code:
val view = LayoutInflater.from(context).inflate(R.layout.pdf, null)
val displayMetrics = DisplayMetrics()
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
context.display?.getRealMetrics(displayMetrics)
} else {
context.windowManager.defaultDisplay?.getMetrics(displayMetrics)
}
view.measure(
View.MeasureSpec.makeMeasureSpec(displayMetrics.widthPixels, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(displayMetrics.heightPixels, View.MeasureSpec.EXACTLY))
view.layout(0,0, displayMetrics.widthPixels, displayMetrics.heightPixels)
val document = PdfDocument()
val width = view.measuredWidth
val height = view.measuredHeight
val pageInfo = PdfDocument.PageInfo.Builder(width, height, 1).create()
val page = document.startPage(pageInfo)
val canvas = page.canvas
val bitMap = loadBitmapFromView(width, height)
canvas.drawBitmap(bitMap, 0f,0f, null)
view.draw(canvas)
document.finishPage(page)
document.writeTo(outputStream)
document.close()
}
private fun loadBitmapFromView(width: Int, height: Int): Bitmap {
val b: Bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
return b
}
Upvotes: 0
Views: 55