downloadDocument method

Future<void> downloadDocument()

Implementation

Future<void> downloadDocument() async {
  final document = pdfDocument;
  if (!enableDownloadAction || document == null) {
    return;
  }

  try {
    final data = await document.getDataDart();
    final blob = html.Blob(<Object>[data], 'application/pdf');
    final blobUrl = liPdfViewerBrowserBridge.createObjectUrlFromBlob(blob);

    final anchor = html.AnchorElement(href: blobUrl)
      ..style.display = 'none'
      ..download = downloadFileName.trim().isEmpty
          ? 'document.pdf'
          : downloadFileName.trim();

    html.document.body?.append(anchor);
    liPdfViewerBrowserBridge.clickAnchor(anchor);
    anchor.remove();

    Future<void>.delayed(const Duration(seconds: 1), () {
      liPdfViewerBrowserBridge.revokeObjectUrl(blobUrl);
    });
  } catch (error) {
    _setError('Failed to download PDF: $error');
  }
}