[英]With flutter printing package, how do I print a PDF from a URL?
I am using the printing package in flutter: https://pub.dev/packages/printing我在 flutter 中使用打印 package: https://pub.dev/packages/printing
The documentation shows how to create PDFs from scratch.该文档展示了如何从头开始创建 PDF。 I already have a PDF at a URL.
我已经在 URL 有一个 PDF。 Is there a way to retrieve it and print it with the package?
有没有办法用 package 检索它并打印它? Or is there an alternate method with another package?
或者是否有另一个 package 的替代方法?
I have tried the following ways to achieve the printing feature:我尝试了以下方法来实现打印功能:
var data = await http.get(url);
await Printing.layoutPdf(onLayout: (_) => data.bodyBytes);
But I found that this way will have the printing area disorder issue when printing a roller size paper (POS printer)但是我发现这种方式在打印滚筒尺寸的纸张(POS打印机)时会出现打印区域混乱的问题
var data = await http.get(url);
await Printing.sharePdf(bytes: data.bodyBytes, filename: 'my-document.pdf');
This one needs an additional step to print (click the 'functions' icon of browser then print)这个需要一个额外的步骤来打印(点击浏览器的“功能”图标然后打印)
import 'package:url_launcher/url_launcher.dart';
if (await canLaunch(url)) {
await launch(url);
}
This one needs an additional step to print (click the 'functions' icon of browser then print)这个需要一个额外的步骤来打印(点击浏览器的“功能”图标然后打印)
var data = await http.get(url);
final output = await getTemporaryDirectory();
final file = File('${output.path}/your_file_name_${new DateTime.now().microsecondsSinceEpoch}.pdf');
await file.writeAsBytes(data.bodyBytes);
await FlutterPdfPrinter.printFile(file.path);
*** this package will have channel registrant issue , please run flutter clean
after installation *** *** 这个包会存在频道注册问题,请在安装后运行
flutter clean
***
This package is a little bit outdated, but it works for me to print with a roller 80 size pdf.这个包有点过时了,但它适合我用 80 尺寸的 pdf 滚筒打印。 It uses the Swift UIKit package, which has good compatibility.
它使用Swift UIKit 包,具有良好的兼容性。
My scenario is to print a roller 80 size pdf with infinite height, the printing package doesn't work for cuz it cannot preview the pdf properly.我的方案是打印具有无限高度的滚筒 80 尺寸 pdf,打印包不适用于因为它无法正确预览 pdf。 So I changed to the last way to print, which works for me.
所以我改用最后一种打印方式,这对我有用。 If you just print an A4 or a regular size PDF, the printing package is still a good choice.
如果只是打印A4或者普通尺寸的PDF,打印包还是不错的选择。
From the plugin documentation To save the pdf file using the path_provider library:从插件文档使用 path_provider 库保存 pdf 文件:
final output = await getTemporaryDirectory();
final file = File("${output.path}/example.pdf");
await file.writeAsBytes(pdf.save());
print the document using the iOS or Android print service使用 iOS 或 Android 打印服务打印文档
await Printing.layoutPdf(
onLayout: (PdfPageFormat format) async => pdf.save());
Convert your string url to URI, please make sure you are adding http将您的字符串 url 转换为 URI,请确保添加 http
http: ^0.13.4 http:^0.13.4
import 'package:http/http.dart' as http;导入“包:http/http.dart”为 http;
Uri uri = Uri.parse('Your link here');
http.Response response = await http.get(uri);
var pdfData = response.bodyBytes;
await Printing.layoutPdf(onLayout: (PdfPageFormat
format) async => pdfData);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.