how to show a pdf fetched from an API response in flutter?

579

Hi I got the same response as you. Here I used Dio api structure. Take this as reference. Below code work fine for me.

Future<File?> downloadPDF(String applicantId, String templateId) async {
try {
  final response = await dioClient.post('xxxxxx/pdfService/pdfOperation/downloadPDF', data:DownloadRequestDTO(applicantId, templateId), headers: <String, dynamic>{
'Content-Type': 'application/json'}, options: Options(responseType: ResponseType.bytes));
  if (response != null) {
    final Directory? appDir = Platform.isAndroid
        ? await getExternalStorageDirectory()
        : await getApplicationDocumentsDirectory();
    String tempPath = appDir!.path;
    final String fileName =
        DateTime.now().microsecondsSinceEpoch.toString() + '-' + 'akt.pdf';
    File file = new File('$tempPath/$fileName');
    if (!await file.exists()) {
      await file.create();
    }
    await file.writeAsBytes(response);
    return file;
  }
  throw DownloadException('The download failed.', response);
} catch (value) {
  if (value is DioError) {
    print(value.response);
  }
  print(value.toString());
}

}

Then use open_file package to open downloaded file

static Future<String?> openFile(String url) async {
final OpenResult result = await OpenFile.open(url);
switch (result.type) {
  case ResultType.error:
    return result.message;
  case ResultType.fileNotFound:
    return LocaleKeys.fileNotFound.tr();
  case ResultType.noAppToOpen:
    return LocaleKeys.noAppToOpen.tr();
  case ResultType.permissionDenied:
    return LocaleKeys.permissionDenied.tr();
  default:
    return null;
}}

And call this functions as below

Future<void> downloadAndOpenFile(BuildContext context) async {
try {
  File? file = await downloadPDF('1234', 'xxxxxx');
  if (file != null) {
    final String? result = await openFile(file.path.toString());
    if (result != null) {
      // Warning
    }
  }
} catch (e) {
  print(e);
}}
Share:
579
Max33
Author by

Max33

Updated on January 02, 2023

Comments

  • Max33
    Max33 about 1 year

    I am working in a project where I have to show certificate that the user finished a course, there is an URL of the API that uses the get method within a token to have acces to a pdf file, the problem is that I do not know how to show or transform that response into a pdf, using flutter, I tried to use the url_launcher dependency because in the browser shows the pdf normally, but the problem is that it I need to pass a token to that url. the second thing that i tried was to fetched the response of the api and save it in a temporal file and use flutter_pdfview dependency but it shows errors. this is how the response of the api looks like:

    %PDF-1.4
    1 0 obj
    <<
    /Title (þÿ)
    /Creator (þÿ)
    /Producer (þÿQt 5.5.1)
    /CreationDate (D:20211120205047)
    >>
    endobj
    2 0 obj
    <<
    /Type /Catalog
    /Pages 3 0 R
    >>
    endobj
    4 0 obj
    <<
    /Type /ExtGState
    /SA true
    /SM 0.02
    /ca 1.0
    /CA 1.0
    /AIS false
    

    this is what I tried:

     Future LoadPDF(APIurl)async {
        Map<String,String> Headers={
          'Content-type': 'application/json; charset=UTF-8',
          'Accept': 'application/json',
          'Authorization': 'Bearer $userToken'
        };
        final response = await http.get(Uri.parse(APIurl),headers: Headers);
        final bytes = response.bodyBytes;
        // print(response.bodyBytes);
        var dir = await getTemporaryDirectory();
        File file = File(dir.path + "/data.pdf"); 
        await file.writeAsBytes(bytes, flush: true);
        setState(() {
        loadDocument(file);
        });
        // return file;
        
      }
    
    • Igor Corradi
      Igor Corradi about 2 years
      Did you find any solution for this?
    • Suganya
      Suganya about 2 years
      Did you fixed this error. I have the same problem.
  • Max33
    Max33 over 2 years
    I tried with that package but when I run the project it shows an error: What went wrong: Execution failed for task ':app:checkDebugAarMetadata'. > Could not resolve all files for configuration ':app:debugRuntimeClasspath'. > Could not find com.github.barteksc:android-pdf-viewer:3.2.0-beta.1.