Code saving file:
on<GeneratePDFFromInvoice>((event, emit) async {
final aspHeaderLogo =
await rootBundle.loadString('assets/images/asp_logo.svg');
final aspFooterSlogan =
await rootBundle.loadString('assets/images/asp_slogan.svg');
final fontBold = Font.ttf(
await rootBundle.load('fonts/Mulish/static/Mulish-Regular.ttf'));
final regFont = Font.ttf(
await rootBundle.load('fonts/Mulish/static/Mulish-Bold.ttf'));
final pw.Document pdf =
pw.Document(theme: PDFTheme(fontBold, regFont).themeData());
PDF(
pdf: pdf,
invoice: event.invoice,
aspHeaderLogo: aspHeaderLogo,
aspFooterSlogan: aspFooterSlogan)
.createPDF();
String fileName =
'${event.invoice.projectNumber}-${event.invoice.invoiceNumber} Invoice.pdf';
final Uint8List fileData = await pdf.save();
const String mimeType = 'application/pdf';
final XFile pdfFile =
XFile.fromData(fileData, mimeType: mimeType, name: fileName);
await pdfFile.saveTo(fileName);
});
Error from browser console is:
Uncaught RangeError: Offset is outside the bounds of the DataView
at DataView.getUint32 (<anonymous>)
at atE.a5A (main.dart.js:92807:5)
at Object.aJI (main.dart.js:25128:3)
at HD.U4 (main.dart.js:94596:28)
at HD.py (main.dart.js:94570:37)
at anw.$3 (main.dart.js:95329:97)
at q_.atL (main.dart.js:95196:7)
at Wp.agb (main.dart.js:95253:8)
at Wp.cm (main.dart.js:95279:22)
at Oj.cm (main.dart.js:94447:16)
The error occurs regardless of which browser I am on, but only happens when I deploy Firebase. There is no error on localhosting. The error doesn't seem to happen when I don't load a font into the PDF. Also using pdf_package link below. https://pub.dev/packages/pdf
CodePudding user response:
await rootBundle.load('assets/fonts/Mulish/static/Mulish-Bold.ttf'));
Simple type was the issue. missing 'assets' directory reference. Thanks to @Greg Fenton