Flutter create Chinese character PDF file and print file in printer
我正在尝试开发一个Flutter应用程序,该应用程序可以将文档打印到纸上。 我只能使用英文字符。 有人可以帮助使其与中文一起使用吗?
这是我的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | import 'dart:convert'; import 'dart:io'; import 'dart:typed_data'; import 'dart:ui'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:pdf/pdf.dart'; import 'package:printing/printing.dart'; void main() => runApp(new MaterialApp(home: new MyApp())); class MyApp extends StatelessWidget { final shareWidget = new GlobalKey(); Future<PDFDocument> _generateDocument() async{ final pdf = new PDFDocument(deflate: zlib.encode); final page = new PDFPage(pdf, pageFormat: PDFPageFormat(216.0, 384.0)); final g = page.getGraphics(); final top = page.pageFormat.height; g.setColor(new PDFColor(0.0, 1.0, 1.0)); var font = await rootBundle.load("assets/GenYoMinTW-Heavy.ttf"); PDFTTFFont ttf = new PDFTTFFont(pdf, font); //PDFTTFFont ttf = new PDFTTFFont(pdf, (new File("assets/open-sans.ttf").readAsBytesSync() as Uint8List).buffer.asByteData()); g.setColor(new PDFColor(0.3, 0.3, 0.3)); //var encoded = utf8.encode("檯號: 1"); g.drawString(ttf, 20.0, '\\u4f60\\u597d', 10.0 * PDFPageFormat.MM, top - 10.0 * PDFPageFormat.MM); return pdf; } void _printPdf() { print("Print ..."); // final pdf = _generateDocument(); // Printing.printPdf(document: pdf); _generateDocument().then((pdf) { Printing.printPdf(document: pdf); }); } void _sharePdf() { print("Share ..."); //final pdf = _generateDocument(); // Calculate the widget center for iPad sharing popup position final RenderBox referenceBox = shareWidget.currentContext.findRenderObject(); final topLeft = referenceBox.localToGlobal(referenceBox.paintBounds.topLeft); final bottomRight = referenceBox.localToGlobal(referenceBox.paintBounds.bottomRight); final bounds = new Rect.fromPoints(topLeft, bottomRight); _generateDocument().then((pdf) { Printing.sharePdf(document: pdf, bounds: bounds); }); } @override Widget build(BuildContext context) { return new Scaffold( appBar: new AppBar( title: const Text('Printing example'), ), body: new Center( child: new Column( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: <Widget>[ new RaisedButton( child: new Text('Print Document'), onPressed: _printPdf), new RaisedButton( key: shareWidget, child: new Text('Share Document'), onPressed: _sharePdf), ], ), ), ); } } |
使用中文Unicode打印时,屏幕不会进入打印预览页面。
如果有帮助,这是我的pubspec.yaml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | name: testmanual description: A new Flutter application. # The following defines the version and build number for your application. # A version number is three numbers separated by dots, like 1.2.43 # followed by an optional build number separated by a +. # Both the version and the builder number may be overridden in flutter # build by specifying --build-name and --build-number, respectively. # Read more about versioning at semver.org. version: 1.0.0+1 environment: sdk:">=2.0.0-dev.68.0 <3.0.0" dependencies: flutter: sdk: flutter # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^0.1.2 image_picker: ^0.4.10 flutter_mailer: ^0.1.1 video_player: ^0.6.5 datetime_picker_formfield: ^0.1.3 device_calendar: ^0.0.6 connectivity: ^0.3.1 path_provider:"^0.4.0" flutter_date_picker: ^0.1.2 image_form_field: ^0.0.2 flutter_youtube:"^1.1.1" cached_network_image: ^0.4.2 qr_flutter: ^1.1.3 share: ^0.5.3 audioplayers: ^0.7.8 chewie: ^0.7.0 printing: ^1.0.5 pdf: ^1.0.6 font_awesome_flutter: ^8.1.0 dev_dependencies: flutter_test: sdk: flutter # For information on the generic Dart part of this file, see the # following page: https://www.dartlang.org/tools/pub/pubspec # The following section is specific to Flutter. flutter: # The following line ensures that the Material Icons font is # included with your application, so that you can use the icons in # the material Icons class. uses-material-design: true # To add assets to your application, add an assets section, like this: assets: - assets/ding.wav - assets/qpon_download.pdf - assets/open-sans.ttf - assets/GenYoMinTW-Heavy.ttf # An image asset can refer to one or more resolution-specific"variants", see # https://flutter.io/assets-and-images/#resolution-aware. # For details regarding adding assets from package dependencies, see # https://flutter.io/assets-and-images/#from-packages # To add custom fonts to your application, add a fonts section here, # in this"flutter" section. Each entry in this list should have a #"family" key with the font family name, and a"fonts" key with a # list giving the asset and other descriptors for the font. For # example: # fonts: # - family: Schyler # fonts: # - asset: fonts/Schyler-Regular.ttf # - asset: fonts/Schyler-Italic.ttf # style: italic # - family: Trajan Pro # fonts: # - asset: fonts/TrajanPro.ttf # - asset: fonts/TrajanPro_Bold.ttf # weight: 700 # # For details regarding fonts from package dependencies, # see https://flutter.io/custom-fonts/#from-packages |
不幸的是,PDF库似乎只能使用ASCII字符。在这里,我已附加了代码以英语生成PFD。它还可以访问移动设备的"共享"页面,因此您可以将PDF保存到另一个应用程序,或通过AirPrint之类的方法将其发送到打印机。希望这可以帮助。我也重新编写了代码。我将尽快向您的回购请求。您可能想向[PDF] [1]库的作者David PHAM-VAN提交错误报告。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | import 'dart:io'; import 'package:flutter/material.dart'; import 'package:flutter_share/flutter_share.dart'; import 'package:pdf/pdf.dart'; import 'package:path_provider/path_provider.dart'; void main() => runApp(new MaterialApp(home: new MyApp())); class MyApp extends StatelessWidget { Future<PDFDocument> _generateDocument() async{ final pdf = new PDFDocument(); final page = new PDFPage(pdf, pageFormat: PDFPageFormat(216.0, 384.0)); final top = page.pageFormat.height; final g = page.getGraphics(); final font = new PDFFont(pdf); g.setColor(new PDFColor(0.3, 0.3, 0.3)); String text ="We can only use ASCII characters"; g.drawString(font, 12.0, text, 1.0 * PDFPageFormat.mm, top-10*PDFPageFormat.mm); await _localFile.then((File file){ print("Saving local file"); file.writeAsBytesSync(pdf.save()); }); return pdf; } void _sharePDF() { print("Print ..."); _generateDocument().then((pdf) { _localPath.then((String path){ FlutterShare.share(fileUrl:"$path/pdf.pdf"); }); }); } Future<File> get _localFile async { final path = await _localPath; return File('$path/pdf.pdf'); } Future<String> get _localPath async { final directory = await getApplicationDocumentsDirectory(); return directory.path; } @override Widget build(BuildContext context) { return new Scaffold( appBar: new AppBar( title: const Text('Printing example'), ), body: new Center( child: new Column( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: <Widget>[ new RaisedButton( child: new Text('Print Document'), onPressed: _sharePDF) ], ), ), ); } } |
编辑:我不知道如何快速发送拉取请求,所以只用此代码替换main.dart即可。这也是您想要的pubspec.yaml代码/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | name: testmanual description: A new Flutter application. # The following defines the version and build number for your application. # A version number is three numbers separated by dots, like 1.2.43 # followed by an optional build number separated by a +. # Both the version and the builder number may be overridden in flutter # build by specifying --build-name and --build-number, respectively. # Read more about versioning at semver.org. version: 1.0.0+1 environment: sdk:">=2.0.0-dev.68.0 <3.0.0" dependencies: flutter: sdk: flutter # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^0.1.2 image_picker: ^0.4.10 path_provider:"^0.4.0" qr_flutter: ^1.1.3 printing: ^1.0.5 pdf: ^1.0.6 flutter_share: ^0.0.4 # flutter_mailer: ^0.1.1 # video_player: ^0.6.5 # datetime_picker_formfield: ^0.1.3 # device_calendar: ^0.0.6 # connectivity: ^0.3.1 # flutter_date_picker: ^0.1.2 # image_form_field: ^0.0.2 # flutter_youtube:"^1.1.1" # cached_network_image: ^0.4.2 # share: ^0.5.3 # audioplayers: ^0.7.8 # chewie: ^0.7.0 # font_awesome_flutter: ^8.1.0 dev_dependencies: flutter_test: sdk: flutter # For information on the generic Dart part of this file, see the # following page: https://www.dartlang.org/tools/pub/pubspec # The following section is specific to Flutter. flutter: # The following line ensures that the Material Icons font is # included with your application, so that you can use the icons in # the material Icons class. uses-material-design: true # To add assets to your application, add an assets section, like this: assets: - assets/ding.wav - assets/qpon_download.pdf - assets/open-sans.ttf - assets/GenYoMinTW-Heavy.ttf # An image asset can refer to one or more resolution-specific"variants", see # https://flutter.io/assets-and-images/#resolution-aware. # For details regarding adding assets from package dependencies, see # https://flutter.io/assets-and-images/#from-packages # To add custom fonts to your application, add a fonts section here, # in this"flutter" section. Each entry in this list should have a #"family" key with the font family name, and a"fonts" key with a # list giving the asset and other descriptors for the font. For # example: # fonts: # - family: Schyler # fonts: # - asset: fonts/Schyler-Regular.ttf # - asset: fonts/Schyler-Italic.ttf # style: italic # - family: Trajan Pro # fonts: # - asset: fonts/TrajanPro.ttf # - asset: fonts/TrajanPro_Bold.ttf # weight: 700 # # For details regarding fonts from package dependencies, # see https://flutter.io/custom-fonts/#from-packages |
编辑2:
重要!当您将PDF写入文件时,这会将其直接写入手机。例如,如果您使用的是iOS,请转到
1 | [1]: https://pub.dartlang.org/packages/pdf |
现在,它可以与dart pdf库的1.3.5版本一起使用:github.com/DavBfr/dart_pdf