Fix PDF loading: fetch PDF bytes directly and use SfPdfViewer.memory()

This commit is contained in:
Leon Bösche
2026-01-25 01:08:47 +01:00
parent 73db23f590
commit bb8cb5a23d
2 changed files with 70 additions and 9 deletions

View File

@@ -1,3 +1,4 @@
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:web/web.dart' as web;
@@ -23,6 +24,7 @@ class _PublicFileViewerState extends State<PublicFileViewer> {
String? _error;
Map<String, dynamic>? _fileData;
VideoPlayerController? _videoController;
List<int>? _pdfBytes;
@override
void initState() {
@@ -46,6 +48,11 @@ class _PublicFileViewerState extends State<PublicFileViewer> {
_isLoading = false;
});
// Load PDF bytes if it's a PDF file
if (_isPdfFile()) {
await _loadPdfBytes();
}
// Initialize video controller if it's a video file
if (_isVideoFile()) {
_initializeVideoPlayer();
@@ -58,6 +65,28 @@ class _PublicFileViewerState extends State<PublicFileViewer> {
}
}
Future<void> _loadPdfBytes() async {
if (_fileData?['viewUrl'] != null) {
try {
final apiClient = getIt<ApiClient>();
// Extract the path from viewUrl and call it directly
final viewUrl = _fileData!['viewUrl'] as String;
final uri = Uri.parse(viewUrl);
final path = uri.path + (uri.query.isNotEmpty ? '?${uri.query}' : '');
final bytes = await apiClient.getBytes(path);
setState(() {
_pdfBytes = bytes;
});
} catch (e) {
// If loading fails, we'll show an error or fallback
setState(() {
_error = 'Failed to load PDF content.';
});
}
}
}
Future<void> _initializeVideoPlayer() async {
final url = _fileData?['viewUrl'] ?? _fileData?['downloadUrl'];
if (url != null) {
@@ -115,15 +144,35 @@ class _PublicFileViewerState extends State<PublicFileViewer> {
if (viewUrl == null) return const SizedBox();
if (_isPdfFile()) {
return Expanded(
child: SfPdfViewer.network(
viewUrl,
canShowScrollHead: false,
canShowScrollStatus: false,
enableDoubleTapZooming: true,
enableTextSelection: false,
),
);
if (_pdfBytes != null) {
return Expanded(
child: SfPdfViewer.memory(
Uint8List.fromList(_pdfBytes!),
canShowScrollHead: false,
canShowScrollStatus: false,
enableDoubleTapZooming: true,
enableTextSelection: false,
),
);
} else if (_error != null) {
return Expanded(
child: Center(
child: Text(
_error!,
style: TextStyle(color: AppTheme.primaryText),
textAlign: TextAlign.center,
),
),
);
} else {
return const Expanded(
child: Center(
child: CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation<Color>(AppTheme.accentColor),
),
),
);
}
} else if (_isVideoFile() && _videoController != null) {
return Expanded(
child: AspectRatio(

View File

@@ -97,6 +97,18 @@ class ApiClient {
}
}
Future<List<int>> getBytes(String path) async {
try {
final response = await _dio.get(
path,
options: Options(responseType: ResponseType.bytes),
);
return response.data;
} on DioException catch (e) {
throw _handleError(e);
}
}
Future<Map<String, dynamic>> postRaw(String path, {dynamic data}) async {
try {
final response = await _dio.post(path, data: data);