> ## Documentation Index
> Fetch the complete documentation index at: https://docs.markpdf.tech/llms.txt
> Use this file to discover all available pages before exploring further.

# Ejemplos

> Real use cases for the Flutter/Dart package: file_picker, large PDFs, BYOS, RAG and ZIPs.

# Ejemplos

## Select and convert a PDF (`file_picker`)

```dart theme={null}
import 'package:file_picker/file_picker.dart';
import 'package:markpdf/markpdf.dart';

final client = MarkpdfClient(apiKey: 'YOUR_API_KEY');

Future<String?> pickAndConvert() async {
  final result = await FilePicker.platform.pickFiles(
    type: FileType.custom,
    allowedExtensions: ['pdf'],
    withData: true, // necesario para obtener bytes en Web
  );
  if (result == null) return null;

  final picked = result.files.single;
  final convertResult = await client.convertBytes(
    picked.bytes!,
    picked.name,
    options: const ConvertOptions(mode: ConversionMode.fast),
  );

  return switch (convertResult) {
    MarkdownResult(:final markdown) => markdown,
    JsonConvertResult(:final result) => result.markdown,
    QueuedResult() => null, // no debería pasar con autoPoll: true (default)
  };
}
```

`withData: true` is necessary for `file_picker` to return the bytes in memory — essential in Flutter Web, where there is no filesystem.

## Show the converted Markdown on screen

```dart theme={null}
class ConvertScreen extends StatefulWidget {
  const ConvertScreen({super.key});

  @override
  State<ConvertScreen> createState() => _ConvertScreenState();
}

class _ConvertScreenState extends State<ConvertScreen> {
  final client = MarkpdfClient(apiKey: 'YOUR_API_KEY');
  String? markdown;
  bool loading = false;

  Future<void> convert(Uint8List bytes, String filename) async {
    setState(() => loading = true);
    try {
      final result = await client.convertBytes(bytes, filename);
      if (result is MarkdownResult) {
        setState(() => markdown = result.markdown);
      }
    } on MarkpdfException catch (e) {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text('Error: ${e.message}')),
      );
    } finally {
      setState(() => loading = false);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: loading
          ? const Center(child: CircularProgressIndicator())
          : SingleChildScrollView(
              padding: const EdgeInsets.all(16),
              child: SelectableText(markdown ?? 'Selecciona un PDF'),
            ),
    );
  }
}
```

## PDF large with page range

```dart theme={null}
final result = await client.convertFromUrl(
  'https://bucket.example.com/manual-800-papages.pdf?sig=...',
  filename: 'manual.pdf',
  options: const ConvertOptions(pages: '120-145', mode: ConversionMode.fast),
);
```

Combine it with [`pdfIndex`](/docs/sdks/flutter/reference#pdfindex) to know what range to order without downloading the entire PDF to the device.

## BYOS: upload the result directly to your storage

Generate the pre-signed URL from your backend and pass it to the client:

```dart theme={null}
final result = await client.convertFromUrl(
  pdfUrl,
  options: ConvertOptions(
    responseFormat: ResponseFormat.json,
  ),
);
```

<Note>
  `ConvertOptions` does not expose `outputUrl` directly because in mobile apps the pre-signed URL is typically generated and consumed in your backend, not on the device. If you need BYOS from the client, consider exposing your own endpoint that receives the PDF, build the pre-signed URL from your storage, and call the API with [Node.js](/docs/sdks/nodejs/examples#byos-subir-el-resultado-directo-a-tu-storage) or [Python](/docs/sdks/python/examples).
</Note>

## Pipeline RAG with `pdfIndex`

```dart theme={null}
final spine = await client.pdfIndex('https://bucket.example.com/informe-anual.pdf?sig=...');
print('${spine.pageCount} pápages, ~${spine.estimatedTokensFull} full tokens');

final target = spine.sections.firstWhere((s) => s.text.contains('Resultados'));
final following = spine.sections.where((s) => s.page > target.page).firstOrNull;
final endPage = following?.page != null ? following!.page - 1 : spine.pageCount;

final result = await client.convertFromUrl(
  'https://bucket.example.com/informe-anual.pdf?sig=...',
  options: ConvertOptions(pages: '${target.page}-$endPage', mode: ConversionMode.fast),
);
```

See [PDF Index for AI agents](/docs/concepts/pdf-index-for-ai-agents).

## Process a ZIP

```dart theme={null}
final bytes = await File('/path/documents.zip').readAsBytes();
final result = await client.convertBytes(
  bytes,
  'documents.zip',
  options: const ConvertOptions(
    inputFormat: InputFormat.zip,
    responseFormat: ResponseFormat.json,
  ),
);

if (result is JsonConvertResult) {
  print(result.result.markdown); // Markdown concatenated from the supported documents inside the ZIP
}
```

<Warning>
  The file size and quantity limits within ZIP are in [Limits](/docs/concepts/limits). An out-of-range ZIP casts `PayloadTooLargeException`.
</Warning>
