### Installation Source: https://context7.com/jksalcedo/compose-to-pdf/llms.txt Instructions on how to add the ComposeToPdf library to your Android project using JitPack. ```APIDOC ## Installation Add the JitPack repository and dependency to your Android project. ```kotlin // settings.gradle.kts or build.gradle.kts (project level) repositories { maven { url = uri("https://jitpack.io") } } // app/build.gradle.kts dependencies { implementation("com.github.jksalcedo:compose-to-pdf:1.1.0") } ``` ``` -------------------------------- ### Install ComposeToPdf Dependency Source: https://context7.com/jksalcedo/compose-to-pdf/llms.txt Instructions for adding the ComposeToPdf library to an Android project using JitPack. ```kotlin repositories { maven { url = uri("https://jitpack.io") } } dependencies { implementation("com.github.jksalcedo:compose-to-pdf:1.1.0") } ``` -------------------------------- ### Generate Multi-Page PDF Report with Compose Source: https://context7.com/jksalcedo/compose-to-pdf/llms.txt This example shows how to initialize a PdfGenerator and define multiple pages using Jetpack Compose composables. It handles file I/O operations asynchronously using Kotlin coroutines and includes support for remote images via PdfAsyncImage. ```kotlin import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.foundation.border import androidx.compose.foundation.layout.* import androidx.compose.material3.* import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.unit.TextUnit import androidx.compose.ui.unit.TextUnitType import androidx.compose.ui.unit.dp import com.jksalcedo.app.* import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import java.io.File import java.io.FileOutputStream class ReportActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { val scope = rememberCoroutineScope() Button(onClick = { scope.launch(Dispatchers.IO) { val pdfGenerator = PdfGenerator(context = this@ReportActivity) val pdfPath = getExternalFilesDir("PDF") pdfPath?.let { dir -> val file = File(dir, "report.pdf") val outputStream = FileOutputStream(file) pdfGenerator.generate( outputStream = outputStream, pageSize = PdfPageSize.A4(72).orientation(Orientation.PORTRAIT), margin = 160.dp, timeout = 5000, pages = listOf( { Column(modifier = Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center) { Text("Annual Report 2025", fontSize = TextUnit(24f, TextUnitType.Sp)) } }, { Column { Text("Executive Summary", fontSize = TextUnit(18f, TextUnitType.Sp)); PdfAsyncImage(model = "https://example.com/chart.png", modifier = Modifier.fillMaxWidth().height(200.dp)) } }, { Column(modifier = Modifier.border(1.dp, Color.Black).padding(8.dp)) { Text("Q1 Results: $1.2M"); Text("Q2 Results: $1.5M") } } ) ) } }}) { Text("Generate Report") } } } } ``` -------------------------------- ### Generate PDF from Compose Content Source: https://context7.com/jksalcedo/compose-to-pdf/llms.txt Demonstrates how to use the PdfGenerator class to render multiple Composable pages into a PDF file. It handles file stream setup, page configuration, and result processing. ```kotlin val pdfGenerator = PdfGenerator(context = this) val pdfFile = File(getExternalFilesDir("PDF"), "document.pdf") val outputStream = FileOutputStream(pdfFile) val result = pdfGenerator.generate( outputStream = outputStream, pageSize = PdfPageSize.A4(72), margin = 160.dp, timeout = 3000, pages = listOf( { Text(text = "Hello, this is page 1") }, { Column { Text(text = "Page 2 Title"); Spacer(modifier = Modifier.height(16.dp)); Text(text = "Page 2 content") } } ) ) if (result.isSuccess) println("Success") else println("Failed") ``` -------------------------------- ### Generate PDF from Compose UI Source: https://github.com/jksalcedo/compose-to-pdf/blob/main/README.md Use the pdfGenerator to render a list of Composable functions into a PDF file. This example demonstrates setting page size, orientation, margins, and rendering both text and asynchronous images. ```kotlin val file = File(it, "test.pdf") val outputStream = FileOutputStream(file) val result = pdfGenerator.generate( outputStream = outputStream, pageSize = PdfPageSize.A4(72) .orientation(Orientation.LANDSCAPE), margin = 160.dp, pages = listOf({ Text( text = content.text.toString() ) }, { PdfAsyncImage( model = "https://www.pixelstalk.net/wp-content/uploads/2016/06/HD-images-of-nature-download.jpg", contentDescription = "" ) } ) ) ``` -------------------------------- ### Add ComposeToPdf Dependency Source: https://github.com/jksalcedo/compose-to-pdf/blob/main/README.md Include the library dependency in your app-level build.gradle.kts file to start using the PDF generation features. ```kotlin dependencies { implementation("com.github.jksalcedo:compose-to-pdf:1.1.0") } ``` -------------------------------- ### POST /generate Source: https://github.com/jksalcedo/compose-to-pdf/blob/main/README.md Generates a PDF document from a list of Jetpack Compose composables. ```APIDOC ## POST /generate ### Description Generates a multi-page PDF document using native Android PdfDocument based on provided Compose UI content. ### Method POST ### Endpoint /generate ### Parameters #### Request Body - **outputStream** (FileOutputStream) - Required - The stream where the PDF will be written. - **pageSize** (PdfPageSize) - Required - Defines the dimensions and DPI of the PDF pages. - **margin** (Dp) - Optional - Sets the margin for the pages. - **pages** (List<@Composable () -> Unit>) - Required - A list of composable functions representing the content of each page. ### Request Example { "pageSize": "PdfPageSize.A4(72).orientation(Orientation.LANDSCAPE)", "margin": "160.dp", "pages": ["ComposableContent1", "ComposableContent2"] } ### Response #### Success Response (200) - **result** (PdfResult) - The result object containing the status of the PDF generation. #### Response Example { "status": "success", "file": "test.pdf" } ``` -------------------------------- ### Define PDF Page Sizes and Orientations Source: https://context7.com/jksalcedo/compose-to-pdf/llms.txt Demonstrates how to instantiate standard paper sizes, custom dimensions, and apply orientation settings using the PdfPageSize data class. It supports various DPI settings for different output requirements. ```kotlin import com.jksalcedo.app.PdfPageSize import com.jksalcedo.app.Orientation val a4Portrait = PdfPageSize.A4(72) val a4Landscape = PdfPageSize.A4(72).orientation(Orientation.LANDSCAPE) val customFromInches = PdfPageSize.fromInches(widthInches = 6.0, heightInches = 9.0, dpi = 72) val fullyCustom = PdfPageSize(width = 500, height = 700, dpi = 72) ``` -------------------------------- ### Configure JitPack Repository Source: https://github.com/jksalcedo/compose-to-pdf/blob/main/README.md Add the JitPack repository to your project's build configuration to enable the download of the library from GitHub. ```kotlin repositories { maven { url = uri("https://jitpack.io") } } ``` -------------------------------- ### Generate PDF Report from Compose UI Source: https://context7.com/jksalcedo/compose-to-pdf/llms.txt This snippet demonstrates how to define PDF content using standard Compose Text components and handle the result of the PDF generation process using Kotlin coroutines and Toast notifications for user feedback. ```kotlin Text("Q4 Results: $2.1M") Spacer(modifier = Modifier.height(8.dp)) Text( text = "Total: $6.6M", fontSize = TextUnit(14f, TextUnitType.Sp) ) withContext(Dispatchers.Main) { if (result.isSuccess) { Toast.makeText( this@ReportActivity, "PDF saved: ${file.absolutePath}", Toast.LENGTH_LONG ).show() } else { Toast.makeText( this@ReportActivity, "Failed: ${result.exceptionOrNull()?.message}", Toast.LENGTH_LONG ).show() } } ``` -------------------------------- ### Render Asynchronous Images in PDF Source: https://context7.com/jksalcedo/compose-to-pdf/llms.txt Shows how to use the PdfAsyncImage component within a PdfGenerator workflow. This allows for loading images from URLs or local resources during the PDF generation process. ```kotlin pdfGenerator.generate( outputStream = outputStream, pageSize = PdfPageSize.A4(72), margin = 72.dp, pages = listOf({ PdfAsyncImage( model = "https://example.com/image.jpg", contentDescription = "A sample image", modifier = Modifier.fillMaxWidth().height(200.dp) ) }) ) ``` -------------------------------- ### PdfPageSize Configuration Source: https://context7.com/jksalcedo/compose-to-pdf/llms.txt Defines how to configure page dimensions, orientation, and resolution for PDF generation. ```APIDOC ## PdfPageSize Configuration ### Description Defines page dimensions for PDF documents using standard sizes (ISO, US, Envelopes, Business Cards) or custom dimensions. Supports DPI configuration and orientation switching. ### Usage - **Standard Sizes**: Use predefined factory methods like `PdfPageSize.A4(dpi)`. - **Custom Sizes**: Use `PdfPageSize.fromInches`, `PdfPageSize.fromMillimeters`, or the primary constructor. - **Orientation**: Call `.orientation(Orientation.LANDSCAPE)` or `.orientation(Orientation.PORTRAIT)` on an existing instance. ### Example ```kotlin val a4Landscape = PdfPageSize.A4(72).orientation(Orientation.LANDSCAPE) val custom = PdfPageSize.fromInches(6.0, 9.0, 72) ``` ``` -------------------------------- ### PdfGenerator.generate() API Source: https://context7.com/jksalcedo/compose-to-pdf/llms.txt Documentation for the `generate` function in `PdfGenerator`, the main entry point for creating PDF documents from Compose UI. ```APIDOC ## PdfGenerator.generate() The main entry point for PDF generation. This suspend function takes an output stream and a list of Composable lambdas, rendering each lambda as a separate page in the resulting PDF document. The function runs on the Main dispatcher internally and handles all view attachment/detachment automatically. ### Method `suspend fun generate( outputStream: OutputStream, pageSize: PdfPageSize, margin: Dp = 0.dp, timeout: Int = 10000, pages: List<@Composable () -> Unit> ): Result ` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None (parameters are passed directly to the function) ### Request Example ```kotlin import com.jksalcedo.app.PdfGenerator import com.jksalcedo.app.PdfPageSize import com.jksalcedo.app.Orientation import androidx.compose.material3.Text import androidx.compose.ui.unit.dp import java.io.File import java.io.FileOutputStream // Initialize the PDF generator with an Activity context val pdfGenerator = PdfGenerator(context = this) // Create output file and stream val pdfFile = File(getExternalFilesDir("PDF"), "document.pdf") val outputStream = FileOutputStream(pdfFile) // Generate PDF with multiple pages val result = pdfGenerator.generate( outputStream = outputStream, pageSize = PdfPageSize.A4(72), // A4 at 72 DPI margin = 160.dp, // 1 inch margin timeout = 3000, // 3 second timeout per page pages = listOf( { // Page 1: Simple text content Text(text = "Hello, this is page 1") }, { // Page 2: More complex layout Column { Text(text = "Page 2 Title") Spacer(modifier = Modifier.height(16.dp)) Text(text = "Page 2 content goes here") } } ) ) // Handle result when { result.isSuccess -> { println("PDF saved to: ${pdfFile.absolutePath}") println(result.getOrNull()) // "Successfully generated 2 pages!" } result.isFailure -> { println("PDF generation failed: ${result.exceptionOrNull()?.message}") } } ``` ### Response #### Success Response (200) Returns a `Result` indicating success or failure. On success, the string contains a message like "Successfully generated X pages!". #### Response Example ```json { "successMessage": "Successfully generated 2 pages!" } ``` #### Error Response Returns a `Result` with an exception on failure. The exception message will contain details about the error. #### Error Response Example ```json { "errorMessage": "Timeout occurred while generating page 1" } ``` ``` -------------------------------- ### PdfAsyncImage Component Source: https://context7.com/jksalcedo/compose-to-pdf/llms.txt Explains how to use the PdfAsyncImage composable to load and render images during PDF generation. ```APIDOC ## PdfAsyncImage ### Description A Composable function that integrates with the PDF generation system to load images asynchronously via Coil. It supports URLs and local drawable resources. ### Parameters - **model** (Any) - Required - The image source (URL string, resource ID, etc.) - **contentDescription** (String) - Optional - Accessibility description - **modifier** (Modifier) - Optional - Layout constraints - **contentScale** (ContentScale) - Optional - Scaling behavior - **clipToBound** (Boolean) - Optional - Whether to clip the image to the defined bounds ### Example ```kotlin PdfAsyncImage( model = "https://example.com/image.jpg", contentDescription = "Sample", modifier = Modifier.height(200.dp) ) ``` ``` -------------------------------- ### Set Page Orientation with Orientation Enum (Kotlin) Source: https://context7.com/jksalcedo/compose-to-pdf/llms.txt Demonstrates how to use the Orientation Enum with PdfPageSize to set page orientation to portrait or landscape. The orientation method is idempotent, meaning calling it multiple times has no additional effect. ```Kotlin import com.jksalcedo.app.Orientation import com.jksalcedo.app.PdfPageSize // Portrait orientation (taller than wide) val portrait = PdfPageSize.A4(72).orientation(Orientation.PORTRAIT) // Result: width=595, height=842 // Landscape orientation (wider than tall) val landscape = PdfPageSize.A4(72).orientation(Orientation.LANDSCAPE) // Result: width=842, height=595 // Orientation is idempotent - calling it multiple times is safe val stillLandscape = landscape.orientation(Orientation.LANDSCAPE) ``` -------------------------------- ### Orientation Enum Usage Source: https://context7.com/jksalcedo/compose-to-pdf/llms.txt Demonstrates how to use the Orientation Enum with PdfPageSize to set page orientation to portrait or landscape. ```APIDOC ## Orientation Enum An enumeration for specifying page orientation. Used with `PdfPageSize.orientation()` to switch between portrait and landscape modes. ### Usage Examples ```kotlin import com.jksalcedo.app.Orientation import com.jksalcedo.app.PdfPageSize // Portrait orientation (taller than wide) val portrait = PdfPageSize.A4(72).orientation(Orientation.PORTRAIT) // Result: width=595, height=842 // Landscape orientation (wider than tall) val landscape = PdfPageSize.A4(72).orientation(Orientation.LANDSCAPE) // Result: width=842, height=595 // Orientation is idempotent - calling it multiple times is safe val stillLandscape = landscape.orientation(Orientation.LANDSCAPE) ``` ### Enum Values - **PORTRAIT**: Represents portrait page orientation. - **LANDSCAPE**: Represents landscape page orientation. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.