Home > Software engineering >  FileSystemException (FileSystemException: Cannot open file, path =
FileSystemException (FileSystemException: Cannot open file, path =

Time:07-25

I am using pdf package for my flutter app. It works on android and ios but it throws the following error when I try to generate pdf on windows.

error snap

This is the code that generate pdf

pdf_api.file

import 'dart:io';
import 'package:open_file/open_file.dart';
import 'package:path_provider/path_provider.dart';
import 'package:pdf/widgets.dart';

class PdfApi {
  static Future<File> saveDocument({
    required String name,
    required Document pdf,
  }) async {
    final bytes = await pdf.save();

    final dir = await getApplicationDocumentsDirectory();
    final file = File('${dir.path}/$name');

    await file.writeAsBytes(bytes);

    return file;
  }

  static Future openFile(File file) async {
    final url = file.path;

    await OpenFile.open(url);
  }
}

pdf_invoice_api.dart file

import 'dart:io';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;
import 'package:pdf/widgets.dart';
import 'package:saltanat_marriage_hall/api/pdf_api.dart';

import '../models/invoice.dart';
import '../utils.dart';

class PdfInvoiceApi {
  static Future<File> generate(Invoice invoice) async {
    final pdf = Document();
    print('pdf ${pdf.document}');
    pdf.addPage(pw.MultiPage(
      build: (context) => [
        buildHeader(invoice),
        SizedBox(height: 1 * PdfPageFormat.cm),
        buildTitle(invoice),
        buildInvoice(invoice),
        Divider(),
        buildTotal(invoice),
      ],
      footer: (context) => buildFooter(invoice),
    ));

    return PdfApi.saveDocument(
        name: 'sultanat_invoice${DateTime.now().toString()}.pdf', pdf: pdf);
  }

CodePudding user response:

You missed the extension

final file = File('${dir.path}/$name.pdf');

Also remove spaces from filename.

  • Related