I am trying to create PDF file of A4 paper size from an android app from xml view using native PDFDocument library. I want to generate a pdf with single page that displays some information. I am facing a problem wrapping the view contents in the page correctly. I have attached the outcome with some mock data.
How can i make the view content to auto fit in the specified page size (A4 equivalent 595, 842) without overlapping one another. My implementation
val pdfDocument = PdfDocument()
val pageInfo =
PdfDocument.PageInfo.Builder(595, 842, 1)
.create()
val page = pdfDocument.startPage(pageInfo)
val pageCanvas = page.canvas
val pageWidth = pageCanvas.width
val pageHeight = pageCanvas.height
val measuredWidth = View.MeasureSpec.makeMeasureSpec(pageWidth,
View.MeasureSpec.EXACTLY)
val measuredHeight = View.MeasureSpec.makeMeasureSpec(pageHeight, View.MeasureSpec.EXACTLY)
view.measure(measuredWidth, measuredHeight)
view.layout(0, 0, pageWidth, pageHeight)
view.draw(pageCanvas)
pdfDocument.finishPage(page)