title: Generating PDF file with Images in flutter
body: <p>I need some help please... I am trying to generate a PDF file (text &amp; Images) using flutter, so I used the PDF package <code>pdf: ^3.3.0</code>, the text is shown once I generated the PDF file but every time I try to insert an image the below error is showing...even the image is loading in the main screen...
  the error is <a href="https://i.stack.imgur.com/rCzdX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rCzdX.png" alt="enter image description here" /></a></p>
  <p>my code is as the following:</p>
  <pre><code>import 'dart:io';
  import 'dart:typed_data';
  
  import 'package:flutter/cupertino.dart';
  import 'package:flutter/material.dart';
  import 'package:flutter/services.dart';
  import 'package:path_provider/path_provider.dart';
  import 'package:pdf/pdf.dart';
  import 'package:pdf/widgets.dart' as pw;
  import 'package:duct_sizer/pdf_preview_screen.dart';
  
  class MyHomePage extends StatelessWidget {
    final pdf = pw.Document();
    final image = pw.MemoryImage(File('images/duct.jpg').readAsBytesSync());
  
    writeOnPdf() async {
      // final profileImage = pw.MemoryImage(
      //   (await rootBundle.load('images/duct.jpg')).buffer.asUint8List(),
      // );
  
      pdf.addPage(pw.MultiPage(
        pageFormat: PdfPageFormat.a5,
        margin: pw.EdgeInsets.all(32),
        build: (pw.Context context) {
          return &lt;pw.Widget&gt;[
            pw.Header(level: 0, child: pw.Text(&quot;Easy Approach Document&quot;)),
            pw.Paragraph(
                text:
                    &quot;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.&quot;),
            pw.Paragraph(
                text:
                    &quot;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.&quot;),
            pw.Header(level: 1, child: pw.Text(&quot;Second Heading&quot;)),
            pw.Image(image),
            pw.Paragraph(
                text:
                    &quot;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.&quot;),
            pw.Paragraph(
                text:
                    &quot;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.&quot;),
            pw.Paragraph(
                text:
                    &quot;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.&quot;),
          ];
        },
      ));
    }
  
    Future savePdf() async {
      final bytes = await pdf.save();
  
      Directory documentDirectory = await getApplicationDocumentsDirectory();
  
      String documentPath = documentDirectory.path;
  
      File file = File(&quot;$documentPath/example.pdf&quot;);
  
      file.writeAsBytesSync(bytes);
    }
  
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(
          title: Text(&quot;PDF Flutter&quot;),
        ),
  
        body: Container(
          width: double.infinity,
          height: double.infinity,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: &lt;Widget&gt;[
              Text(
                &quot;PDF TUTORIAL&quot;,
                style: TextStyle(fontSize: 34),
              ),
              Image(
                image: AssetImage('images/header.png'),
              ),
            ],
          ),
        ),
  
        floatingActionButton: FloatingActionButton(
          onPressed: () async {
            writeOnPdf();
            await savePdf();
  
            Directory documentDirectory =
                await getApplicationDocumentsDirectory();
  
            String documentPath = documentDirectory.path;
  
            String fullPath = &quot;$documentPath/example.pdf&quot;;
            print(fullPath);
  
            Navigator.push(
                context,
                MaterialPageRoute(
                    builder: (context) =&gt; PDFPreviewScreen(
                          path: fullPath,
                        )));
          },
          child: Icon(Icons.save),
        ), // This trailing comma makes auto-formatting nicer for build methods.
      );
    }
  }
  </code></pre>
  <p>and the PDF preview page is</p>
  <pre><code>import 'package:flutter/material.dart';
  import 'package:flutter_full_pdf_viewer/flutter_full_pdf_viewer.dart';
  import 'package:flutter_full_pdf_viewer/full_pdf_viewer_plugin.dart';
  import 'package:flutter_full_pdf_viewer/full_pdf_viewer_scaffold.dart';
  
  class PDFPreviewScreen extends StatelessWidget {
    final String path;
  
    PDFPreviewScreen({this.path});
  
    @override
    Widget build(BuildContext context) {
      return PDFViewerScaffold(
        path: path,
      );
    }
  }
  </code></pre>
  <p>please assets me</p>
  <p>This is a new update on the pubspec.yaml file which shows the dependencies &amp; the images identifications
  <a href="https://i.stack.imgur.com/eBWPP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/eBWPP.png" alt="enter image description here" /></a>
  also As I said earlier the photo is displayed in the main App direct, but the the PDF this error will happen</p>