'Android convert pdf pages to images error: java.io.IOException: cannot create document: File not in PDF format or corrupted

I'm developing an app to display pdf documents received from server using this library:

https://github.com/barteksc/AndroidPdfViewer

Everything works fine, I can render the pdf using input streams like below:

reportsViewModel.inputStreamResponse.observe(getViewLifecycleOwner(), _inputStream -> {
            pdfView.fromStream(_inputStream).load();
            progressDialog.dismiss();

            PdiToImage.convert(requireContext(), _inputStream);
        });

Now I want to convert each pdf pages to images, I tried something like this:

private static Bitmap bitmap = null;

    public static Bitmap convert(Context context, InputStream inputStream) {
        PdfiumCore pdfiumCore = new PdfiumCore(context);
        try {
            PdfDocument document = pdfiumCore.newDocument(new byte[inputStream.available()]);
            final int pageCount = pdfiumCore.getPageCount(document);
            for (int index = 0; index < pageCount; index++) {
                pdfiumCore.openPage(document, index);

                int width = pdfiumCore.getPageWidthPoint(document, index);
                int height = pdfiumCore.getPageHeightPoint(document, index);

                bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

                pdfiumCore.renderPageBitmap(document, bitmap, index, 0, 0, width, height);
            }
            pdfiumCore.closeDocument(document);
        } catch (IOException e) {
            //TODO: Error: document is not pdf
            e.printStackTrace();
        }

        return bitmap;
    }

But the code throws an exception:

 java.io.IOException: cannot create document: File not in PDF format or corrupted

My document is pdf because I uploaded it in server, I tried many times with different pdfs. Also there is an issue in the github about this problem:

https://github.com/barteksc/AndroidPdfViewer/issues/175

But it is not solved yet, and unfortunately is closed. My question is how should I approach to find a solution, or what I'm doing wrong on my code. Thanks in adavance.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source