用抖动将图像添加到pdf

时间:2020-04-23 12:24:22

标签: pdf flutter dart pdf-generation

您好,下面的flutter代码会在视频上生成pdf文件,我需要在pdf文件中添加头像,该怎么办?我必须确保显示从网址获取的头像。当前,pdf显示一些文本,我需要做的是创建头像和在其中插入数据的表格,该怎么做?

PdfViewer.dart

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

//Classe che si occupa di mostrare il PDF a video
class PdfPreviewScreen extends StatelessWidget {

  //Path del pdf 
  PdfPreviewScreen({this.path});

  final String path;

  @override
  Widget build(BuildContext context) {
    return PDFViewerScaffold(

      path: path,
    );
  }
}

GenerazionePDF.dart

//Variabile che rappresenta il cantiere
Cantiere c;
//Variabile che rappresenta il rapporto
Rapporto rp;
//Lista dei ristoranti presenti all'interno del cantiere che si sta visualizzando
List<Ristorante> listaRistoranti=new List<Ristorante>();
//Valori salvati corrispondenti al ristorante
String ragioneSociale;
int costo = 0;
int extraPreventivo = 0;
String data = DateFormat('dd-MM-yyyy').format(DateTime.now());
var firstColor = Color(0xff5b86e5), secondColor = Color(0xff36d1dc);
final dateFormat = DateFormat("EEEE, MMMM d, yyyy 'at' h:mma");
final timeFormat = DateFormat("h:mm a");
final format = DateFormat("dd-MM-yyyy");
DateTime date;
TimeOfDay time;

final pdf = pw.Document();
Future savePdf() async{
    Directory documentDirectory = await getApplicationDocumentsDirectory();

    String documentPath = documentDirectory.path;

    File file = File("$documentPath/example.pdf");

    file.writeAsBytesSync(pdf.save());
  }

  writeOnPdf(){


    pdf.addPage(
      pw.MultiPage(
        pageFormat: PdfPageFormat.a5,
        margin: pw.EdgeInsets.all(32),

        build: (pw.Context context){
          return <pw.Widget>  [
             /* pw.Header(
              level: 0,
             child: Text("Rapporto")

            ),*/

            /*pw.Header(
               level: 0,
               child: Image.network('https://flutter-examples.com/wp-content/uploads/2019/09/blossom.jpg',width: 300, height: 200, fit: BoxFit.contain,)

            ),*/

            pw.Paragraph(
              text: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Malesuada fames ac turpis egestas sed tempus urna. Quisque sagittis purus sit amet. A arcu cursus vitae congue mauris rhoncus aenean vel elit. Ipsum dolor sit amet consectetur adipiscing elit pellentesque. Viverra justo nec ultrices dui sapien eget mi proin sed."
            ),

            pw.Paragraph(
                text: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Malesuada fames ac turpis egestas sed tempus urna. Quisque sagittis purus sit amet. A arcu cursus vitae congue mauris rhoncus aenean vel elit. Ipsum dolor sit amet consectetur adipiscing elit pellentesque. Viverra justo nec ultrices dui sapien eget mi proin sed."
            ),

            pw.Header(
              level: 1,
              child: pw.Text("Second Heading")
            ),

            pw.Paragraph(
                text: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Malesuada fames ac turpis egestas sed tempus urna. Quisque sagittis purus sit amet. A arcu cursus vitae congue mauris rhoncus aenean vel elit. Ipsum dolor sit amet consectetur adipiscing elit pellentesque. Viverra justo nec ultrices dui sapien eget mi proin sed."
            ),

            pw.Paragraph(
                text: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Malesuada fames ac turpis egestas sed tempus urna. Quisque sagittis purus sit amet. A arcu cursus vitae congue mauris rhoncus aenean vel elit. Ipsum dolor sit amet consectetur adipiscing elit pellentesque. Viverra justo nec ultrices dui sapien eget mi proin sed."
            ),

            pw.Paragraph(
                text: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Malesuada fames ac turpis egestas sed tempus urna. Quisque sagittis purus sit amet. A arcu cursus vitae congue mauris rhoncus aenean vel elit. Ipsum dolor sit amet consectetur adipiscing elit pellentesque. Viverra justo nec ultrices dui sapien eget mi proin sed."
            ),
          ];
        },


      )
    );
  }

//Funzione che si occupa dell'inserimento del ristorante all'interno del cantiere
Future<bool> generaRapporto(BuildContext context,Rapporto rptemp) async {
//Se checkInserimento è true sto inserendo un elemento in un cantierie e check inserimento è false inserisco l'elemento in un rapportino
  //print("Funzione di rapportino");
  String pathLink= await Rapporto.getTestata();
  writeOnPdf();
  await savePdf();
  Directory documentDirectory = await getApplicationDocumentsDirectory();
  String documentPath = documentDirectory.path;
  String fullPath = "$documentPath/example.pdf";
  Navigator.push(context, MaterialPageRoute(
    builder: (context) => PdfPreviewScreen(path: fullPath,)
   ));

  return true;
}

//Implementazione dell HexColor
Color hexToColor(String code) {
  return new Color(int.parse(code.substring(1, 7), radix: 16) + 0xFF000000);
}

//Funzione che permette di salvare il valore tempaneo della data
DateTime setData(String datapassing) {
  print("Sono in setData con valore: " + datapassing);
  data = datapassing;
  return DateTime.now();
}

//Classe che rappresenta la view del ristorante
class RapportoPage extends StatelessWidget {
  //Costruttore per l'utilizzo nei cantieri
  RapportoPage(this.rtemp);

  //Oggetto che rappresenta i rapportini
  final Rapporto rtemp;

  //Creazione della lista dei ristoranti
  Widget setUi(BuildContext context) {
    rp=rtemp;
    return Padding(
        padding: EdgeInsets.all(40.0),
        child: new Container(
            child: new Center(
                child: new Column(children: [
          Wrap(spacing: 50, // spacing between each of the widgets below
              children: <Widget>[
                new RaisedButton(onPressed: () {
                  Navigator.pop(context);
                })
              ]),
          new Padding(padding: EdgeInsets.only(top: 40.0)),
          new Text(
            'Rapportini',
            style: new TextStyle(color: hexToColor("#000000"), fontSize: 25.0),
          ),
          //Inserimento del textfield della ragione sociale
          new Padding(padding: EdgeInsets.only(top: 50.0)),
          /*new TextFormField(
            decoration: new InputDecoration(
              labelText: "Inserisci Ragione Sociale",
              fillColor: Colors.white,
              border: new OutlineInputBorder(
                borderRadius: new BorderRadius.circular(25.0),
                borderSide: new BorderSide(),
              ),
              //fillColor: Colors.green
            ),
            validator: (val) {
              if (val.length == 0) {
                print("La ragione sociale non può essere vuota");
              } else {
                ragioneSociale = val;
              }
            },
            onChanged: (val) {
              ragioneSociale = val;
            },
            keyboardType: TextInputType.emailAddress,
            style: new TextStyle(
              fontFamily: "Poppins",
            ),
          ),
          //Inserimento del Costo
          new Padding(padding: EdgeInsets.only(top: 30.0)),
          new TextFormField(
            decoration: new InputDecoration(
              labelText: "Inserisci il costo",
              fillColor: Colors.white,
              border: new OutlineInputBorder(
                borderRadius: new BorderRadius.circular(25.0),
                borderSide: new BorderSide(),
              ),
            ),
            validator: (val) {
              if (val.length == 0 && Verifica.verificaDouble(val.toString())) {
                print("Attenzione campo costo non corretto");
              }
            },
            onChanged: (val) {
              costo = int.parse(val);
            },
            keyboardType: TextInputType.emailAddress,
            style: new TextStyle(
              fontFamily: "Poppins",
            ),
          ),
          new Padding(padding: EdgeInsets.only(top: 30.0)),
          //inserimento della data
          new DateTimeField(
            decoration: new InputDecoration(labelText: data),
            format: format,
            onShowPicker: (context, currentValue) {
              return showDatePicker(
                  context: context,
                  firstDate: DateTime(1900),
                  initialDate: currentValue,
                  lastDate: DateTime(2100));
            },
          ),
          new Padding(padding: EdgeInsets.only(top: 40.0)),*/
          //Bottone per l'inserimento
          new NiceButton(
              radius: 40,
              padding: const EdgeInsets.all(15),
              text: "Genera rapporto",
              icon: Icons.picture_as_pdf,
              gradientColors: [secondColor, firstColor],
              onPressed: () {
                  generaRapporto(context,rtemp);
              }    
              ),
          new Padding(padding: EdgeInsets.only(top: 40.0)),
          // getListaRistoranti(),

        ]))));
  }

  //Widget che inserisce dello spazio fra altri widget
  Widget getSpace() {
    return SizedBox(height: 10);
  }

  //Widget che eseguo il build dello schermo
  @override
  Widget build(BuildContext context) {

          return Container(
            color: TemaApp.background,
            child: Scaffold(
              backgroundColor: Colors.transparent,
              body: Stack(
                children: <Widget>[
                  getSpace(),
                  getSpace(),
                  setUi(context),
                  SizedBox(
                    height: MediaQuery.of(context).padding.bottom,
                  ),
                  //getListaRistoranti(),
                ],
              ),
            ),
          );
        }
}

0 个答案:

没有答案