Home > database >  Flutter create PDF from rest API
Flutter create PDF from rest API

Time:10-10

I'm have an api to get an inventory from mysql DB. It's working fine for building a listView. But for this app, I need to create a PDF with the values I get from the DB and honestly I don't know how to do this...

I'm using pdf package. It's ok for me to create the PDF but I don't know how to fill it.

This is my API call function :

Future<List> getInventory(String _refToSearch) async {
    String urlQuery = "";
    if (isFromScan == false) {
      urlQuery = "http://*****/getinventory.php";
      final res = await http.get(Uri.parse(urlQuery));
      return jsonDecode(res.body);
    } else {
      urlQuery = "http://*****/getinventorybyref.php";
      Response res =
          await Dio().get(urlQuery, queryParameters: {"ref": _refToSearch});
      return jsonDecode(res.data);
    }
  }

Thank you for your help

CodePudding user response:

I have saved PDF from Rest API using this method

  Future bioringPdf(String calType) async {
    //get pdf from link
    var dio = Dio();
    // String ManualTechnique = 'Incision depth : ${widget.result['ManualTechnique'].toString()}';
    // String FemtoTechnique = 'Tunnel depth : ${widget.result['FemtoTechnique'].toString()}';
    Response response = await dio.post(
      "https://biotechcalculators.com/apicall/bioring.php",

      data: {
        "generate_pdf": "yes",
        "thickest_model_code": "",
        "calculation_id": cal.cal_id,
        "lns_img": cal.lens_image
      },

      
      options: Options(
          responseType: ResponseType.bytes,
          followRedirects: false,
          validateStatus: (status) {
            return status < 500;
          }),
    );
    String path = await ExtStorage.getExternalStoragePublicDirectory(ExtStorage.DIRECTORY_DOWNLOADS);
    String fullPath = "$path/$calType ${widget.cal_id}.pdf";

    //write in download folder
    File file = File(fullPath);
    var status = await Permission.storage.status;
    if (!status.isGranted) {
      await Permission.storage.request();
    }
    var raf = file.openSync(mode: FileMode.write);
    raf.writeFromSync(response.data);
    await raf.close();
    pdfValidatePopop(context, "PDF have been successfully saved in device");
  }

Change Body and other data according to your requirments

  • Related