PDF Creation
The Docutain SDK for Kotlin Multiplatform comes with the ability to create searchable and non-searchable PDF documents either from a scanned document with Docutain's Scanner SDK or an imported file.
Initialization
- Follow the Getting started guide
- Initialize the Docutain SDK for Kotlin Multiplatform as described here
Create a PDF
import de.docutain.sdk.kmp.DocutainSdk
// ...
// scan or import a file
// ...
val scope = rememberCoroutineScope()
scope.launch {
val pdfFile = DocutainSdk.writePDF()
// do something with the generated PDF file
}
You can provide a target file path or file:// URL and decide whether an existing file should be overwritten:
val scope = rememberCoroutineScope()
scope.launch {
val pdfFile = DocutainSdk.writePDF(
fileUri = destinationUri,
overwrite = true
)
}
If PDF generation fails, writePDF throws DocutainException. See Error Handling for more details.
writePDF returns a file:// URL on Android and iOS.
Page format
You can specify the PDF page format by providing the appropriate value:
import de.docutain.sdk.kmp.PDFPageFormat
val scope = rememberCoroutineScope()
scope.launch {
val pdfFile = DocutainSdk.writePDF(pageFormat = PDFPageFormat.A4)
}
The following page formats are available:
| Value | Description |
|---|---|
FIT_TO_PAGES | Each page keeps the aspect ratio of the scanned image. |
A4 / A4_LANDSCAPE | A4 portrait / landscape. A4 is the KMP default. |
A5 / A5_LANDSCAPE | A5 portrait / landscape. |
LETTER / LETTER_LANDSCAPE | US Letter portrait / landscape. |
LEGAL / LEGAL_LANDSCAPE | US Legal portrait / landscape. |
The overwrite parameter determines what happens if the target file already exists. If it is true, the existing file gets overwritten. If it is false, a number is appended to the file name, e.g. TestPDF(1).pdf.
PDF Compression
You can specify a maximum file size for the PDF. If the uncompressed PDF would exceed this limit, it is compressed to fit. Compression can reduce image quality and make PDF generation take longer. Set maxSizeKB in KB; for example, 10 * 1024 for 10 MB:
val scope = rememberCoroutineScope()
scope.launch {
val pdfFile = DocutainSdk.writePDF(maxSizeKB = 10 * 1024)
}
maxSizeKB defaults to 0, which means no size limit.