espresso3389 / pdfrx

pdfrx is yet another PDF viewer implementation that built on the top of pdfium. The plugin currently supports Android, iOS, Windows, macOS, Linux, and Web.
MIT License
60 stars 36 forks source link

Custom HTTP Headers in Network Requests #132

Open akshatshah opened 1 month ago

akshatshah commented 1 month ago

We have been trying to figure out a way to use the PdfViewer.uri widget with a link that requires authenticated headers. The current setup we have uses a separate cache mechanism to download the file and then pass it on to the PdfViewer widget. Being able to pass the headers directly to the widget would be optimal in terms of caching and performance. Any insight on this would be appreciated. I would also be willing to work on a PR for this if you're open to it!

espresso3389 commented 1 month ago

Could you please give me more info about your pains or requests? I'm always open to any such PR but I want to know more about your situation.

akshatshah commented 4 weeks ago

Essentially let's say you have a pdf link that needs a auth token to enable access for the client device. I wasn't able to find a way to pass headers to the http request.

Current Setup:

Get the file from cache (download if not available already):

  Future<Either<ApiFailure, String>> getFilePath(String url) async {
    try {
      final credentials = await _authController.getUserCredentials();
      final headers = credentials.authHeader;
      final file = await _cacheManager.getSingleFile(
        url,
        headers: headers,
      );
      return right(file.path);
    } catch (e) {
      return left(ApiFailure.platform(LocaleKeys.standardErrorMessage.tr()));
    }
  }

Pass the path to the cached file in the PdfViewer

 return PdfViewer.file(
      path,

Ideal Setup would be to pass the headers to the widget itself (or having a custom client option that can make authenticated requests). Here the headers would have a bearer token that allows the client to access the file:

 PdfViewer.uri(
  uri,
  headers: {
    'Authorization': 'Bearer <token>'
 ....

Maybe I am missing something and this feature already exists. I would love some guidance if that's the case.

espresso3389 commented 3 weeks ago

Could you please check 5717726e31dbfcaee7bba0d332c260ab62d7a095?

It introduces headers to PdfDocument.openUri function.

https://github.com/espresso3389/pdfrx/blob/5717726e31dbfcaee7bba0d332c260ab62d7a095/lib/src/pdf_api.dart#L234

espresso3389 commented 3 weeks ago

1.0.57 contains the fix above.

akshatshah commented 3 weeks ago

Hey, sorry for the delayed response. I just tried to test but there is no way to pass the headers to the PdfDocument.openUri function from the widget. I went through the code and adding links to places where headers parameter would be needed below so that we can pass it from the widget. Thank you for all your help so far!

https://github.com/espresso3389/pdfrx/blob/5717726e31dbfcaee7bba0d332c260ab62d7a095/lib/src/widgets/pdf_viewer.dart#L103

https://github.com/espresso3389/pdfrx/blob/5717726e31dbfcaee7bba0d332c260ab62d7a095/lib/src/pdf_document_ref.dart#L123

https://github.com/espresso3389/pdfrx/blob/5717726e31dbfcaee7bba0d332c260ab62d7a095/lib/src/pdf_document_ref.dart#L144

https://github.com/espresso3389/pdfrx/blob/5717726e31dbfcaee7bba0d332c260ab62d7a095/lib/src/pdf_document_ref.dart#L154