Taseen
Taseen

Reputation: 1255

Flutter Web: Show PDF or Any File from Assets on another browser tab or window

Recently, I was working on the portfolio website where I need to show the user's CV which was in the PDF format on another tab. Since this was a standalone Web Project, I didn't want to handle PDF viewing for iOS and Android.

Upvotes: 3

Views: 2566

Answers (1)

Taseen
Taseen

Reputation: 1255

First, you need to install this package: universal_html

flutter pub add universal_html

import the package in your file

import 'package:universal_html/html.dart' as html;

And here is the method:

Future<void> showCV() async {

   var bytes = await rootBundle.load("assets/files/cv.pdf"); // location of your asset file

   final blob = html.Blob([bytes], 'application/pdf');
   final url = html.Url.createObjectUrlFromBlob(blob);
   html.window.open(url, "_blank");
   html.Url.revokeObjectUrl(url);
}

Voila !

Upvotes: 6

Related Questions