Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mixed pages of different pdf on iOS #20

Open
shabanov-krl opened this issue Oct 5, 2023 · 3 comments
Open

Mixed pages of different pdf on iOS #20

shabanov-krl opened this issue Oct 5, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@shabanov-krl
Copy link

package version: 1.0.7
ios version: 16.4

mix_pages.mov
enum _PdfState {
  loading,
  success,
  error,
}

class PdfView extends StatefulWidget {
  final Uint8List bytes;
  final VoidCallback onZoomed;
  final VoidCallback onZoomReset;

  const PdfView({
    required this.bytes,
    required this.onZoomed,
    required this.onZoomReset,
    super.key,
  });

  @override
  State<PdfView> createState() => _PdfViewState();
}

class _PdfViewState extends State<PdfView> {
  _PdfState _state = _PdfState.loading;
  late PDFDocument _document;
  late final String _cacheFileRelativePath = 'pdf_view/$hashCode.pdf';

  @override
  void initState() {
    super.initState();

    _loadDocument();
  }

  @override
  void dispose() {
    PDFDocument.clearPreviewCache();
    _clearCache();

    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    switch (_state) {
      case _PdfState.loading:
        return const Center(
          child: DocumentLoader(),
        );
      case _PdfState.success:
        return PDFViewer(
          document: _document,
          scrollDirection: Axis.vertical,
          showIndicator: true,
          showPicker: false,
          showNavigation: false,
          onZoomChanged: (scale) {
            if (scale == 1) {
              widget.onZoomReset();
            } else {
              widget.onZoomed();
            }
          },
        );
      case _PdfState.error:
        return DocumentError(
          onRetry: _loadDocument,
        );
    }
  }

  Future<void> _clearCache() async {
    final Directory cacheDirectory = await getDocumentViewerCacheDirectory();

    final file = File(path.join(
      cacheDirectory.path,
      _cacheFileRelativePath,
    ));

    if (file.existsSync()) {
      file.deleteSync();
    }
  }

  Future<File> _writeBytesToFile() async {
    final Directory cacheDirectory = await getDocumentViewerCacheDirectory();

    return File(path.join(
      cacheDirectory.path,
      _cacheFileRelativePath,
    ))
      ..createSync(recursive: true)
      ..writeAsBytesSync(widget.bytes);
  }

  Future<void> _loadDocument() async {
    try {
      setState(() {
        _state = _PdfState.loading;
      });

      final File file = await _writeBytesToFile();

      _document = await PDFDocument.fromFile(
        file,

        clearPreviewCache: false,
      );

      setState(() {
        _state = _PdfState.success;
      });
    } catch (_) {
      setState(() {
        _state = _PdfState.error;
      });
    }
  }
}
@shabanov-krl
Copy link
Author

@kaichii

@kaichii kaichii added the bug Something isn't working label Oct 7, 2023
@kaichii
Copy link
Owner

kaichii commented Oct 7, 2023

@shabanov-krl Thanks for your patience, I'll check this issue later.

@fabbyDev
Copy link

i have the same problem :( , something new?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants