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: 2547

Answers (2)

Janam Maharjan
Janam Maharjan

Reputation: 106

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);

Does it work in web hosting also ? because in my case this code is working in local host but not in web host.

Upvotes: 0

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