fetchPdfAsBytes method
Implementation
Future<Uint8List> fetchPdfAsBytes(String url) async {
// Step 1: Try direct fetch first
try {
final response = await http.get(Uri.parse(url));
if (response.statusCode == 200 && response.bodyBytes.isNotEmpty) {
return response.bodyBytes;
} else {
throw Exception('Direct fetch failed with status: ${response.statusCode}');
}
} catch (e) {
// Direct fetch failed, try proxy if available
if (proxyUrl != null && proxyUrl!.isNotEmpty) {
try {
final fullProxyUrl = '$proxyUrl${Uri.encodeComponent(url)}';
final response = await http.get(Uri.parse(fullProxyUrl));
if (response.statusCode == 200 && response.bodyBytes.isNotEmpty) {
return response.bodyBytes;
} else {
throw Exception('Proxy fetch failed with status: ${response.statusCode}');
}
} catch (proxyError) {
throw Exception('Both direct fetch and proxy fetch failed. Direct error: $e, Proxy error: $proxyError');
}
} else {
throw Exception('Direct fetch failed and no proxy URL provided. Error: $e');
}
}
}