Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unhandled Exception: RangeError (index): Index out of range: index should be less than

Tags:

flutter

dart

I am having a problem when trying to save a pdf using the "pdf_dart" library. I assume the problem is linked to the image I'm downloading from the internet to try to attach to the pdf but I'm not sure what it is.

CODE

import 'dart:io';
import 'package:app_formulario/models/Customer.dart';
import 'package:path_provider/path_provider.dart';
import 'package:pdf/pdf.dart' as pdfDart;
import 'package:pdf/widgets.dart';
import 'package:image/image.dart' as imageDart;
import 'package:flutter/material.dart' as materialDart;
import 'package:http/http.dart' as http;

class PDFBloc {
  PDFBloc() {
    final Document pdf = Document();

    http
        .get(
            "https://clsp.com.br/wp-content/uploads/2018/05/logo-clsp-500px.png")
        .then((onValue) {
      final image = pdfDart.PdfImage(
        pdf.document,
        image: onValue.bodyBytes,
        width: 300,
        height: 150,
      );

      pdf.addPage(Page(build: (Context context) {
        return Center(
          child: Image(image),
        ); // Center
      }));

      criarPDF(pdf);
    });
  }

void criarPDF(Document pdf) async {
    Directory appDocDir = await getApplicationDocumentsDirectory();
    String appDocPath = appDocDir.path;
    appDocPath = "/storage/emulated/0/Android/data/com.rwrd.app_formulario";

    final File file = File('$appDocPath/example.pdf');
    file.writeAsBytesSync(pdf.save());
  }

The error happens right when I am going to save the pdf with the image, that is, apparently the image is downloaded normally, however when inserting it in the pdf and trying to save it, the error is caused.

I don't know if the way I'm saving is the correct one, maybe that's it, but unfortunately I can't access the file folder without going all the way through it.

The error stack

E/flutter (18670): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: RangeError (index): Index out of range: index should be less than 6307: 6308
E/flutter (18670): #0      Uint8List.[]  (dart:typed_data-patch/typed_data_patch.dart:2209:7)
E/flutter (18670): #1      PdfImage._prepare 
package:pdf/src/image.dart:183
E/flutter (18670): #2      PdfObject._write 
package:pdf/src/object.dart:53
E/flutter (18670): #3      PdfOutput.write 
package:pdf/src/output.dart:69
E/flutter (18670): #4      SetMixin.forEach  (dart:collection/set.dart:148:30)
E/flutter (18670): #5      PdfDocument._write 
package:pdf/src/document.dart:168
E/flutter (18670): #6      PdfDocument.save 
package:pdf/src/document.dart:176
E/flutter (18670): #7      Document.save 
package:pdf/widgets/document.dart:71
E/flutter (18670): #8      PDFBloc.criarPDF 
package:app_formulario/blocs/pdfBloc.dart:53
E/flutter (18670): <asynchronous suspension>
E/flutter (18670): #9      new PDFBloc.<anonymous closure> 
package:app_formulario/blocs/pdfBloc.dart:35
E/flutter (18670): #10     _rootRunUnary  (dart:async/zone.dart:1134:38)
E/flutter (18670): #11     _CustomZone.runUnary  (dart:async/zone.dart:1031:19)
E/flutter (18670): #12     _FutureListener.handleValue  (dart:async/future_impl.dart:139:18)
E/flutter (18670): #13     Future._propagateToListeners.handleValueCallback  (dart:async/future_impl.dart:680:45)
E/flutter (18670): #14     Future._propagateToListeners  (dart:async/future_impl.dart:709:32)
E/flutter (18670): #15     Future._completeWithValue  (dart:async/future_impl.dart:524:5)
E/flutter (18670): #16     _AsyncAwaitCompleter.complete  (dart:async-patch/async_patch.dart:32:15)
E/flutter (18670): #17     _completeOnAsyncReturn  (dart:async-patch/async_patch.dart:290:13)
E/flutter (18670): #18     _withClient (package:http/http.dart)
E/flutter (18670): <asynchronous suspension>
E/flutter (18670): #19     get 
package:http/http.dart:46
E/flutter (18670): #20     new PDFBloc 
package:app_formulario/blocs/pdfBloc.dart:18
E/flutter (18670): #21     FormBloc._recoverCustomerInformations 
package:app_formulario/blocs/formBloc.dart:37
E/flutter (18670): #22     _rootRunUnary  (dart:async/zone.dart:1134:38)
E/flutter (18670): #23     _CustomZone.runUnary  (dart:async/zone.dart:1031:19)
E/flutter (18670): #24     _CustomZone.runUnaryGuarded  (dart:async/zone.dart:933:7)
E/flutter (18670): #25     _BufferingStreamSubscription._sendData  (dart:async/stream_impl.dart:338:11)
E/flutter (18670): #26     _DelayedData.perform  (dart:async/stream_impl.dart:593:14)
E/flutter (18670): #27     _StreamImplEvents.handleNext  (dart:async/stream_impl.dart:709:11)
E/flutter (18670): #28     _PendingEvents.schedule.<anonymous closure>  (dart:async/stream_impl.dart:669:7)
E/flutter (18670): #29     _rootRun  (dart:async/zone.dart:1122:38)
E/flutter (18670): #30     _CustomZone.run  (dart:async/zone.dart:1023:19)
E/flutter (18670): #31     _CustomZone.runGuarded  (dart:async/zone.dart:925:7)
E/flutter (18670): #32     _CustomZone.bindCallbackGuarded.<anonymous closure>  (dart:async/zone.dart:965:23)
E/flutter (18670): #33     _rootRun  (dart:async/zone.dart:1126:13)
E/flutter (18670): #34     _CustomZone.run  (dart:async/zone.dart:1023:19)
E/flutter (18670): #35     _CustomZone.runGuarded  (dart:async/zone.dart:925:7)
E/flutter (18670): #36     _CustomZone.bindCallbackGuarded.<anonymous closure>  (dart:async/zone.dart:965:23)
E/flutter (18670): #37     _microtaskLoop  (dart:async/schedule_microtask.dart:43:21)
E/flutter (18670): #38     _startMicrotaskLoop  (dart:async/schedule_microtask.dart:52:5)
E/flutter (18670):
like image 677
Richard Willian Avatar asked Nov 20 '25 06:11

Richard Willian


1 Answers

change pdf image widget to this,

final image = pdfDart.PdfImage.jpeg(
    pdf.document,
    image: onValue.bodyBytes,
);

now, your image should appear in the pdf but you will have to resize your image if you want to fix height and width.

like image 129
Bhoomin Naik Avatar answered Nov 23 '25 05:11

Bhoomin Naik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!