2

In my application a pdf report only opens in print preview which allows user to directly print the pdf document. Now i want to automate this to verify the pdf content.

I have got the pdf content through an api which is in base64 [did split to get only data], i tried converting to byte array after decoding but it only prints junk characters.[byte array to string] Now i have converted this data into ByteBuffer and want this to write in pdf.

ByteBuffer decodedBytes = new BASE64Decoder().decodeBufferToByteBuffer(
    new String(base64split2[1].substring(0, base64split2[1].length() - 1))
);

Can someone tell me how do i convert this decodedBytes of ByteBuffer to pdf.

Thanks

Michael
  • 41,989
  • 11
  • 82
  • 128
mk_
  • 164
  • 1
  • 14
  • write to file that buffer – josejuan Jul 19 '19 at 11:25
  • @josejuan I can write a byte to a file, but when i try to write bytebuffer i get this The method write(int) in the type FileOutputStream is not applicable for the arguments (ByteBuffer) – mk_ Jul 19 '19 at 11:35
  • then look for _"How to write a `ByteBuffer` to file"_ (nothing to do with applications, reports and pdf) see https://stackoverflow.com/help/how-to-ask – josejuan Jul 19 '19 at 11:58
  • You could use `ByteBuffer.array()` to convert to a byte array and then write that one. But I'm not sure if that is your question. – Tilman Hausherr Jul 19 '19 at 12:02
  • Or you could use `Base64.getDecoder().decode()` on your string and it will bring a byte array. Btw "new String" isn't needed on a String. – Tilman Hausherr Jul 19 '19 at 12:18
  • From reading your other question, it seems you were able to solve this. Thus please delete your question or answer it yourself, to avoid orphans. However `BASE64Decoder` is an internal class that may be gone in future versions, so you might still try the class/method mentioned in the comments. – Tilman Hausherr Jul 23 '19 at 09:41

2 Answers2

1
  byte[] decodedBytes = new BASE64Decoder().decodeBuffer(str);
        InputStream targetStream = new ByteArrayInputStream(decodedBytes);
        PDDocument document = PDDocument.load(targetStream);
        document.save("C:/test.pdf");
        FileUtils.writeByteArrayToFile(new File("C:/test.pdf"), decodedBytes);

Using above code to convert to pdf.

mk_
  • 164
  • 1
  • 14
  • You can call `PDDocument.load(decodedBytes)` directly, this is faster and will have a smaller memory footprint. (Because passing an inputStream will force PDFBox to cache - PDFBox assumes that InputStream is sequential and PDF parsing needs random access). – Tilman Hausherr Jul 24 '19 at 09:08
1

Getting blob data of pdf from API :


  Future<dynamic> getBlobdata(int pdfId) async {
    try {
      var response = await Dio().get(
        "https://www.google.com/pdf/$pdfId",
        options: Options(
          responseType: ResponseType.bytes,
          contentType: 'application/octet-stream',
        ),
      );

      var data = {"data": response.data.buffer};
      return data;
    } on DioError catch (error) {
      var data = error.response.data;
      return data;
    }
  }

Define file name and directory to save file :


String fileName = "pdf$pdfId";
final dir = await getExternalStorageDirectory();
var pdfBlob = await getBlobdata(1); // have to be in a asyn func to use await

Save Pdf :


final file = File("${dir.path}/$fileName.pdf");
await file.writeAsBytes(pdfBlob.asUint8List());

View Pdf in app :


 await OpenFile.open("${dir.path}/$fileName.pdf");