使用Flutter打印程序包时,如何从URL打印PDF? [英] With flutter printing package, how do I print a PDF from a URL?
问题描述
我正在快速使用打印软件包: https://pub.dev/packages/printing
I am using the printing package in flutter: https://pub.dev/packages/printing
文档显示了如何从头开始创建PDF.我已经有一个URL的PDF.有没有办法找回它并与包装一起打印?还是在另一个软件包中有替代方法?
The documentation shows how to create PDFs from scratch. I already have a PDF at a URL. Is there a way to retrieve it and print it with the package? Or is there an alternate method with another package?
推荐答案
我尝试了以下方法来实现打印功能:
I have tried the following ways to achieve the printing feature:
- 直接获取远程数据字节
使用流行的软件包印刷
var data = await http.get(url);
await Printing.layoutPdf(onLayout: (_) => data.bodyBytes);
但是我发现这种方法在打印卷筒纸(POS打印机)时会出现打印区域混乱的问题
But I found that this way will have the printing area disorder issue when printing a roller size paper (POS printer)
- 共享(第一个替代)
使用sharePdf函数
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)
- 从浏览器中查看并本地打印
签出此软件包 url_launcher
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)
- 下载远程文件并保存,使用flutter_pdf_printer软件包.
使用此软件包 flutter_pdf_printer 和路径提供程序
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);
***该软件包将具有频道注册人问题,请在安装后运行 flutter clean
***
这个包装有些过时了,但是它对我来说可以用80尺寸的pdf滚轴打印.它使用Swift UIKit软件包,该软件包具有良好的兼容性.
*** this package will have channel registrant issue, please run flutter clean
after installation ***
This package is a little bit outdated, but it works for me to print with a roller 80 size pdf. It uses the Swift UIKit package, which has good compatibility.
我的方案是打印高度无限的80尺寸的pdf滚轴,该打印包不适用于cuz,因为它无法正确预览pdf.因此,我改变了最后一种打印方式,这种方式对我来说很有效.如果仅打印A4或常规尺寸的PDF,则打印包仍然是不错的选择.
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. 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.
这篇关于使用Flutter打印程序包时,如何从URL打印PDF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!