如何在颤振中创建pdf和查看?

如何在颤振中创建pdf和查看?,pdf,flutter,dart,Pdf,Flutter,Dart,我正在尝试创建pdf并查看它 我应用了创建pdf的插件,将pdf保存到设备存储器的插件,以及 用于查看pdf的插件 我跟着。 但是如果我尝试使用import'package:pdf/widgets.dart'导入,代码中会出现错误,物料元素不工作导入“包装:颤振/material.dart” 我做错了什么 代码: 变量: final pdf = Document(); 创建pdf文件页面: return Scaffold( appBar: AppBar(title: Text("

我正在尝试创建pdf并查看它

我应用了创建pdf的插件,将pdf保存到设备存储器的插件,以及 用于查看pdf的插件

我跟着。 但是如果我尝试使用
import'package:pdf/widgets.dart'导入,代码中会出现错误,物料元素不工作
导入“包装:颤振/material.dart”

我做错了什么

代码:

变量:

final pdf = Document();
创建pdf文件页面:

return Scaffold(
      appBar: AppBar(title: Text("PDF CREATE"),
      actions: <Widget>[
        IconButton(
          icon: Icon(Icons.save),
          onPressed: () => savePdfFile(),
        )
      ],),
      body: pdf.addPage(Page(
      pageFormat: PdfPageFormat.a4,
      build: (BuildContext context) {
        return Center(
          child: Text("Hello Flutter"),
        );
      })),
    );

代码中的问题是您同时使用了材质库和PDF库。PDF插件提供的小部件在常规的Fratter框架中不起作用。您可以像示例中显示的那样使用它们构建PDF。要获取PDF文件,您需要先生成它,然后将它传递到您想要显示它的屏幕

像这样试试,对我来说很管用

        Future<File> createPDF(){
         final Document pdf = Document();
            pdf.addPage(
                //Your PDF design here with the widget system of the plugin
MultiPage(
      pageFormat:
          PdfPageFormat.letter.copyWith(marginBottom: 1.5 * PdfPageFormat.cm),
      crossAxisAlignment: CrossAxisAlignment.start,
      theme: Theme(
        tableHeader: TextStyle(fontSize: 8.0),
        tableCell: TextStyle(fontSize: 8.0),
      ),
      header: (Context context) {
        if (context.pageNumber == 1) {
          return null;
        }
        return Container(
            alignment: Alignment.centerRight,
            margin: const EdgeInsets.only(bottom: 3.0 * PdfPageFormat.mm),
            padding: const EdgeInsets.only(bottom: 3.0 * PdfPageFormat.mm),
            decoration: const BoxDecoration(
                border:
                    BoxBorder(bottom: true, width: 0.5, color: PdfColors.grey)),
            child: Text('VCR',
                style: Theme.of(context)
                    .defaultTextStyle
                    .copyWith(color: PdfColors.grey)));
      },
    );


        output = await getTemporaryDirectory();
          final file = File('${output.path}/example.pdf');
          file.writeAsBytesSync(pdf.save());
        return file;
        }
Future createPDF(){
最终文件pdf=文件();
pdf.addPage(
//您的PDF设计在这里与插件的小部件系统
多页(
页面格式:
PdfPageFormat.letter.copyWith(marginBottom:1.5*PdfPageFormat.cm),
crossAxisAlignment:crossAxisAlignment.start,
主题:主题(
tableHeader:TextStyle(fontSize:8.0),
tableCell:TextStyle(fontSize:8.0),
),
标题:(上下文){
if(context.pageNumber==1){
返回null;
}
返回容器(
对齐:alignment.centerRight,
边距:仅限常量边集(底部:3.0*PdfPageFormat.mm),
填充:仅限常量边集(底部:3.0*PdfPageFormat.mm),
装饰:康斯特盒子装饰(
边界:
BoxBorder(底部:真,宽度:0.5,颜色:PdfColors.grey)),
子:文本('VCR',
风格:主题(上下文)
.defaultTextStyle
.copyWith(颜色:PdfColors.grey));
},
);
输出=等待getTemporaryDirectory();
final file=file('${output.path}/example.pdf');
writeAsBytesSync(pdf.save());
返回文件;
}
创建PDF后,将其显示在脚手架中,如下所示:

import 'package:flutter/material.dart';
import 'package:flutter_full_pdf_viewer/full_pdf_viewer_scaffold.dart';


    class PDFScreen extends StatelessWidget {
      final String pathPDF;
      PDFScreen({this.pathPDF});

      @override
      Widget build(BuildContext context) {
        return PDFViewerScaffold(
            appBar: AppBar(
              title: Text("Document"),
              actions: <Widget>[
                IconButton(
                  icon: Icon(Icons.share),
                  onPressed: () {},
                ),
              ],
            ),
            path: pathPDF);
      }
    }
导入“包装:颤振/材料.省道”;
导入“package:flatter_full_pdf_viewer/full_pdf_viewer_scaffold.dart”;
类PDFScreen扩展了无状态小部件{
最终字符串路径PDF;
PDFScreen({this.pathPDF});
@凌驾
小部件构建(构建上下文){
返回PDFViewerScaffold(
appBar:appBar(
标题:文本(“文件”),
行动:[
图标按钮(
图标:图标(Icons.share),
按下:(){},
),
],
),
路径:pathPDF);
}
}
如果调用file.absolute.path,则从第一个函数获得的pathPDf

重要提示:函数和PDF屏幕必须在单独的文件中!!在实现生成PDF的函数的地方,不能导入“package:flatter/material.dart”


希望这对从html生成pdf文件有帮助

这里回答了这个问题,用于创建Pdf文件

使用软件包:

而不是:

之后,您可以创建PDF文件,然后使用:


查看生成的PDF文件。

那么,先生,从何处调用函数
createPDF()
意味着从哪个类?请提供更多的代码,我仍然没有完全理解。在调用Navigator函数转到PDF查看屏幕之前,请调用createPDF()并将文件路径作为导航器的参数Kay先生,我理解,但如果我不导入“package:flatter/material.dart”;然后导入build()、materialApp()、IconButton()和等将不起作用。您不应在同一文件中校准它们。请使用助手类创建一个新文件,然后在推送导航类之前调用它。我使用syncfusion\u Flatter\u pdf:^18.4.48-beta创建pdf,然后使用Flatter\u full\u pdf\u viewer:^1.0.6查看pdf。我使用syncfusion\u Flatter\u pdf:^18.4.48-beta创建pdf,然后使用flatter_full_pdf_查看器:^1.0.6查看pdf
        Future<File> createPDF(){
         final Document pdf = Document();
            pdf.addPage(
                //Your PDF design here with the widget system of the plugin
MultiPage(
      pageFormat:
          PdfPageFormat.letter.copyWith(marginBottom: 1.5 * PdfPageFormat.cm),
      crossAxisAlignment: CrossAxisAlignment.start,
      theme: Theme(
        tableHeader: TextStyle(fontSize: 8.0),
        tableCell: TextStyle(fontSize: 8.0),
      ),
      header: (Context context) {
        if (context.pageNumber == 1) {
          return null;
        }
        return Container(
            alignment: Alignment.centerRight,
            margin: const EdgeInsets.only(bottom: 3.0 * PdfPageFormat.mm),
            padding: const EdgeInsets.only(bottom: 3.0 * PdfPageFormat.mm),
            decoration: const BoxDecoration(
                border:
                    BoxBorder(bottom: true, width: 0.5, color: PdfColors.grey)),
            child: Text('VCR',
                style: Theme.of(context)
                    .defaultTextStyle
                    .copyWith(color: PdfColors.grey)));
      },
    );


        output = await getTemporaryDirectory();
          final file = File('${output.path}/example.pdf');
          file.writeAsBytesSync(pdf.save());
        return file;
        }
import 'package:flutter/material.dart';
import 'package:flutter_full_pdf_viewer/full_pdf_viewer_scaffold.dart';


    class PDFScreen extends StatelessWidget {
      final String pathPDF;
      PDFScreen({this.pathPDF});

      @override
      Widget build(BuildContext context) {
        return PDFViewerScaffold(
            appBar: AppBar(
              title: Text("Document"),
              actions: <Widget>[
                IconButton(
                  icon: Icon(Icons.share),
                  onPressed: () {},
                ),
              ],
            ),
            path: pathPDF);
      }
    }