Ejemplos
Subir punto final con Ktor
import io.ktor.server.application.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.ktor.http.content.*
import tech.markpdf.*
fun Route.convertRoutes(client: MarkpdfClient) {
post("/upload") {
val multipart = call.receiveMultipart()
var bytes: ByteArray? = null
var filename = "document.pdf"
multipart.forEachPart { part ->
if (part is PartData.FileItem) {
bytes = part.streamProvider().readBytes()
filename = part.originalFileName ?: filename
}
part.dispose()
}
val result = client.convertBytes(bytes!!, filename, ConvertOptions(mode = ConversionMode.FAST))
val markdown = when (result) {
is ConvertResult.Markdown -> result.markdown
is ConvertResult.Json -> result.result.markdown
is ConvertResult.Queued -> error("unexpected queued result")
}
call.respondText(markdown, contentType = io.ktor.http.ContentType.Text.Plain)
}
}
Convertir un archivo adjunto seleccionado en Android
class ConvertViewModel(
private val client: MarkpdfClient,
private val contentResolver: ContentResolver,
) : ViewModel() {
private val _state = MutableStateFlow<UiState>(UiState.Idle)
val state: StateFlow<UiState> = _state
fun convert(uri: Uri) {
viewModelScope.launch {
_state.value = UiState.Loading
try {
val bytes = contentResolver.openInputStream(uri)!!.use { it.readBytes() }
val result = client.convertBytes(bytes, "document.pdf")
_state.value = when (result) {
is ConvertResult.Markdown -> UiState.Success(result.markdown)
is ConvertResult.Json -> UiState.Success(result.result.markdown)
is ConvertResult.Queued -> UiState.Loading
}
} catch (e: MarkpdfException) {
_state.value = UiState.Error(e.detail)
}
}
}
}
PDF grande con rango de páginas
val result = client.convertFromUrl(
url = "https://bucket.example.com/manual-800-papages.pdf?sig=...",
filename = "manual.pdf",
options = ConvertOptions(pages = "120-145", mode = ConversionMode.FAST),
)
pdfIndex para saber qué rango ordenar sin descargar todo el PDF del lado del cliente.
BYOS: sube el resultado directamente a tu almacenamiento
val result = client.convertFromUrl(
url = pdfUrl,
filename = "report.pdf",
options = ConvertOptions(
responseFormat = ResponseFormat.JSON,
outputUrl = presignedPutUrl, // pre-signed PUT to your bucket
outputHeadUrl = presignedHeadUrl, // optional: detect cache hits
),
)
outputUrl, API carga el Markdown directamente a su almacenamiento y devuelve un JSON liviano en lugar del cuerpo completo. Consulte Compresión para outputEncoding.
Canalización RAG con pdfIndex
val PDF = "https://bucket.example.com/informe-anual.pdf?sig=..."
// 1. Index without converting the entire document
val spine = client.pdfIndex(PDF)
val pageCount = spine["pageCount"]!!.jsonPrimitive.int
println("$pageCount pápages, ~${spine["estimatedTokensFull"]} full tokens")
// 2. Choose the section of interest
val sections = spine["sections"]!!.jsonArray
val target = sections.first { it.jsonObject["text"]!!.jsonPrimitive.content.contains("Resultados") }
val targetPage = target.jsonObject["page"]!!.jsonPrimitive.int
// 3. Bring Markdown only from that section
val result = client.convertFromUrl(
url = PDF,
filename = "informe-anual.pdf",
options = ConvertOptions(pages = "$targetPage-$pageCount", mode = ConversionMode.FAST),
)
Procesar un ZIP
val zipBytes = File("documents.zip").readBytes()
val result = client.convertBytes(
zipBytes, "documents.zip",
options = ConvertOptions(
inputFormat = InputFormat.ZIP,
responseFormat = ResponseFormat.JSON,
),
)
if (result is ConvertResult.Json) {
println(result.result.markdown) // Markdown concatenated from the documents inside the ZIP
}
Los límites de tamaño y cantidad de archivos dentro de ZIP están en Límites. Un ZIP fuera de rango lanza
MarkpdfException.PayloadTooLargeException.