How can I show a pdf in base64 in flutter?

2,419

I didn't test it but you have flutter package displaying pdf : flutter full pdf viewer You can find here an example of using this package with a pdf asset.

Share:
2,419
jhap
Author by

jhap

Updated on December 12, 2022

Comments

  • jhap
    jhap over 1 year

    I have the content of a pdf in base64 and I need to show it in a container in a flutter application.

    I'm trying to do it with the image, pdf and convert packages from dart,but I get an error with the image class and I don't know how to show the image.

    List<int> pdfDataBytes = base64.decode(fileContent);
      Image img = decodeImage(pdfDataBytes);
      PdfImage image = PdfImage(
        pdf,
        image: img.data.buffer.asUint8List(),
        width: img.width,
        height: img.height);
    

    This is the message of the error in the image class: Try using 'as prefix' for one of the import directives, or hiding the name from all but one of the imports

  • jhap
    jhap almost 5 years
    Thanks, but the article shows it with a pdf file in a path, I only have the content in base64
  • hawkbee
    hawkbee almost 5 years
    Can't you store your content in a file ?
  • hawkbee
    hawkbee almost 5 years
    Future<String> _createFileFromString() async { final encodedStr = "put base64 encoded string here"; Uint8List bytes = base64.decode(encodedStr); String dir = (await getApplicationDocumentsDirectory()).path; File file = File( "$dir/" + DateTime.now().millisecondsSinceEpoch.toString() + ".pdf"); await file.writeAsBytes(bytes); return file.path; }
  • jhap
    jhap almost 5 years
    where do you get the method? "The method 'getApplicationDocumentsDirectory' isn't defined for the class"
  • hawkbee
    hawkbee almost 5 years
    getApplicationDocumentsDirectory is from the package path_provider pub.dev/packages/path_provider