Ejemplos
Cargar punto final con Spring Boot
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import tech.markpdf.*;
@RestController
public class ConvertController {
private final MarkpdfClient client = new MarkpdfClient(System.getenv("MARKPDF_API_KEY"));
@PostMapping(value = "/upload", produces = "text/markdown")
public String upload(@RequestParam("file") MultipartFile file) throws Exception {
ConvertOptions options = ConvertOptions.builder()
.mode(ConvertOptions.Mode.FAST)
.build();
ConvertResult result = client.convertBytes(file.getBytes(), file.getOriginalFilename(), options);
return switch (result) {
case ConvertResult.Markdown md -> md.markdown();
case ConvertResult.Json j -> j.result().markdown();
case ConvertResult.Queued q -> throw new IllegalStateException("unexpected queued result");
};
}
}
Punto final reactivo con CompletableFuture
@PostMapping(value = "/upload-async", produces = "text/markdown")
public CompletableFuture<String> uploadAsync(@RequestParam("file") MultipartFile file) throws Exception {
return client.convertBytesAsync(file.getBytes(), file.getOriginalFilename(), ConvertOptions.defaults())
.thenApply(result -> switch (result) {
case ConvertResult.Markdown md -> md.markdown();
case ConvertResult.Json j -> j.result().markdown();
case ConvertResult.Queued q -> throw new IllegalStateException("unexpected queued result");
});
}
PDF grande con rango de páginas
ConvertOptions options = ConvertOptions.builder()
.pages("120-145")
.mode(ConvertOptions.Mode.FAST)
.build();
ConvertResult result = client.convertFromUrl(
"https://bucket.example.com/manual-800-papages.pdf?sig=...",
"manual.pdf",
options
);
pdfIndex para saber qué rango ordenar sin descargar todo el PDF del lado del cliente.
BYOS: sube el resultado directamente a tu almacenamiento
ConvertOptions options = ConvertOptions.builder()
.responseFormat(ConvertOptions.ResponseFormat.JSON)
.outputUrl(presignedPutUrl) // pre-signed PUT to your bucket
.outputHeadUrl(presignedHeadUrl) // optional: detect cache hits
.build();
ConvertResult result = client.convertFromUrl(pdfUrl, "report.pdf", options);
if (result instanceof ConvertResult.Json json) {
System.out.println(json.result().markdown()); // vacío si se usó outputUrl
}
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
String PDF = "https://bucket.example.com/informe-anual.pdf?sig=...";
// 1. Index without converting the entire document
Map<String, Object> spine = client.pdfIndex(PDF, null);
List<Map<String, Object>> sections = (List<Map<String, Object>>) spine.get("sections");
// 2. Choose the section of interest
Map<String, Object> target = sections.stream()
.filter(s -> ((String) s.get("text")).contains("Resultados"))
.findFirst()
.orElseThrow();
int targetPage = (int) target.get("page");
int pageCount = (int) spine.get("pageCount");
// 3. Bring Markdown only from that section
ConvertOptions options = ConvertOptions.builder()
.pages(targetPage + "-" + pageCount)
.mode(ConvertOptions.Mode.FAST)
.build();
ConvertResult result = client.convertFromUrl(PDF, "informe-anual.pdf", options);
Procesar un ZIP
byte[] zipBytes = Files.readAllBytes(Path.of("documents.zip"));
ConvertOptions options = ConvertOptions.builder()
.inputFormat(ConvertOptions.InputFormat.ZIP)
.responseFormat(ConvertOptions.ResponseFormat.JSON)
.build();
ConvertResult result = client.convertBytes(zipBytes, "documents.zip", options);
if (result instanceof ConvertResult.Json json) {
System.out.println(json.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.