flutter_plugin_pdf_viewer icon indicating copy to clipboard operation
flutter_plugin_pdf_viewer copied to clipboard

does not work on non-English devices

Open da7om85 opened this issue 6 years ago • 2 comments

the pdf files does not shown on non-English devices such as Arabic language. when changing the language of android system to English it works, but in Arabic it does not work.

da7om85 avatar May 22 '19 18:05 da7om85

this code will fix the issue

import 'dart:async';
import 'dart:io';

import 'package:flutter/services.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:flutter_plugin_pdf_viewer/src/page.dart';
import 'package:path_provider/path_provider.dart';
import 'package:rxdart/rxdart.dart';

class PDFDocument {
  static const MethodChannel _channel =
      const MethodChannel('flutter_plugin_pdf_viewer');

  String _filePath;
  int count;

  static String ene2(var input) {
    String answer = input;
    answer = answer.replaceAll("١", "1");
    answer = answer.replaceAll("٢", "2");
    answer = answer.replaceAll("٣", "3");
    answer = answer.replaceAll("٤", "4");
    answer = answer.replaceAll("٥", "5");
    answer = answer.replaceAll("٦", "6");
    answer = answer.replaceAll("٧", "7");
    answer = answer.replaceAll("٨", "8");
    answer = answer.replaceAll("٩", "9");
    answer = answer.replaceAll("٠", "0");
    return answer;
  }

  /// Load a PDF File from a given File
  ///
  ///
  static Future<PDFDocument> fromFile(File f) async {
    PDFDocument document = PDFDocument();
    document._filePath = f.path;
    try {
      var pageCount =
          await _channel.invokeMethod('getNumberOfPages', {'filePath': f.path});
      pageCount = ene2(pageCount);
      document.count = document.count = int.parse(pageCount);
    } catch (e) {
      throw Exception('Error reading PDF! from file');
    }
    return document;
  }

  /// Load a PDF File from a given URL.
  /// File is saved in cache
  ///
  static Future<PDFDocument> fromURL(String url) async {
    // Download into cache
    File f = await DefaultCacheManager().getSingleFile(url);
    PDFDocument document = PDFDocument();
    document._filePath = f.path;
    try {
      var pageCount =
          await _channel.invokeMethod('getNumberOfPages', {'filePath': f.path});

      pageCount = ene2(pageCount);
      document.count = document.count = int.parse(pageCount);
    } catch (e) {
      throw Exception('Error reading PDF! from url');
    }
    return document;
  }

  /// Load a PDF File from assets folder
  ///
  ///
  static Future<PDFDocument> fromAsset(String asset) async {
    // To open from assets, you can copy them to the app storage folder, and the access them "locally"

    File file;
    try {
      var dir = await getApplicationDocumentsDirectory();
      file = File("${dir.path}/file.pdf");
      var data = await rootBundle.load(asset);
      var bytes = data.buffer.asUint8List();
      await file.writeAsBytes(bytes, flush: true);
    } catch (e) {
      throw Exception('Error parsing asset file!');
    }
    PDFDocument document = PDFDocument();
    document._filePath = file.path;
    try {
      var pageCount = await _channel
          .invokeMethod('getNumberOfPages', {'filePath': file.path});
      pageCount = ene2(pageCount);
      document.count = document.count = int.parse(pageCount);
    } catch (e) {
      throw Exception('Error reading PDF! from asset');
    }
    return document;
  }

  /// Load specific page
  ///
  /// [page] defaults to `1` and must be equal or above it
  Future<PDFPage> get({int page = 1}) async {
    assert(page > 0);
    var data = await _channel
        .invokeMethod('getPage', {'filePath': _filePath, 'pageNumber': page});
    return new PDFPage(data);
  }

  // Stream all pages
  Observable<PDFPage> getAll() {
    return Future.forEach<PDFPage>(List(count), (i) async {
      final data = await _channel
          .invokeMethod('getPage', {'filePath': _filePath, 'pageNumber': i});
      return data;
    }).asStream();
  }
}

da7om85 avatar Jun 02 '19 23:06 da7om85

cc @lohanidamodar

error

static String ene2(var input) { String answer = input; answer = answer.replaceAll("١", "1"); answer = answer.replaceAll("٢", "2"); answer = answer.replaceAll("٣", "3"); answer = answer.replaceAll("٤", "4"); answer = answer.replaceAll("٥", "5"); answer = answer.replaceAll("٦", "6"); answer = answer.replaceAll("٧", "7"); answer = answer.replaceAll("٨", "8"); answer = answer.replaceAll("٩", "9"); answer = answer.replaceAll("٠", "0"); return answer; }

da7om85 avatar Aug 21 '20 18:08 da7om85