pdf.save() Not working for PDF creation help!

187 views
Skip to first unread message

potato gaming

unread,
Apr 12, 2021, 3:42:47 AM4/12/21
to Flutter Development (flutter-dev)
i am reproducing a code from a pdf tutorial, which i have copied the exact codes. but i cant make it work. there is only one line that is not working. pls help! thank you!

CODE HERE 
_______
import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;
import 'package:ppmobile/installers/pdf/pdfViewer.dart';

class MyAppforPDf extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: PdfTest(),
    );
  }
}

class PdfTest extends StatelessWidget {
  final pdf = pw.Document();
  writeOnPdf() {
    pdf.addPage(
      pw.MultiPage(
        pageFormat: PdfPageFormat.a4,
        margin: pw.EdgeInsets.all(32),
        build: (pw.Context context) {
          return <pw.Widget>[
            pw.Header(
              level: 0,
              child: pw.Text("1st header"),
            ),
            pw.Paragraph(text: "TEST PDF"),
            pw.Paragraph(text: "TEST PDF"),
            pw.Paragraph(text: "TEST PDF"),
            pw.Paragraph(text: "TEST PDF"),
            pw.Paragraph(text: "TEST PDF"),
            pw.Paragraph(text: "TEST PDF"),
            pw.Header(level: 1, child: pw.Text("2nd ehader")),
            pw.Paragraph(text: "TEST PDF"),
            pw.Paragraph(text: "TEST PDF"),
            pw.Paragraph(text: "TEST PDF"),
            pw.Paragraph(text: "TEST PDF"),
            pw.Paragraph(text: "TEST PDF"),
            pw.Paragraph(text: "TEST PDF"),
            pw.Paragraph(text: "TEST PDF"),
            pw.Paragraph(text: "TEST PDF"),
            pw.Paragraph(text: "TEST PDF"),
          ];
        },
      ),
    );
  }

  Future savePdf() async {
    Directory documentDirectory = await getApplicationDocumentsDirectory();
    String documentPath = documentDirectory.path;
    File file = File("$documentPath/example.pdf");
    file.writeAsBytesSync(pdf.save());
  }

  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Container(
        width: double.infinity,
        height: double.infinity,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              "PDF TUTORIAL",
              style: TextStyle(fontSize: 34),
            )
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () async {
          writeOnPdf();
          await savePdf();

          Directory documentDirectory =
              await getApplicationDocumentsDirectory();
          String documentPath = documentDirectory.path;
          String fullPath = "$documentPath/example.pdf";

          Navigator.push(
            context,
            MaterialPageRoute(
              builder: (context) => PDFScreen(
                pathPDF: fullPath,
              ),
            ),
          );
        },
        child: Icon(Icons.save),
      ),
    );
  }
}

Suzuki Tomohiro

unread,
Apr 12, 2021, 7:35:40 AM4/12/21
to potato gaming, Flutter Development (flutter-dev)
Error message?

--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/e3eca7cf-9dd0-4edc-943e-51d536d03a7an%40googlegroups.com.

potato gaming

unread,
Apr 12, 2021, 8:50:54 PM4/12/21
to Suzuki Tomohiro, Flutter Development (flutter-dev)
oh my. so thats what is missing. Thank you guys so much!

potato gaming

unread,
Apr 13, 2021, 3:40:17 AM4/13/21
to Suzuki Tomohiro, Flutter Development (flutter-dev)
hello all, any idea on how to add image on pdf? im customizing my own pdf file. by using this tutorial. thank you!
Reply all
Reply to author
Forward
0 new messages