Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pdf support #31

Open
Bhoomi-Zalavadiya opened this issue Jun 16, 2020 · 1 comment
Open

Pdf support #31

Bhoomi-Zalavadiya opened this issue Jun 16, 2020 · 1 comment

Comments

@Bhoomi-Zalavadiya
Copy link

Bhoomi-Zalavadiya commented Jun 16, 2020

How can i print pdf file?
Can you please add the pdf print support?

@P-B1101
Copy link

P-B1101 commented May 12, 2022

you can convert pdf to bitmap and print bitmap.

private ArrayList<Bitmap> pdfToBitmap(File pdfFile) {
       ArrayList<Bitmap> bitmaps = new ArrayList<>();
       try {
           PdfRenderer renderer = new PdfRenderer(ParcelFileDescriptor.open(pdfFile, ParcelFileDescriptor.MODE_READ_ONLY));

           Bitmap bitmap;
           final int pageCount = renderer.getPageCount();
           for (int i = 0; i < pageCount; i++) {
               PdfRenderer.Page page = renderer.openPage(i);
               float ratio = 2.6f;
               float width = ratio * page.getWidth();
               float height = ratio * page.getHeight();
               bitmap = Bitmap.createBitmap((int) width, (int) height, Bitmap.Config.ARGB_8888);
               page.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);

               bitmaps.add(bitmap);

               // close the page
               page.close();

           }

           // close the renderer
           renderer.close();
       } catch (Exception ex) {
           ex.printStackTrace();
       }

       return bitmaps;
   }

the ratio value depends on your pdf width. I came with this amount with testing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants