1

I was trying to use convertApi (https://www.convertapi.com/pdf-to-compress) in Flutter to compress a pdf. I managed to send a request, get a 200 response code, and extract the 'FileData' from the Json response. However, when I convert the string obtained from the Json to a Uint8List then save the file, the pdf is unreadable...

When I make the request from the website with the same pdf, everything is working fine.

Does someone have a clue of what I am doing wrong ?

    var request = new MultipartRequest("POST", url);
    request.files.add(MultipartFile.fromBytes(
          'File', 
          pdf.save(), // This is a List<int>
          contentType: MediaType("application", "pdf"),
          filename: '${FS.pdfStorage}/${task.header.taskId}.pdf'));
    var response = await Response.fromStream(await request.send());
    var compressedPdf = Uint8List.fromList(jsonDecode((response.body))["Files"][0]["FileData"].codeUnits);
istarii
  • 11
  • 2
  • Surely the returned file data in the JSON is encoded, probably in base 64. So use `base64.decode` instead getting the code units. – Richard Heap Jan 30 '20 at 17:29
  • If you planning to do conversions that return one file, please set Accept: application/octet-stream request header and save response body directly to a file. This way you will get better performance. – Jonas Jan 31 '20 at 07:08
  • Thank you both for your comments! `base64.decode` was indeed the solution. I will make sure to add the correct header now that the request is working! – istarii Jan 31 '20 at 07:45

0 Answers0